Wednesday, 6 February 2013

Compare two files in cobol


1) Compare two files & write matching records to output file.

Option1: 
This program will degrade the performance but for understanding the logic this option has given. Option2 is efficient. It is explained below option1 program. 

       IDENTIFICATION DIVISION.                                        
       PROGRAM-ID. FILCMPR                                           
       ENVIRONMENT DIVISION.                                          
       CONFIGURATION SECTION                                       
       OBJECT-COMPUTER. IBM-390.                                  
       SOURCE-COMPUTER. IBM-390                                
       INPUT-OUTPUT SECTION.                                           
       FILE-CONTROL                                                  
           SELECT INFILE ASSIGN TO DD1                     
           FILE STATUS IS FS1.                                        
           SELECT OUTFILE ASSIGN TO DD2                
           FILE STATUS IS FS2.                                       
           SELECT MCHFILE ASSIGN TO DD3               
           FILE STATUS IS FS3.                                       
       DATA DIVISION.                                                 
       FILE SECTION.                                                   
       FD INFILE.                                                      
       01 INREC.                                                       
           05 EID1                  PIC X(05).                    
           05 FILLER              PIC X(01).                      
           05 ENAME1          PIC X(06).                       
           05 FILLER              PIC X(01).                      
           05 ESAL1              PIC 9(05).                      
           05 FILLER             PIC X(62).                      
       FD OUTFILE.                                                   
       01 OUTREC.                                                   
           05 EID2                 PIC X(05).                       
           05 FILLER              PIC X(01).                      
           05 EADD2             PIC X(10).                    
           05 FILLER              PIC X(64).                      
       FD MCHFILE.                                                  
       01 MCHREC.                                                  
           05 EIDO                 PIC X(05).                      
           05 FILLER               PIC X(01).                      
           05 ENAMEO          PIC X(06).                 
           05 FILLER               PIC X(01).                      
           05 ESALO               PIC 9(05).                     
           05 FILLER               PIC X(01).                      
           05 EADDO             PIC X(10).                   
           05 FILLER               PIC X(51).                      
       WORKING-STORAGE SECTION.                   
       01 FS1                       PIC X(02).                        
       01 FS2                       PIC X(02).                        
       01 FS3                       PIC X(02).                        
       01 SWITCH1             PIC X(01).                  
           88 EOF1                 VALUE 'Y'.                     
           88 NOT-EOF1        VALUE 'N'.               
       01 SWITCH2              PIC X(01).                 
           88 EOF2                  VALUE 'Y'.                    
           88 NOT-EOF2         VALUE 'N'.              
       01 WS-EID                   PIC X(05).                   
       PROCEDURE DIVISION.                               
           SET NOT-EOF1 TO TRUE                         
           PERFORM OPEN-PARA.                          
           PERFORM READ-PARA UNTIL EOF1.    
           PERFORM CLOSE-PARA.                         
           STOP RUN.                                                
       OPEN-PARA.                                                 
           OPEN INPUT INFILE                                 
           OPEN OUTPUT MCHFILE.                       
           DISPLAY ' INFILE  OPEN STATUS ' FS1
           DISPLAY ' MCHFILE OPEN STATUS ' FS3.                        
       READ-PARA.                                                      
           IF FS1 = '00'                                               
              READ INFILE                                            
                AT END                                                  
                   SET EOF1 TO TRUE                           
                NOT AT END                                         
                   MOVE EID1 TO WS-EID                                 
                    SET NOT-EOF2 TO TRUE                          
                     PERFORM OPEN-OUT-PARA                  
                     PERFORM READ-OUT-PARA UNTIL EOF2                  
                     PERFORM CLOSE-OUT-PARA                            
              END-READ                                                 
           END-IF.                                                     
       OPEN-OUT-PARA.                                        
           OPEN INPUT OUTFILE                             
           DISPLAY ' OUTFILE OPEN STATUS ' FS2.
       READ-OUT-PARA.                                          
           IF FS2 = '00'                                               
             READ OUTFILE                                         
              AT END                                                   
                SET EOF2 TO TRUE                              
              NOT AT END                                           
                IF WS-EID = EID2                                 
                   MOVE EID1 TO EIDO                       
                   MOVE ENAME1 TO ENAMEO        
                   MOVE ESAL1 TO ESALO                  
                   MOVE EADD2 TO EADDO               
                   WRITE MCHREC                                
                END-IF                                                  
             END-READ.                                             
       CLOSE-OUT-PARA.                                      
           CLOSE OUTFILE                                               
           DISPLAY ' OUTFILE CLOSE STATUS ' FS2.     
       CLOSE-PARA.                                                     
           CLOSE INFILE                                                
           CLOSE MCHFILE.                                           
           DISPLAY ' INFILE  CLOSE STATUS ' FS1.     
           DISPLAY ' MCHFILE CLOSE STATUS ' FS3. 

JCL to Execute above cobol program -
//STEP010    EXEC  IGYWCLG,MEM=FILCMPR                             
//SYSPRINT DD  SYSOUT=*                                          
//COBOL.SYSIN  DD  DSN=XX7384A.JCLLIB.SELFTEST.Y2013.COB(&MEM),  
//                             DISP=SHR                                                 
//GO.DD1      DD          *
                                                    
21400 SANDIP 33333                                               
21500 MANDIP 44444                                               
21600 RANDIP 99999                                               
21700 BALDIP 66666
                                               
//GO.DD2      DD         *                                                    
21500 PUNE                                                       
21600 MUMBAI                                                     
21900 CHENAI                                                     
22000 HYDERABAD
                                                  
//GO.DD3      DD          SYSOUT=*                                             
//LKED.SYSLMOD DD  DSN=XX7384A.COBOL.LOADLIB(&MEM),DISP=SHR      
//*
                                                              

Output :
21500 MANDIP 44444 PUNE      
21600 RANDIP 99999 MUMBAI
    


Above program will degrade the performance so this is not advisable.
Option2 program is efficient. 

Option2:
NOTE: Both the files are sorted on key and duplicates are removed from both input files.

IDENTIFICATION DIVISION.
PROGRAM-ID. COMPFIL.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT INFILE1 ASSIGN TO DD1.
  SELECT INFILE2 ASSIGN TO DD2.
  SELECT INFILEB ASSIGN TO DD3.
  SELECT INFILE1O ASSIGN TO DD4.
  SELECT INFILE2O ASSIGN TO DD5.
DATA DIVISION.
FILE SECTION.
FD INFILE1.
01 INREC1.
    05 ID1   PIC 9(03).
    05 FILLER PIC X(77).
FD INFILE2.
01 INREC2.
    05 ID2      PIC 9(03).
    05 FILLER PIC X(77).
FD INFILEB.
01 INRECB.
    FILLER      PIC X(80).
FD INFILE1O.
01 INREC1O.
     FILLER     PIC X(80).
FD INFILE2O.
01 INREC2O.
     FILLER     PIC X(80).
WORKING-STORAGE SECTION.
01 WS-VAR.
     05 SWT1             PIC X(01).
         88 EOF1          VALUE 'Y'.
         88 NOT-EOF1   VALUE 'N'.
     05 SWT2             PIC X(01).
         88 EOF2          VALUE 'Y'.
         88 NOT-EOF2   VALUE 'N'.
     05 WS-ID1          PIC 9(03).
     05 WS-ID2          PIC 9(03).
PROCEDURE DIVISION
     SET NOT-EOF1 TO TRUE
     SET NOT-EOF2 TO TRUE
     OPEN INPUT INFILE1
                       INFILE2
     OPEN OUTPUT INFILEB
                          INFILE1O
                          INFILE2O   
     PERFORM READ-FILE1 
     PERFORM READ-FILE2
     PERFORM MTCH-PARA
     PERFORM CLOS-PARA.
READ-FILE1.
     READ INFILE1 
           AT END 
                SET EOF1 TO TRUE
           NOT AT END
                MOVE ID1 TO WS-ID1
     END-READ.
READ-FILE2.
     READ INFILE2 
           AT END 
                SET EOF2 TO TRUE
           NOT AT END
                MOVE ID2 TO WS-ID2
     END-READ.
MTCH-PARA.
    IF EOF1 AND EOF2
        GO TO CLOS-PARA
    END-IF
    IF EOF1 AND NOT-EOF2
        PERFORM READ-FILE2
        INITIALIZE INREC2O
        MOVE INREC2 TO INREC2O
        WRITE INREC2O
        GO TO MTCH-PARA
   END-IF
   IF NOT-EOF1 AND EOF2
       PERFORM READ-FILE1 
       INITIALIZE INREC1O
       MOVE INREC1 TO INREC1O
       WRITE INREC1O 
   END-IF
   IF WS-ID1 = WS-ID2
       INITIALIZE INRECB
       MOVE INREC1 TO INRECB
       WRITE INRECB
       PERFORM READ-FILE1
       PERFORM READ-FILE2
       GO TO MTCH-PARA
   ELSE
       IF WS-ID1 < WS-ID2
           INITIALIZE INREC1 
           MOVE INREC1 TO INREC1O
           WRITE INREC1O
           PERFORM READ-FILE1
           GO TO MTCH-PARA
      ELSE
           INITIALIZE INREC2O
           MOVE INREC2 TO INREC2O
           WRITE INREC2O
           PERFORM READ-FILE2
           GO TO MTCH-PARA
      END-IF
   END-IF.
CLOS-PARA.
   CLOSE INFILE1
             INFILE2
             INFILEB
             INFILE1O
             INFILE2O.
   STOP RUN.              

JCL to run this program:
//AB7384A JOB (ACCT,U,A),'COMPFIL',CLASS=B,
//            MSGCLASS=O,NOTIFY=&SYSUID
//*
//   JCLLIB ORDER=AB7384A.SOURCE.PROCLIB
//STEP1 EXEC IGYWCLG,MEM=COMPFIL
//COBOL.SYSIN DD DSN=AB7384A.SOURCE.COBOL(&MEM),DISP=SHR
//GO.DD1 DD DSN=AB7384A.TEST.IN1,DISP=SHR
//GO.DD2 DD DSN=AB7384A.TEST.IN2,DISP=SHR
//GO.DD3 DD DSN=AB7384A.TEST.IN3,DISP=OLD
//GO.DD4 DD DSN=AB7384A.TEST.IN4,DISP=OLD
//GO.DD5 DD DSN=AB7384A.TEST.IN5,DISP=OLD 
//LKED.SYSLMOD DD DSN=AB7384A.COBOL.LOADLIB(&MEM),DISP=SHR
//GO.SYSIN DD *
//*

Input File1:
080
100
125
150
175
200

Input File2:
020
040
080
090
095
100
150
160
170
175
200
210
220

Match file:
080
100
150
175
200

Nomch file1:  (No Match Data from file1) 
125

Nomch file2:  (No Match Data from file2) 
020
040
090
160
170
210
220

6 comments:

  1. Don’t we have to sort the files before compare?

    ReplyDelete
    Replies
    1. if it is sorting order then only above coding will work.

      Delete
  2. We can do same logic for two ksds files using index organization?

    ReplyDelete
  3. I’m getting (000) of Nomch file1 data

    ReplyDelete