MERGE Operator (for Beginners)


Summary

For extensive information, go through the MERGE Operator for Experienced

MERGE operator merges one or more input files, processes them, and splits them into one or more output data sets.

Syntax -

Merging three input files to output file -

//TOOLIN DD *
  MERGE FROM(INDD01,INDD02,INDD03) TO(OUTPUT) USING(MERG)
/*
//MERGCNTL DD *
  MERGE FIELDS=(starting-position,length,type,ascending/descending)
/*

Merging three input files to two output files -

//TOOLIN DD *
  MERGE FROM(INDD01,INDD02,INDD03) USING(MERG)
/*
//MERGCNTL DD *
  MERGE FIELDS=(starting-position,length,type,ascending/descending)
  OUTFIL FNAMES=OUTDD01,INCLUDE=(starting-position,length,type,EQ,matching-value)
  OUTFIL FNAMES=OUTDD02,SAVE
/*
MERGE Operator

Required Operands


  • FROM - FROM parameter specifies ddname of the input file.
  • USING - USING operand specifies the first 4-characters of the ddname (xxxxCNTL) for the DFSORT control statement file.

Optional Operands


  • TO - TO operand specifies 1 to 10 ddnames of output files.
  • VSAMTYPE - VSAMTYPE operand specifies the record format for a VSAM input file (F or V).

Example -


Scenario - Merges the employee details and separates employee details based on the country code "IN", "US".

INPUT1 - MATEPK.INPUT.PSFILE

 BROWSE    MATEPK.INPUT.PSFILE                        Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
001  PAWAN         MAINFRAME           JPM       AP        IN                   
002  SRINIVAS      TESTING             ORACLE    TG        IN                   
003  SRIDHAR       SAS                 CG        OR        US                   
004  VENKATESH     ABAP                CSC       CA        US                   
005  RAVI          HADOOP              CTS       FL        US                   
006  PRASAD        HR                  INFOSYS   MI        US                   
007  RAJA          TESTING             IBM       CA        US                   
******************************** Bottom of Data ********************************

INPUT2 - MATEPK.INPUT.PSFILE4

 BROWSE    MATEPK.INPUT.PSFILE4                       Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
008  KUMAR         SAP BASIS           TCS       TN        IN                   
009  RAMESH        SAP SD              ORACLE    MN        US                   
010  RAVINDER      SAP ABAP            INFOTECH  AK        US                   
******************************** Bottom of Data ********************************

JCL -

***************************** Top of Data ******************************
//MATEPKME JOB (123),'MATEPK',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),        
//             NOTIFY=&SYSUID                                           
//*                                                                     
//STEP01   EXEC PGM=ICETOOL                                             
//INDD01   DD DSN=MATEPK.INPUT.PSFILE,DISP=SHR                          
//INDD02   DD DSN=MATEPK.INPUT.PSFILE4,DISP=SHR                         
//OUTDD1   DD DSN=MATEPK.OUTPUT.PSFILE3,                                
//            DISP=(NEW,CATLG,DELETE),VOLUME=SER=DEVHD4,                
//            SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA,                        
//            DCB=(DSORG=PS,RECFM=FB,LRECL=80,BLKSIZE=800)              
//OUTDD2   DD DSN=MATEPK.OUTPUT.PSFILE4,                                
//            DISP=(NEW,CATLG,DELETE),VOLUME=SER=DEVHD4,                
//            SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA,                        
//            DCB=(DSORG=PS,RECFM=FB,LRECL=80,BLKSIZE=800)              
//TOOLIN   DD *                                                         
  MERGE FROM(INDD01,INDD02) USING(CTL1)                                 
/*                                                                      
//CTL1CNTL DD *                                                         
  MERGE FIELDS=(1,3,ZD,A)                                               
  OUTFIL FNAMES=OUTDD1,INCLUDE(60,2,CH,EQ,C'IN')                        
  OUTFIL FNAMES=OUTDD2,INCLUDE(60,2,CH,EQ,C'US')                        
/*                                                                      
//TOOLMSG  DD SYSOUT=*                                                  
//DFSMSG   DD SYSOUT=*                                                  
//                                                                      
**************************** Bottom of Data ****************************

OUTPUT1 - MATEPK.OUTPUT.PSFILE3

 BROWSE    MATEPK.OUTPUT.PSFILE3                      Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
001  PAWAN         MAINFRAME           JPM       AP        IN                   
002  SRINIVAS      TESTING             ORACLE    TG        IN                   
008  KUMAR         SAP BASIS           TCS       TN        IN                   
******************************** Bottom of Data ********************************

OUTPUT2 - MATEPK.OUTPUT.PSFILE4

 BROWSE    MATEPK.OUTPUT.PSFILE4                      Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
003  SRIDHAR       SAS                 CG        OR        US                   
004  VENKATESH     ABAP                CSC       CA        US                   
005  RAVI          HADOOP              CTS       FL        US                   
006  PRASAD        HR                  INFOSYS   MI        US                   
007  RAJA          TESTING             IBM       CA        US                   
009  RAMESH        SAP SD              ORACLE    MN        US                   
010  RAVINDER      SAP ABAP            INFOTECH  AK        US                   
******************************** Bottom of Data ********************************

TOOLMSG (SDSF SPOOL) - Verify TOOLMSG for the return code of the submitted job.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9
********************************* TOP OF DATA ********************************************
1ICE600I 0 DFSORT ICETOOL UTILITY RUN STARTED                                   
                                                                                
 ICE650I 0 VISIT http://www.ibm.com/storage/dfsort FOR ICETOOL PAPERS, EXAMPLES AND MORE 
                                                                                
 ICE632I 0 SOURCE FOR ICETOOL STATEMENTS:  TOOLIN                               
                                                                                
                                                                                
 ICE630I 0 MODE IN EFFECT:  STOP                                                
                                                                                
            MERGE FROM(INDD01,INDD02) USING(CTL1)                               
ICE606I 0 DFSORT CALL 0001 FOR MERGE FROM MERGEIN  TO OUTFIL   USING CTL1CNTL COMPLETED
ICE602I 0 OPERATION RETURN CODE:  00                                            
                                                                                
                                                                                
 ICE601I 0 DFSORT ICETOOL UTILITY RUN ENDED - RETURN CODE:  00                  
******************************** BOTTOM OF DATA ******************************************

Explaining Solution -

  • INDD01,INDD02 - Specifies the ddnames for input files.
  • OUTDD1,OUTDD2 - Specifies the ddnames for output files.
  • TOOLIN DD * - Specifies the ICETOOL statements for DFSORT.
  • CTL1CNTL DD * - Specifies the DFSORT statements for processing.
  • TOOLMSG - Specifies where to write the ICETOOL processing messages.
  • DFSMSG - Specifies where to write the DFSORT processing messages.
  • MERGE FROM(INDD,INDD1) USING(CTL1) - Merges the datasets INDD01, INDD02 based on the CTL1 condition.
  • MERGE FIELDS=(1,3,ZD,A) - Merges the records based on the field exists at 1 to 3 colummns.
  • OUTFIL FNAMES=OUTDD1,INCLUDE(60,2,CH,EQ,C'IN') - Separates the records which are having "IN" from 60-62 columns and copies them to OUTDD1 dataset.
  • OUTFIL FNAMES=OUTDD2,INCLUDE(60,2,CH,EQ,C'US') - Separates the records which are having "US" from 60-62 columns and copies them to OUTDD2 dataset.