COBOL Tutorial

Progress0 of 0 lessons

COBOL External Sort Utility

An external sort utility is a separate program or JCL step that sorts (or merges) data outside your COBOL program. On the mainframe, DFSORT (or SYNCSORT) is commonly used: the job runs the sort step, then your COBOL program reads or writes the sorted file. This page explains what an external sort is, when to use it instead of the COBOL SORT statement, and how COBOL programs work with externally sorted data.

Explain Like I'm Five: External vs In-Program Sort

Imagine you have a stack of cards to sort. In-program sort is like sorting them yourself at your desk: you use COBOL (RELEASE, SORT, RETURN) to do it inside your program. External sort is like giving the stack to a separate machine that sorts them, then giving you back the sorted stack. Your program only sees the result: a file that is already in order. The "machine" is the sort utility (e.g. DFSORT) run as its own step in the job.

What Is an External Sort Utility?

An external sort utility runs as a separate step in a batch job. It reads from an input file (or multiple files for merge), sorts (or merges) the records, and writes to an output file. The COBOL program does not execute the sort; it runs in a different step and simply uses the sorted file. On z/OS, the usual utility is DFSORT (or a compatible product like SYNCSORT). You invoke it via JCL and pass control statements (SORT FIELDS=, INCLUDE/OMIT, SUM, OUTREC, etc.) to define keys and optional transformations.

How COBOL Uses Externally Sorted Data

A typical flow is: (1) An earlier step (or a prior job) creates an unsorted file. (2) The sort step runs: SORTIN is the input, SORTOUT is the output, and the utility sorts by the specified keys. (3) Your COBOL program runs in the next step and opens SORTOUT as a normal sequential file. It reads records in sorted order; no SORT statement is needed. If your program produces a file that must be sorted for a downstream job, your step writes a file that becomes SORTIN for a later sort step.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
*> This program reads a file that was sorted by a previous JCL step. *> It does not perform the sort; it just processes records in order. FILE-CONTROL. SELECT SORTED-FILE ASSIGN TO SORTOUT. DATA DIVISION. FD SORTED-FILE. 01 SORTED-RECORD. 05 KEY-FIELD PIC X(10). 05 DATA-FIELD PIC X(70). PROCEDURE DIVISION. MAIN. OPEN INPUT SORTED-FILE PERFORM UNTIL no-more READ SORTED-FILE AT END SET no-more TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE SORTED-FILE STOP RUN.

External Sort vs COBOL SORT Statement

In-program sorting uses the COBOL SORT statement with a sort work file (SD), RELEASE in an INPUT PROCEDURE, and RETURN in an OUTPUT PROCEDURE. You control filtering and transformation in COBOL. External sort uses the system utility: you define keys and options in control cards; the utility does the sort. The table below summarizes the main differences.

In-program SORT vs external sort
AspectIn-program (COBOL SORT)External (e.g. DFSORT)
Where it runsInside the COBOL program (SORT statement)Separate step in the job (e.g. DFSORT)
Input/outputUSING/GIVING or INPUT/OUTPUT PROCEDURESORTIN/SORTOUT (and work files) in JCL
Filtering/transformINPUT/OUTPUT PROCEDURE in COBOLINCLUDE/OMIT, INREC, OUTREC in control cards
Typical useModerate size; need COBOL logic during sortVery large sorts; use utility features

When to Use External Sort

  • Very large volumes: the system sort is optimized for big data sets and work files.
  • You need utility features: INCLUDE/OMIT, SUM (collapse duplicates), INREC/OUTREC, multiple OUTFILs.
  • Shop standard: many sites run all sorts in a single sort step for consistency and tuning.
  • Merge only: merging several pre-sorted files is straightforward in JCL with the merge option.

When to Use In-Program SORT

  • You need INPUT PROCEDURE or OUTPUT PROCEDURE to filter or transform records in COBOL.
  • Moderate size and you want all logic in one program.
  • You are not in a JCL environment (e.g. some non-mainframe COBOL) where an external sort step is not available.

Step-by-Step: Using an Externally Sorted File in COBOL

  • In JCL, define a sort step: input (e.g. SORTIN), output (SORTOUT), work files (SORTWK01, etc.), and SORT FIELDS= (and any INCLUDE/OMIT, etc.).
  • In the next step, assign the sort output (e.g. SORTOUT) to your COBOL program.
  • In COBOL, define the file as a normal sequential file (FD) with a record layout matching the sort output.
  • OPEN INPUT, READ in a loop, process each record (they will be in sort order), then CLOSE.

Best Practices

  • Match the record layout in COBOL to the sort output (positions, lengths, keys) so the program sees the same format the utility produced.
  • Document in the program that the input (or output) file is produced or consumed by an external sort step.
  • If the sort step uses INREC/OUTREC, ensure your COBOL layout matches the final record format.

Test Your Knowledge

1. How does a COBOL program typically use an external sort?

  • CALL the sort utility from PROCEDURE DIVISION
  • Read or write a file that is sorted by a separate JCL step
  • Use SORT-MERGE in the FILE SECTION
  • Link with the sort library at compile time

2. When is external sort often preferred over in-program SORT?

  • When you need INPUT PROCEDURE
  • When the sort is very large or you need utility features (SUM, INCLUDE)
  • When you have no JCL
  • When you need OUTPUT PROCEDURE

Related Concepts

Related Pages