TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Sort & Merge Statement Introduction


Sequential files are used very commonly in data processing applications. The records in these files usually need to be put in ascending or descending order for proper, easy, and fast accessing.

Sorting data in a file or combining two or more files is a common requirement in nearly all applications.

Sorting is a technique used to arrange the records in either ascending or descending order to be able to perform the sequential processing.

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 - 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.

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 output from sorting or merging. If you want to process the records after you sort or merge them, code an output procedure.

  3. Request the sort or merge.

  4. Determine whether the sort or merge operation was successful.

We are going to see detailed explanation of each steps in next chapter..

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal