Comparing two files in cobol



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Comparing two files in cobol

Postby padma.ibmmf » Wed Mar 31, 2010 11:00 pm

Hi Friends,
i am a begineer in mainframes,i need your suggestion for my query in cobol file handling.
I have two infile -file1 & file2 has sorted records for ENO.
I have ENO,ENAME,EMOB in file1 & ENO1,ELCN,EDOB in file2.
Now i want to compare file1 & file2 based on ENO,the matching recoreds should write in outfile3 along with duplicates and unmatched records in outfile4.
Kindly help me out.
I have written code,but couldnt able to move duplicates in outfile3.Kindly correct me.


ID DIVISION.
PROGRAM-ID. COMPARE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INFILE1 ASSIGN DD1
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS INFL-ST1.
SELECT INFILE2 ASSIGN DD2
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS INFL-ST2.
*************************************
SELECT OUTFILE1 ASSIGN DD3
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS OUTFL-ST1.
SELECT OUTFILE2 ASSIGN DD4
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS OUTFL-ST2.
DATA DIVISION.
FILE SECTION.
FD INFILE1.
01 INREC1.
04 ENO PIC X(04).
04 NAME PIC X(20).
04 EMOB PIC X(10).
04 FILLER PIC X(46).
FD INFILE2.
01 INREC2.
04 ENO1 PIC X(04).
04 ELCN PIC X(05).
04 EDOB PIC X(10).
04 FILLER PIC X(61).
FD OUTFILE1.
01 OUTREC1.
04 ENO2 PIC X(04).
04 NAME2 PIC X(20).
04 EMOB2 PIC X(10).
04 ELCN2 PIC X(05).
04 EDOB2 PIC X(10).
04 FILLER PIC X(31).
FD OUTFILE2.
01 OUTREC2.
04 ENO3 PIC X(04).
04 NAME3 PIC X(20).
04 FILLER PIC X(56).
WORKING-STORAGE SECTION.
01 INFL-ST1 PIC X(2).
88 INFL-OK1 VALUE '00'.
88 END-INFL1 VALUE '10'.
01 INFL-ST2 PIC X(2).
88 INFL-OK2 VALUE '00'.
88 END-INFL2 VALUE '10'.
01 OUTFL-ST1 PIC X(2).
88 OUTFL-OK1 VALUE '00'.
88 END-OUTFL1 VALUE '10'.
01 OUTFL-ST2 PIC X(2).
88 OUTFL-OK2 VALUE '00'.
88 END-OUTFL2 VALUE '10'.
PROCEDURE DIVISION.
000-MAIN.
DISPLAY 'FILE COMPARING STARTS NOW'
PERFORM 100-OPEN-PARA
THRU 100-OPEN-PARA-EXIT
PERFORM 200-PROCESS-PARA
THRU 200-PROCESS-PARA-EXIT
UNTIL END-INFL1 AND END-INFL2
PERFORM 300-CLOSE-PARA
THRU 300-CLOSE-PARA-EXIT
PERFORM 999-STOP-RUN
THRU 999-STOP-RUN-EXIT.
100-OPEN-PARA.
OPEN INPUT INFILE1
IF INFL-OK1
DISPLAY 'INFILE OPEN SUCESSFULLY'
ELSE
DISPLAY 'ERROR OPENING INFILE: ' INFL-ST1
PERFORM 999-STOP-RUN
THRU 999-STOP-RUN-EXIT
END-IF
OPEN INPUT INFILE2
IF INFL-OK2
DISPLAY 'INFILE OPEN SUCESSFULLY'
ELSE
DISPLAY 'ERROR OPENING INFILE: ' INFL-ST2
PERFORM 999-STOP-RUN
THRU 999-STOP-RUN-EXIT
END-IF
*******************************************************
OPEN OUTPUT OUTFILE1
IF OUTFL-OK1
DISPLAY 'OUTFILE OPEN SUCESSFULLY'
ELSE
DISPLAY 'ERROR OPENING OUTFILE1: ' OUTFL-ST1
PERFORM 999-STOP-RUN
THRU 999-STOP-RUN-EXIT
END-IF
OPEN OUTPUT OUTFILE2
IF OUTFL-OK2
DISPLA 'OUTFILE OPEN SUCESSFULLY
ELSE
DISPLAY 'ERROR OPENING OUTFILE2: ' OUTFL-ST2
PERFORM 999-STOP-RUN
THRU 999-STOP-RUN-EXIT
END-IF.
100-OPEN-PARA-EXIT.
EXIT.
200-PROCESS-PARA.
READ INFILE1
IF INFL-OK1
DISPLAY 'INFILE READ SUCESSFULLY'
ELSE
DISPLAY 'ERROR READING INFILE1: ' INFL-ST1
GO TO 200-PROCESS-PARA-EXIT
END-IF
READ INFILE2
IF INF DISPLAY 'INFILE READ SUCESSFULLY'
ELSE
DISPLAY 'ERROR READING INFILE2: ' INFL-ST2
GO TO 200-PROCESS-PARA-EXIT
END-IF
IF ENO = ENO1
MOVE ENO TO ENO2
MOVE NAME TO NAME2
MOVE EMOB TO EMOB2
MOVE ELCN TO ELCN2
MOVE EDOB TO EDOB2
WRITE OUTREC1
EVALUATE OUTFL-ST1
WHEN '00'
CONTINUE
WHEN OTHER
DISPLAY 'ERROR WRITING THE OUTFILE1 ' OUTFL-ST1 L-OK2
GO TO 300-CLOSE-PARA
END-EVALUATE
ELSE
MOVE ENO TO ENO3
MOVE NAME TO NAME3
WRITE OUTREC2
EVALUATE OUTFL-ST2
WHEN '00'
CONTINUE
WHEN OTHER
DISPLAY 'ERROR WRITING THE OUTFILE2 ' OUTFL-ST2
GO TO 300-CLOSE-PARA
END-IF.
200-PROCESS-PARA-EXIT.
EXIT.
300-CLOSE-PARA.
CLOSE INFILE1
IF INFL-OK1
DISPLAY 'INFILE CLOSE SUCESSFULLY'
ELSE
DISPLAY 'ERROR CLOSING INFILE1: ' INFL-ST1
GO TO 300-CLOSE-PARA-EXIT
END-IF
CLOSE INFILE2
IF INFL-OK2
DISPLAY 'INFILE CLOSE SUCESSFULLY'
ELSE
DISPLAY 'ERROR CLOSING INFILE2: ' INFL-ST2
GO TO 300-CLOSE-PARA-EXIT
END-IF
*********************************************************
CLOSE OUTFILE1
IF OUTFL-OK1
DI SPLAY 'INFILE CLOSE SUCESSFULLY'
ELSE
DISPLAY 'ERROR CLOSING INFILE: ' OUTFL-ST1
GO TO 300-CLOSE-PARA-EXIT
END-IF
CLOSE OUTFILE2
IF OUTFL-OK2
DISPLAY 'INFILE CLOSE SUCESSFULLY'
ELSE
DISPLAY 'ERROR CLOSING INFILE: ' OUTFL-ST2
PERFORM 999-STOP-RUN
THRU 999-STOP-RUN-EXIT
END-IF.
300-CLOSE-PARA-EXIT.
EXIT.
999-STOP-RUN.
DISPLAY ' PGM ENDED'
STOP RUN.
padma.ibmmf
 
Posts: 5
Joined: Fri Mar 05, 2010 4:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Comparing two files in cobol

 

Re: Comparing two files in cobol

Postby Robert Sample » Thu Apr 01, 2010 12:10 am

What do you mean by "duplicates" -- can ENO in file 1 have duplicates? can ENO in file 2 have duplicates? what do you want to have happen if there is one record for an ENO value in file 1 and more than one in file 2? what do you want to have happen if there is more than one record for an ENO value in file 1 and one in file 2? what do you want to happen if BOTH file 1 and file 2 have more than one record for an ENO value?

Without the duplicates, this is a classic two-way file matching scenario and there are plenty of references all over as to how to do it.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

Re: Comparing two files in cobol

Postby dick scherrer » Thu Apr 01, 2010 12:21 am

Hello,

At the top od the COBOL part of the forum is a "Sticky" with working sample code to do what you want.

Save the file to your system, copy it onto the mainframe, modify to suit your files and any additona business logic, and you should be ready to run. Suggest you make sure the match/merge code is working (and that you understand how it works) before adding any additional business logic.

This has been implemented thousands of times, so you should not need to start from scratch. . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: Comparing two files in cobol

Postby padma.ibmmf » Thu Apr 01, 2010 8:46 am

Hi Robert,
Thanks for your reply.
Assume i have 1100 records in file1 and 1800 records in file2.
Both are sorted file based on ENO,only file2 have duplicates in ENO.
Now i want to compare file1 and file2 ,have to retrive records that are matching along with duplictes in outfile1,and unmatched records in outfile2 by using only cobol file handling,no sort should be used in cobol PGM
I have mentioned my pgm,kindly help me to update it.
padma.ibmmf
 
Posts: 5
Joined: Fri Mar 05, 2010 4:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Comparing two files in cobol

Postby padma.ibmmf » Thu Apr 01, 2010 8:48 am

Hello Dick,
Thanks for your reply..I will work on it..It is very useful...
padma.ibmmf
 
Posts: 5
Joined: Fri Mar 05, 2010 4:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Comparing two files in cobol

Postby dick scherrer » Thu Apr 01, 2010 9:07 am

Good luck - someone will be here if there are questions :)

d
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post