A COBOL program can sort records inside the program with the SORT statement. That is useful when the program must select records before sorting or handle sorted records one at a time after sorting. For a simple production file sort, a separate JCL DFSORT step is often easier to tune and support, but internal COBOL SORT still has a place.
What does COBOL SORT do?
The COBOL SORT statement arranges records or table elements in a sequence defined by one or more keys. For file sorting, COBOL uses an SD sort-file description, receives records from an input file or input procedure, sorts them, and then writes them to an output file or passes them to an output procedure.
Simple SORT USING GIVING example
The simplest form uses USING for the input file and GIVING for the output file. COBOL opens, reads, writes, and closes those files as part of the SORT. IBM notes that the input and output files named in these phrases must not already be open when the SORT executes.
SELECT INPUT-FILE ASSIGN TO INFILE.
SELECT SORT-FILE ASSIGN TO SORTWK.
SELECT OUTPUT-FILE ASSIGN TO OUTFILE.
SD SORT-FILE.
01 SORT-REC.
05 SORT-ACCOUNT-NO PIC X(10).
05 SORT-DATE PIC X(8).
05 SORT-AMOUNT PIC S9(9)V99 COMP-3.
PROCEDURE DIVISION.
SORT SORT-FILE
ON ASCENDING KEY SORT-ACCOUNT-NO
USING INPUT-FILE
GIVING OUTPUT-FILE
GOBACK.
This form fits a direct file-to-file sort where every input record should appear in the sorted output and no custom record logic is needed inside the COBOL program.
When to use INPUT PROCEDURE
Use INPUT PROCEDURE when records must be selected, edited, or built before the sort begins. The input procedure reads or creates records and sends each accepted record to the sort file with RELEASE.
SORT SORT-FILE
ON ASCENDING KEY SORT-ACCOUNT-NO
INPUT PROCEDURE 2000-BUILD-SORT-RECS
GIVING OUTPUT-FILE.
2000-BUILD-SORT-RECS.
PERFORM UNTIL END-OF-FILE
READ INPUT-FILE
AT END
SET END-OF-FILE TO TRUE
NOT AT END
IF IN-STATUS = 'A'
MOVE INPUT-REC TO SORT-REC
RELEASE SORT-REC
END-IF
END-READ
END-PERFORM.
Use this pattern when the program must drop inactive records, change field layouts, or combine working-storage values before the sort sees the record.
When to use OUTPUT PROCEDURE
Use OUTPUT PROCEDURE when sorted records must be processed before they are written. The output procedure gets each sorted record with RETURN. IBM recommends coding RETURN ... AT END and running until the end condition is reached.
SORT SORT-FILE
ON ASCENDING KEY SORT-ACCOUNT-NO
USING INPUT-FILE
OUTPUT PROCEDURE 3000-WRITE-REPORT.
3000-WRITE-REPORT.
PERFORM UNTIL NO-MORE-SORT-RECS
RETURN SORT-FILE
AT END
SET NO-MORE-SORT-RECS TO TRUE
NOT AT END
MOVE SORT-REC TO REPORT-REC
WRITE REPORT-REC
END-RETURN
END-PERFORM.
This form fits sorted reports, grouped totals, break processing, and cases where the program must inspect the sorted stream before writing the final output.
RELEASE and RETURN in plain English
| Statement | Where it is used | Purpose |
|---|---|---|
RELEASE |
Input procedure | Sends one record to the sort file before sorting starts. |
RETURN |
Output procedure | Gets one sorted record from the sort file after sorting finishes. |
AT END |
RETURN statement |
Detects that no more sorted records are available. |
COBOL SORT vs JCL SORT
Use internal COBOL SORT when selection, transformation, or report logic belongs naturally in the program. Use JCL SORT when the requirement is only to sort, copy, include, omit, reformat, or split files. A separate DFSORT step is usually easier for operations teams to tune because sort memory, work files, and control statements are visible in JCL.
Common mistakes
Opening files before SORT USING or GIVING
Do not open the files named in USING or GIVING before the SORT. The SORT statement handles those files for that form.
Forgetting RELEASE in the input procedure
An input procedure must release records to the sort file. If no records are released, the output procedure or output file will not receive the expected data.
Forgetting RETURN AT END
An output procedure should keep returning sorted records until the AT END condition is reached. With DFSORT, failing to reach the end condition can lead to an abnormal sort termination.
Using internal SORT for every large file
Large high-volume sorts should be reviewed carefully. If the sort can be done outside the program, a JCL SORT step may be simpler to monitor and change.
Review checklist
- Define the sort file with an
SDentry. - Choose one input path:
USINGorINPUT PROCEDURE. - Choose one output path:
GIVINGorOUTPUT PROCEDURE. - Use
RELEASEfor records passed into an input procedure. - Use
RETURN ... AT ENDfor records pulled from an output procedure. - Compare internal COBOL
SORTwith a separate DFSORT step for high-volume batch work.
Related Mainframe Forum guides
For related topics, read JCL SORT examples, SORT INREC and OUTREC examples, COBOL sequential file organization, COBOL READ statement, and COBOL PERFORM statement.
External references
IBM documents the Enterprise COBOL SORT statement, coding the output procedure, and coding the input procedure.
FAQ
What is COBOL SORT used for?
COBOL SORT arranges records or table elements by one or more keys. For files, it can take input from files or an input procedure and send sorted records to files or an output procedure.
What is INPUT PROCEDURE in COBOL SORT?
INPUT PROCEDURE runs before sorting. It selects or builds records and passes them to the sort file with RELEASE.
What is OUTPUT PROCEDURE in COBOL SORT?
OUTPUT PROCEDURE runs after sorting. It gets each sorted record with RETURN and can write, total, or report the record.
Should I use COBOL SORT or JCL SORT?
Use COBOL SORT when program logic must run before or after sorting. Use JCL SORT when the task is mainly file sorting, copying, filtering, or formatting.