Skip to content

MERGE Statement

The MERGE statement merges multiple files with identical key layouts into a single file.

General Format:

       MERGE sort-file-1 
       { ON {ASCENDING} KEY { sort-key-1  }  
            {DESCENDING} 
       [COLLATING SEQUENCE {IS alphabet-name-1} 
       [WITH DUPLICATES IN ORDER] 
       USING file-2 { file-3}... 
       GIVING {file-4}  
       {OUTPUT PROCEDURE IS proc-1 [{THROUGH} proc-2]} 
                                    {THRU   }

Syntax:

  1. sort-file-n is a file described in the File Section with an SD.
  2. sort-key-n is a data element that has been named as a key for the SORT.
  3. alphabet-name is a user defined word.
  4. file-n is a file described in the File Section with an FD.
  5. proc-n is the name of a procedure or section in the program.

General Rules:

Overview of the MERGE Statement. The MERGE Statement is provided with a number of input files (USING files), all sorted on identical key layouts. The MERGE statement OPEN's all of the USING files, READ's the first record of each USING file, compares the records on the KEY, selects a record according to the KEY information, and WRITE's it to the temporary merge-file. When all of the records have been merged into the merge-file, they are transferred into the output file(s) either through the GIVING clause or the RETURN statement in an OUTPUT PROCEDURE.

When all of the records have been merged into the merge-file, there are two ways to return records from the merge-file into an output file. The simplest way, from a syntactic standpoint, is with the USING/GIVING syntax. This requires a statement, such as:

              MERGE ALLSTAR-MERGE 
      *MAJOR SORTKEY 
              ON ASCENDING KEY MEMBER-LAST-NAME 
      *MINOR SORTKEY 
              ON DESCENDING KEY MEMBER-TEAM 
      *DUPLICATES SORTED IN THE ORDER ACQUIRED 
              WITH DUPLICATES IN ORDER 
              USING BASEBALL-FILE, HOOP-FILE 
              GIVING ALLSTAR-FILE.

In this case, the MERGE statement uses the ALLSTAR-MERGE file as the temporary sort file, into which the records are written before they are transferred to the GIVING file.

A slightly more complex alternative, which provides the user with more flexibility involves using the OUTPUT PROCEDURE syntax. This requires a statement, such as:

       77 OUTPUT-MERGE-AT-END PIC X. 
              88 EOF-MERGE-FILE VALUE "Y". 

       ..... 

              MERGE ALLSTAR-MERGE 
      *MAJOR SORTKEY 
              ON ASCENDING KEY MEMBER-LAST-NAME 
      *MINOR SORTKEY 
              ON DESCENDING KEY MEMBER-TEAM 
      *DUPLICATES SORTED IN THE ORDER ACQUIRED 
              WITH DUPLICATES IN ORDER 
              USING BASEBALL-FILE, HOOP-FILE 
              OUTPUT PROCEDURE OUTPUT-PROC. 

       ..... 

              OUTPUT-PROC SECTION. 
              OUTPUT-PROCESS. 
                     OPEN OUTPUT ALLSTAR-FILE. 

                     PERFORM UNTIL EOF-MERGE-FILE 
                            RETURN ALLSTAR-MERGE 
                                   AT END MOVE "Y" TO OUTPUT-MERGE-AT-END 
                                   NOT AT END 
                                          WRITE ALLSTAR-INFO FROM MERGE-DATA 
                            END-RETURN 
                     END-PERFORM.

In this case, the MERGE statement uses the ALLSTAR-MERGE file as the temporary sort file, uses the OUTPUT PROCEDURE as an area where processing can be done, and controls the writing to the output file through the use of the RETURN statement.

Note that while the input files, and output file are described in the FILE SECTION of the program with FD’s, the ALLSTAR-MERGE file must be described in the FILE SECTION of the program with an SD.

General Rules:

  1. The MERGE statement performs the equivalent of OPEN INPUT for all of the USING files. After the MERGE is complete, the MERGE statement CLOSE's the USING and GIVING files.
  2. Sort-file-1 must be described with an SD in the FILE SECTION.
  3. The USING and GIVING files must be described with ORGANIZATION LINE SEQUENTIAL, or ORGANIZATION BINARY SEQENTIAL, and must be described with an FD in the FILE SECTION.
  4. The order in which the KEY clauses are listed indicates the order in which the sort keys are applied. The first KEY listed is the primary key, the second key listed is the second key, etc...
  5. The ASCENDING KEY clause causes values to sort from lowest to highest.
  6. The DESCENDING KEY clause causes values to sort from highest to lowest.
  7. If an ASCENDING/DESCENDING clause has been used, it will be applied to subsequent KEY clauses until an alternative DESCENDING/ASCENDING clause is used.
  8. The COLLATING SEQUENCE clause names the alphabet which is used for purposes of ordering KEY data in the MERGE. Usage of the COLLATING SEQUENCE clause in a MERGE statement overrides the naming of a PROGRAM COLLATING SEQUENCE in the object-computer paragraph.
  9. The MERGE statement writes duplicates in the order in which they are encountered. That is, if there are two USING files, and the current records in each file have duplicate keys, then the order of the listing of the USING files determines which record is written first.
  10. Files listed in the USING clause may not be OPEN when the MERGE statement executes. The MERGE statement OPEN's the files.
  11. All of the input files must be sorted on identical keys, as described by the KEY clause of the MERGE statement.
  12. Files listed in the GIVING clause may not be OPEN when the MERGE statement executes. The MERGE statement OPEN's the files.
  13. The OUTPUT PROCEDURE is executed after all of the files have been merged into the temporary merge file.
  14. Inside the OUTPUT PROCEDURE, the RETURN statement is used to transfer records from the merge file to the output file.
  15. When the OUTPUT PROCEDURE is finished, the MERGE statement closes all files.
  16. The MERGE statement updates file status variable that is defined for merge-file after performing OPEN, READ, WRITE, and CLOSE operations. File Status errors may be detected in DECLARATIVES.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. MERGE-1. 
       ENVIRONMENT DIVISION. 
       INPUT-OUTPUT SECTION. 
       FILE-CONTROL. 
      *MERGE INPUT FILE 
              SELECT BASEBALL-FILE ASSIGN TO "BASEBALLSTARS.TXT" 
              ORGANIZATION IS LINE SEQUENTIAL 
              ACCESS IS SEQUENTIAL 
              FILE STATUS IS BASEBALL-STAT. 

      *MERGE INPUT 
              SELECT HOOP-FILE ASSIGN TO "HOOPSTARS.TXT" 
              ORGANIZATION IS LINE SEQUENTIAL 
              ACCESS IS SEQUENTIAL 
              FILE STATUS IS HOOP-STAT. 

      *MERGE OUTPUT FILE 
              SELECT ALLSTAR-FILE ASSIGN TO "ALLSTARS.TXT" 
              ORGANIZATION IS LINE SEQUENTIAL 
              ACCESS IS SEQUENTIAL 
              FILE STATUS IS ALLSTARS-STAT. 

      *MERGE FILE (SD) 
              SELECT ALLSTAR-MERGE ASSIGN TO "MERGE-WORK". 

       DATA DIVISION. 
       FILE SECTION. 
       FD BASEBALL-FILE. 
       01 BASEBALL-INFO PIC X(40). 

       FD HOOP-FILE. 
       01 HOOP-INFO PIC X(40). 

       FD ALLSTAR-FILE. 
       01 ALLSTAR-INFO PIC X(40). 

       SD ALLSTAR-MERGE. 
       01 MERGE-DATA. 
              05 MEMBER-FIRST-NAME PIC X(10). 
              05 MEMBER-LAST-NAME PIC X(15). 
              05 MEMBER-TEAM PIC X(15). 

       WORKING-STORAGE SECTION. 
       77 BASEBALL-STAT PIC XX. 
       77 HOOP-STAT PIC XX. 
       77 ALLSTARS-STAT PIC XX. 
       77 DUMMY PIC X. 

       PROCEDURE DIVISION. 
       STAR-MERGE. 
              MERGE ALLSTAR-MERGE 
      *MAJOR SORTKEY 
              ON ASCENDING KEY MEMBER-LAST-NAME 
      *MINOR SORTKEY 
              ON DESCENDING KEY MEMBER-TEAM 
      *DUPLICATES SORTED IN THE ORDER ACQUIRED 
              WITH DUPLICATES IN ORDER 
              USING BASEBALL-FILE, HOOP-FILE 
              GIVING ALLSTAR-FILE. 

              DISPLAY "MERGE-1 FINISHED!" LINE 10 COL 10. 
              ACCEPT DUMMY LINE 10 COL 30. 
              STOP RUN.

→baseballstars.txt

       BABE          RUTH          YANKEES 
       LOU           GEHRIG        YANKEES 
       JOE           DIMAGGIO      YANKEES 
       ENOS          SLAUGHTER     CARDINALS 
       DOMINIC       DIMAGGIO      RED SOX 
       JOHNNY        BENCH         REDS 
       BARRY         BONDS         GIANTS 
       HANK          AARON         BRAVES

→hoopstars.txt

       LARRY         BIRD          CELTICS 
       MAGIC         JOHNSON       LAKERS 
       BILL          RUSSELL       CELTICS 
       JULIUS        ERVING        SIXERS 
       SPUD          WEBB          HAWKS 
       MOSES         MALONE        SIXERS 
       KC            JONES         CELTICS 
       MICHAEL       JORDAN        BULLS
Back to top