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.
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:
- Describe the sort or merge file to be used for sorting or merging.
- Describe the input to be sorted or merged. If you want to process the records before you sort them, code an input procedure.
- Describe the output from sorting or merging. If you want to process the records after you sort or merge them, code an output procedure.
- Request the sort or merge.
- Determine whether the sort or merge operation was successful.
Restrictions:
- You cannot run a COBOL program that contains
SORT
orMERGE
statements under z/OS UNIX. This restriction includes BPXBATCH. - You cannot use
SORT
orMERGE
statements in programs compiled with theTHREAD
option. This includes programs that use object-oriented syntax and multithreaded applications, both of which require theTHREAD
option.In addition, the COBOL program that uses
SORT
orMERGE
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. You 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 invokingDFSORT
. - Tape device is used for intermediate work storage.
- L5 is used in the
RECORD
statement ofDFSORT OPTION
control. L5 specifies the average record length. Instead of using L5, the same can be specified by using theAVGRLEN=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).
- The
Sorting a table
Describing the sort or merge file
Describing the input to sorting or merging
Describing the output from sorting or merging
Requesting the sort or merge
Determining whether the sort or merge was successful
Stopping a sort or merge operation prematurely
Improving sort performance with FASTSRT
Controlling sort behavior
DFSORT Application Programming Guide
CICS SORT application restrictions
SORT statement (Enterprise COBOL for z/OS Language Reference)
MERGE statement (Enterprise COBOL for z/OS Language Reference)