Sorting and merging files

You can arrange records in a particular sequence by using a SORT or MERGE statement. You can mix SORT and MERGE statements in the same COBOL program.

Note: The SORT statement, sort processes, and sort restrictions that are described in this topic relate to the format 1 SORT statement only. For more information about sorting a table by using the format 2 SORT statement, see Sorting a table.
SORT statement
Accepts input (from a file or an internal procedure) that is not in sequence, and produces output (to a file or an internal procedure) in a requested sequence. You can add, delete, or change records before or after they are sorted.
MERGE statement
Compares records from two or more sequenced files and combines them in order. You can add, delete, or change records after they are merged.

A program can contain any number of sort and merge operations. They can be the same operation performed many times or different operations. However, one operation must finish before another begins.

With Enterprise COBOL, your IBM® licensed program for sorting and merging must be DFSORT or an equivalent. Where DFSORT is mentioned, you can use any equivalent sort or merge product.

COBOL programs that contain SORT or MERGE statements can reside above or below the 16 MB line.

The steps you take to sort or merge are generally as follows:

  1. Describe the sort or merge file to be used for sorting or merging.
  2. Describe the input to be sorted or merged. If you want to process the records before you sort them, code an input procedure.
  3. Describe the output from sorting or merging. If you want to process the records after you sort or merge them, code an output procedure.
  4. Request the sort or merge.
  5. Determine whether the sort or merge operation was successful.

Restrictions:

  • You cannot run a COBOL program that contains SORT or MERGE statements under z/OS UNIX. This restriction includes BPXBATCH.
  • You cannot use SORT or MERGE statements in programs compiled with the THREAD option. This includes programs that use object-oriented syntax and multithreaded applications, both of which require the THREAD option. Start of changeIn addition, the COBOL program that uses SORT or MERGE statements cannot call directly or indirectly other applications that require z/OS Unix System Services or applications that use multithreading. For example, the JVM uses both of them.End of change
  • Start of changeYou cannot use the DFSORT conventional technique. The conventional technique is used in the following cases:
    • The NOBLKSET option is specified. BLKSET is the default when invoking DFSORT.
    • Tape device is used for intermediate work storage.
    • L5 is used in the RECORD statement of DFSORT OPTION control. L5 specifies the average record length. Instead of using L5, the same can be specified by using the AVGRLEN=n statement.

    To assist migration of legacy COBOL programs, toleration runtime APARs PH20569(V2R2) and PH21261(V2R3/V2R4) are provided for programs running in AMODE 31. There is no toleration for AMODE 64. For details, see Using DFSORT option NOBLKSET (Enterprise COBOL Migration Guide).

    End of change

related concepts  
Sort and merge process

related references   
CICS SORT application restrictions
  
SORT statement (Enterprise COBOL for z/OS Language Reference)  
MERGE statement (Enterprise COBOL for z/OS Language Reference)