Describe the sort files and merge files wnh SELECT statements in the Environment Division and SD (Sort File Description) entries in the Data Division. The sort file or merge file described in an SD entry is the working file used during the sort or merge operation.

You cannot execute any input/output statements for this file, and you do not create a DD statement in the run-time JCL for the file. Do not specify a block size for this file, because these factors are not under your control.

Code FD (File Description) entries, if needed, to describe files used as input to or output from a sort or merge operation. You can also sort or merge records that are defined only in Working Storage. If you are only sorting or merging data items from Working Storage and are not using files as input to or output from a sort or merge operation, you still need SD and FILE-CONTROL entries for the sort-file or merge-file. Every SD entry must contain a record description, for example:



The sort files and merge files are processed through a SORT or MERGE statement in the Procedure Division. The statement specifies the key field(s) within the record upon which the sort or merge is to be sequenced.

You can specify a key or keys as ascending or descending, or when you specify more than one key, as a mixture of the two.

You can mix SORT and MERGE statements in the same program. Within the limits of virtual storage, a VS COBOL II program can contain any number of sort or merge operations, each with its own independent input or output procedure.

The SORT Statement

You can specify input procedures to be performed on the sort records before they are sorted (SORT . . . INPUT PROCEDURE). You can specify output procedures to be performed on the sort records after they are sorted (SORT . . . OUTPUT PROCEDURE). You can use input or output procedures to add, delete, alter, edit, or otherwise modify the records. You can use the SORT statement to:

  • Sort data items (including tables) in Working-Storage
  • Read records directly into the new file without any preliminary processing (SORT ... USING)
  • Transfer sorted records directly to a file without any further processing (SORT ... GIVING).

A COBOL program containing a sort operation is usually organized so that one or more input files are read and operated on by an input procedure. Within the input procedure, a RELEASE statement (analogous to the WRITE statement) places a record into the file to be sorted. That is, when input procedure execution is completed, all the records that are to be sorted have been given to DFSORT. If you do not want to modify or process the records before the sorting operation begins, the SORT statement USING option releases the unmodified records to the new file.

After all the input records have been passed to DFSORT, the sorting operation is executed. This operation arranges the entire set of records in the sequence specified by the key(s). After completion of the sorting operation, sorted records can be made available, one at a time, through a RETURN statement, for modification in an output procedure. If you don't want to modify or process the sorted records, the SORT statement GIVING option names the output file and writes the sorted records to an output file.

The MERGE Statement

You have access to output procedures (used after merging) that can modify the output records. Unlike the SORT statement, you cannot specify an input procedure in the MERGE statement; you must use MERGE . . . USING. The files to be merged must already be in the same sequence. The merge program then combines them into one sequenced file. A message may or may not be issued if the records within the files are out of sequence when Merge is called from a COBOL job.

The MERGE statement execution begins the merge processing. This operation compares keys within the records of the input files, and passes the sequenced records one-by-one to the RETURN statement of an output procedure or to the file named in the GIVING phrase.

If you want to process the merged records, they can be made available to your COBOL program, one at a time, through a RETURN statement in an output procedure. If you don't want to modify or process the merged records, the MERGE statement GIVING phrase names the merged output file into which the merged records will be written.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

IBM Mainframe Topics