Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท File Handling

Sorting and Merging Files

Learn the COBOL SORT and MERGE statements - SD files, keys, USING and GIVING, INPUT and OUTPUT PROCEDURE with RELEASE and RETURN, duplicates and control-break reports on sorted data.

Batch work is full of questions that only make sense on ordered data: "total per department", "customers by region", "match yesterday's transactions to the master file". COBOL has the sort built into the language. SORT reorders a file, and MERGE combines several files that are already in order. You do not write the algorithm; you describe the keys and COBOL (or the system sort utility behind it, such as DFSORT on z/OS) does the work.

The sort file (SD)

Sorting needs a work file. It is declared with SELECT like any file, but in the File Section it gets an SD (sort description) instead of an FD:

COBOL
           SELECT SALES-IN   ASSIGN TO "sales.dat"
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT SALES-OUT  ASSIGN TO "sales.srt"
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT SORT-WORK  ASSIGN TO "sortwork".
       ...
       FD  SALES-IN.
       01  SALES-IN-REC    PIC X(30).
       FD  SALES-OUT.
       01  SALES-OUT-REC   PIC X(30).
       SD  SORT-WORK.
       01  SORT-REC.
           05  SR-REGION   PIC X(4).
           05  SR-SALESMAN PIC X(10).
           05  SR-AMOUNT   PIC 9(7)V99.
           05  FILLER      PIC X(7).

The sort key fields must be defined in the SD record. The input and output files are ordinary files and must not be open when SORT starts; the statement opens and closes them itself.

SORT with USING and GIVING

The simplest form names an input file, an output file and the keys:

COBOL
           SORT SORT-WORK
               ON ASCENDING KEY SR-REGION
               ON DESCENDING KEY SR-AMOUNT
               USING SALES-IN
               GIVING SALES-OUT.
  • Keys are listed from major to minor: here records are ordered by region, and within a region by amount from the largest.
  • USING reads every record from the file(s) into the sort; GIVING writes the sorted result.
  • Equal keys come out in an unspecified order unless you add WITH DUPLICATES IN ORDER, which keeps the original order among equal records.
  • Character keys sort by the machine's collating sequence: ASCII on most open systems and EBCDIC on IBM mainframes. The two differ: in EBCDIC lowercase letters come before uppercase, which come before digits; in ASCII it is digits, then uppercase, then lowercase. You can name another sequence with COLLATING SEQUENCE IS.
flowchart LR
    I["SALES-IN"] --> S["SORT<br/>keys: region, amount"]
    S --> O["SALES-OUT<br/>ordered"]
    S -. "work files" .- W["SORT-WORK"]

The sort sits between an input file and an output file; the work file is used internally.

INPUT PROCEDURE and OUTPUT PROCEDURE

When you need to select, change or build records on the way in or out, replace USING or GIVING with a procedure:

COBOL
           SORT SORT-WORK
               ON ASCENDING KEY SR-REGION
               INPUT PROCEDURE IS SELECT-SALES
               OUTPUT PROCEDURE IS PRINT-SALES.

Input procedure reads your own file, filters, and hands records to the sort with RELEASE:

COBOL
       SELECT-SALES SECTION.
           OPEN INPUT SALES-IN
           READ SALES-IN AT END SET AT-EOF TO TRUE END-READ
           PERFORM UNTIL AT-EOF
               IF SALES-IN-REC (1:4) NOT = "VOID"
                   MOVE SALES-IN-REC TO SORT-REC
                   RELEASE SORT-REC
               END-IF
               READ SALES-IN AT END SET AT-EOF TO TRUE END-READ
           END-PERFORM
           CLOSE SALES-IN.

Output procedure gets the sorted records one by one with RETURN (the sort-file equivalent of READ):

COBOL
       PRINT-SALES SECTION.
           RETURN SORT-WORK
               AT END SET NO-MORE TO TRUE
           END-RETURN
           PERFORM UNTIL NO-MORE
               PERFORM FORMAT-LINE
               RETURN SORT-WORK
                   AT END SET NO-MORE TO TRUE
               END-RETURN
           END-PERFORM.

Rules to remember:

  • The procedures are named sections; the code in them runs when the sort calls them.
  • RELEASE is only legal in the input procedure and RETURN only in the output procedure.
  • Inside the procedures you open and close your own files; the sort manages only its work area.
  • You can mix: USING with an OUTPUT PROCEDURE, or an INPUT PROCEDURE with GIVING.

This is the pattern for a report: output procedure walks the sorted records and prints a total whenever the region changes (a control break, see the payroll project).

MERGE

MERGE combines two or more files that are already sorted on the same keys into one. It does not sort; it interleaves records:

COBOL
           MERGE SORT-WORK
               ON ASCENDING KEY SR-REGION
               USING NORTH-SALES SOUTH-SALES
               GIVING ALL-SALES.

MERGE has no input procedure (the files are used as they are), but an OUTPUT PROCEDURE is allowed. It is much faster than sorting the combined data. It is the way to merge daily files into a weekly file, provided each daily file was sorted on the same keys. If an input file is out of order, the result is out of order too.

Sort registers and failures

COBOL provides special registers to control a sort: SORT-RETURN (set to 0 on success, 16 on failure, and you can set 16 to end a sort early in a procedure), SORT-FILE-SIZE, SORT-CORE-SIZE and, on IBM systems, SORT-MESSAGE. After a SORT check SORT-RETURN:

COBOL
           IF SORT-RETURN NOT = 0
               DISPLAY "Sort failed, code " SORT-RETURN
               MOVE 16 TO RETURN-CODE
           END-IF

On a mainframe the real work is done by the shop's sort product, and the JCL must provide SORTWKnn work data sets and an SYSOUT (JCL for COBOL jobs).

Trying the logic

These Python stand-ins model the data flow: ordering with major and minor keys, the input-filter-release pattern, and a merge of two ordered streams.

Python Stand-in: SORT with major and minor keys, and an input filter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: MERGE of two sorted files and a control break

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first block prints NORT Alex 2100, NORT Kim 1250, SOUT Mara 1900, SOUT Lee 900. The second block prints the six merged pairs, then total A 19 and total B 18.

Common mistakes

  1. Opening the USING or GIVING file before the SORT: the statement fails because the file is already open.
  2. Listing keys in the wrong order, so the minor key becomes the major one.
  3. Trying to MERGE unsorted files.
  4. Forgetting that a key's data type matters: a numeric key sorts numerically, but the same digits in an alphanumeric field sort as text ("10" before "9").
  5. Sorting huge files with a tiny work area. Give the sort enough memory or work space.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do you use instead of FD for the file that SORT works on?

    A sort or merge file is described with SD (sort description).
  2. 2. Which statement hands a record to the sort from an input procedure?

    RELEASE sends the record to the sort; RETURN fetches sorted records in the output procedure.
  3. 3. What does MERGE require of its input files?

    MERGE interleaves ordered files; it does not sort them.
  4. 4. In SORT ON ASCENDING KEY A ON DESCENDING KEY B, which key is major?

    Keys are listed from most to least significant, so A is major.

Summary

  • SORT orders records by one or more keys (major first); the work file is described with SD.
  • USING and GIVING are the easy path; INPUT PROCEDURE with RELEASE and OUTPUT PROCEDURE with RETURN give you control.
  • MERGE combines files that are already sorted on the same keys.
  • Check SORT-RETURN, mind the collating sequence, and remember sorted data is what makes control-break reports possible.

Next: CALL and the Linkage Section