MERGE Statement


The MERGE statement is used to combine two or more sequentially ordered files into a single, merged, and sequentially ordered output file. The files should be in sorted order according to the ascending or descending key that is common for both files.

This operation is usually required when we have multiple files with data in a similar format and want to generate a consolidated, sorted dataset. A MERGE statement can be coded anywhere in the procedure division except in a declarative section.

Syntax -

MERGE work-file-1
     ON ASCENDING/DESCENDING KEY key-name-1 [key-name-2 ...]
     [COLLATING SEQUENCE {IS} alphabet-name-1]
     [USING input-file-1 [, input-file-2, ...]]
	 [OUTPUT PROCEDURE IS para-1 THRU para-2]
     [GIVING output-file-1 [, output-file-2, ...]]

Parameters -

  • work-file-1 - The name of the merge work file. It's a logical file and does not correspond to any physical file.
  • ON ASCENDING/DESCENDING KEY - Defines the sorting order for the merge based on the specified keys.
  • key-name-1, key-name-2, ... - Specifies the data items that determine the sort order.
  • COLLATING SEQUENCE {IS} alphabet-name-1 - Optional. Specifies the collating sequence for sorting. If not mentioned, machine's native sequence is used.
  • USING input-file-1, input-file-2, ... - Specifies the input files that we want to merge. They should be sorted in the sequence mentioned by the sort keys.
  • OUTPUT PROCEDURE IS para-1 THRU para-2 - Specifies the name of a procedure used to select or modify output records from the merge operation.
  • GIVING output-file-1, output-file-2, ... - Specifies the output file or files to which the merged records are written.

Merge processing steps -

  • It opens work file in I-O mode, input files in INPUT mode, and output file in OUTPUT mode.
  • Transfers the records from all input files to the work file using the order coded with key-name-1.
  • Transfers the sorted records from work file to output file.
  • Closes the input, output files and releases (deletes) the work-file.

Practical Example -


Scenario - Let us assume two different employee files contain employee information, and create one merged file containing all employee details from two files.

Input -

MATEPK.EMPFILE.INPUT1

----+----1----+----2----+----3----+----4----+--
E0001EMPLOYEE1     DIR       LOC1 0000100000   
E0002EMPLOYEE2     MGR  DEPT1LOC1 0000080000   
E0004EMPLOYEE4     TL   DEPT1LOC1 0000050000   
E0006EMPLOYEE6     SE   DEPT1LOC1 0000034000

MATEPK.EMPFILE.INPUT2

----+----1----+----2----+----3----+----4----+--
E0003EMPLOYEE3     MGR  DEPT2LOC2 0000075000   
E0005EMPLOYEE5     SSE  DEPT1LOC1 0000045000   
E0007EMPLOYEE7     SSE  DEPT2LOC2 0000046000  

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MERGEFLS. 
       AUTHOR. MTH.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT EMPFILE1 ASSIGN TO INPUT1.  
           SELECT EMPFILE2 ASSIGN TO INPUT2.  
           SELECT EMPFILEO ASSIGN TO OUTPUT1. 
           SELECT WORKFILE ASSIGN TO WORK1.
 
       DATA DIVISION.
       FILE SECTION.
	  * Input File1
       FD EMPFILE1
           RECORD CONTAINS 47 CHARACTERS
           DATA RECORD EMP-REC1
           RECORDING MODE F.
       01 EMP-REC1.
          COPY EMPREC.
	  * Input File2
       FD EMPFILE2
           RECORD CONTAINS 47 CHARACTERS
           DATA RECORD EMP-REC2
           RECORDING MODE F.
       01 EMP-REC2.
          COPY EMPREC.
	  * Output file
       FD EMPFILEO
           RECORD CONTAINS 47 CHARACTERS
           DATA RECORD EMP-RECO 
           RECORDING MODE F.
       01 EMP-RECO.
          COPY EMPREC.
	  * Merge work file
       SD WORKFILE
           RECORD CONTAINS 47 CHARACTERS
           DATA RECORD WORK-REC
           RECORDING MODE F.
       01 WORK-REC.
          05 WORK-EMP-NUM         PIC 9(05).
          05 WORK-REM-REC         PIC X(42).

       PROCEDURE DIVISION.
           MERGE WORKFILE
              ON ASCENDING KEY WORK-EMP-NUM
                 USING EMPFILE1, EMPFILE2
                 GIVING EMPFILEO.
           STOP RUN.

JCL -

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//STEP01  EXEC PGM=MERGEFLS
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//INPUT1   DD  DSN=MATEPK.EMPFILE.INPUT1,DISP=SHR
//INPUT2   DD  DSN=MATEPK.EMPFILE.INPUT2,DISP=SHR
//OUTPUT1  DD  DSN=MATEPK.EMPFILE.OUTPUT1,
//            DISP=(NEW,CATLG,DELETE),
//            SPACE=(TRK,(3,2),RLSE),
//            UNIT=SYSDA,
//            DCB=(DSORG=PS,RECFM=FB,LRECL=47,BLKSIZE=470)
//WORK1    DD  DSN=&&TEMP,
//            DISP=(NEW,DELETE,DELETE),
//            SPACE=(CYL,(10,5),RLSE),
//            UNIT=SYSDA,
//            DCB=(DSORG=PS,RECFM=FB,LRECL=47,BLKSIZE=470)
//SYSOUT   DD  SYSOUT=*  

Output (MATEPK.EMPFILE.OUTPUT1) -

----+----1----+----2----+----3----+----4----+--
E0001EMPLOYEE1     DIR       LOC1 0000100000   
E0002EMPLOYEE2     MGR  DEPT1LOC1 0000080000   
E0003EMPLOYEE3     MGR  DEPT2LOC2 0000075000   
E0004EMPLOYEE4     TL   DEPT1LOC1 0000050000   
E0005EMPLOYEE5     SSE  DEPT1LOC1 0000045000   
E0006EMPLOYEE6     SE   DEPT1LOC1 0000034000   
E0007EMPLOYEE7     SSE  DEPT2LOC2 0000046000   

Explaining Example -

In the above example, MERGE statement merges two sorted input files based on the employee number (1-5 bytes) in both files and writes into the output files.