Wednesday, 6 February 2013

Compare two files in cobol


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

       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
    

1 comment: