Comparing 2 files and writing matched and unmatched records into 2 different files is the very basic program for any development projects using COBOL as programming language.
Below program compares the given 2 files and writes the unmatched records into output.
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPARE.
AUTHOR. SAJID MD.
******************************************************************
* PROGRAM PURPOSE:
*
* ---------------
*
* TO COMPARE 2 GIVEN FILES. *
* WRITE THE UNMATCHED RECORDS. *
******************************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-370.
OBJECT-COMPUTER. IBM-370.
SPECIAL-NAMES.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT I-FILE-1 ASSIGN TO IFILE1.
SELECT I-FILE-2 ASSIGN TO IFILE2.
SELECT O-OUTPUT-FILE ASSIGN TO OUTFILE.
******************************************************************
DATA DIVISION.
******************************************************************
FILE SECTION.
FD
I-FILE-1
RECORD CONTAINS 080 CHARACTERS
RECORDING MODE IS F
LABEL RECORD ARE STANDARD
DATA RECORD IS I-REC-1.
01
I-REC-1 PIC
X(080).
FD
I-FILE-2
RECORD CONTAINS 200 CHARACTERS
RECORDING MODE IS F
LABEL RECORD ARE STANDARD
DATA RECORD IS I-REC-2.
01
I-REC-2 PIC
X(200).
FD
O-OUTPUT-FILE
RECORDING MODE IS F
LABEL RECORD ARE STANDARD
RECORD CONTAINS 080 CHARACTERS
DATA RECORD IS O-OUTPUT-REC.
01
O-OUTPUT-REC PIC
X(080).
*===========================================================
WORKING-STORAGE SECTION.
*===========================================================
01 WS-EOF-1 PIC X VALUE 'N'.
88 END-OF-1 VALUE 'Y'.
01
WS-EOF-2
PIC X VALUE 'N'.
88 END-OF-2 VALUE 'Y'.
01
WS-VARIABLES.
05
WS-REC-1-READ PIC 9(9) VALUE ZERO.
05
WS-REC-2-READ PIC
9(9) VALUE ZERO.
05
WS-REC-WRITTEN PIC
9(9) VALUE ZERO.
01 WS-REC-1.
05 WS-KEY-1.
10
WS-CMP-1 PIC
9(06).
10
WS-FSC-1 PIC
9(08).
10
WS-LINE-1 PIC
9(05).
05 FILLER PIC X(181) VALUE SPACES.
01 WS-REC-2.
05 WS-KEY-2.
10
WS-CMP-2 PIC
9(06).
10
WS-FSC-2 PIC
9(08).
10
WS-LINE-2 PIC
9(05).
05 FILLER PIC X(061) VALUE
SPACES.
01 OUT-REC PIC X(080).
*
==========================================================
* MAIN PROGRAM
*
==========================================================
PROCEDURE DIVISION.
PERFORM 1000-INITIALIZE THRU
1000-EXIT.
DISPLAY 'STARTED COMPARE.'.
PERFORM 2000-MAIN-PROCESS THRU
2000-EXIT
UNTIL END-OF-1.
PERFORM 3000-DISPLAY-COUNTS THRU
3000-EXIT.
CLOSE I-FILE-1
I-FILE-2
O-OUTPUT-FILE.
GOBACK.
*===========================================================
1000-INITIALIZE.
*===========================================================
DISPLAY 'PROGRAM STARTED'.
OPEN INPUT I-FILE-1
I-FILE-2.
OPEN OUTPUT O-OUTPUT-FILE.
PERFORM 2100-READ-FILE-1
THRU 2100-EXIT.
PERFORM 2200-READ-FILE-2
THRU 2200-EXIT.
1000-EXIT.
EXIT.
*===========================================================
2000-MAIN-PROCESS.
*===========================================================
IF WS-KEY-1 = WS-KEY-2
PERFORM 2100-READ-FILE-1 THRU
2100-EXIT
PERFORM 2200-READ-FILE-2 THRU
2200-EXIT
GO TO 2000-EXIT
END-IF.
IF WS-KEY-1 < WS-KEY-2
PERFORM 2500-WRITE-ITEM THRU 2500-EXIT
PERFORM 2100-READ-FILE-1 THRU
2100-EXIT
GO TO 2000-EXIT
END-IF.
IF WS-KEY-1 > WS-KEY-2
PERFORM 2200-READ-FILE-2 THRU
2200-EXIT
GO TO 2000-EXIT
END-IF.
2000-EXIT.
EXIT.
*===========================================================
2100-READ-FILE-1.
*===========================================================
******** DISPLAY '2100-READ-FILE-1.'
READ I-FILE-1
INTO WS-REC-1
AT END
SET END-OF-1 TO TRUE
GO TO 2100-EXIT
END-READ.
ADD 1 TO WS-REC-1-READ.
2100-EXIT.
EXIT.
*===========================================================
2200-READ-FILE-2.
*===========================================================
******** DISPLAY '2200-READ-FILE-2.'
READ I-FILE-2
INTO WS-REC-2
AT END
SET END-OF-2 TO TRUE
GO TO 2200-EXIT
END-READ.
ADD 1 TO WS-REC-2-READ.
2200-EXIT.
EXIT.
*===========================================================
2500-WRITE-ITEM.
*===========================================================
********* DISPLAY '2500-WRITE-ITEM'.
INITIALIZE OUT-REC.
MOVE WS-REC-1 TO OUT-REC.
WRITE O-OUTPUT-REC FROM
OUT-REC.
ADD 1 TO WS-REC-WRITTEN.
2500-EXIT.
EXIT.
*===========================================================
3000-DISPLAY-COUNTS.
*===========================================================
DISPLAY
'*****************************************'.
DISPLAY ' COUNTS '.
DISPLAY
'*****************************************'.
DISPLAY 'RECORDS READ FROM FILE 1
-'
WS-REC-1-READ.
DISPLAY 'RECORDS READ FROM FILE 2
-'
WS-REC-2-READ.
DISPLAY 'TOTAL RECORDS WRITTEN - '
WS-REC-WRITTEN.
DISPLAY '*****************************************'.
3000-EXIT.
EXIT.