Payroll extracts arrive unsorted. Merge programs expect department order. Report CONTROL breaks assume keys are sequenced. The SORT statement defines and initiates a SORT activity—a separate processing phase outside JOB—that sequences an input file using installation sort utilities on z/OS. SORT is not an inline verb inside JOB logic like ADD or MOVE; it is an activity block comparable to JOB itself, usually followed by JOB INPUT of the sorted work file. Broadcom copies input layout to output by default: same record length and field positions, only order changes. Optional BEFORE procedures prescreen or modify records before they enter the sort, requiring SELECT for each retained row. Optional SIZE and WORK tune sort resources. This page explains SORT syntax, USING key rules, descending D suffix, virtual output files, COMMIT options, chaining SORT to JOB, E15/E35 exit behavior at a high level, and mistakes beginners make when keys do not match DEFINE offsets.
1234567SORT input-file-name TO sorted-file-name + USING (sort-key-field-name [D] ...) + [COMMIT ([ACTIVITY|NOACTIVITY] [TERMINAL|NOTERMINAL])] + [SIZE record-count] + [WORK number-of-work-data-sets] + [BEFORE proc-name] + [NAME sort-name]
Input-file-name must appear on a FILE statement in Library and support sequential processing. Sorted-file-name is typically a VIRTUAL work file defined with COPY of the input layout so record pictures align. USING lists one or more key fields in major-to-minor order. NAME sort-name documents the activity for EXECUTE and program maps. COMMIT mirrors JOB and SCREEN activity commit semantics for recoverable resources when your online or SQL environment requires it.
SORT sits in the Activity section alongside JOB, SCREEN, and PROGRAM. When no explicit PROGRAM exists, implied PROGRAM executes JOB and SORT activities in source order until SCREEN. A common batch skeleton sorts once, then processes sorted output:
123456789101112FILE PERSNL FB(150 1800) %PERSNL FILE SORTWRK FB(150 1800) VIRTUAL COPY PERSNL SORT PERSNL TO SORTWRK USING (REGION, BRANCH) NAME MYSORT JOB INPUT SORTWRK NAME MYPROG PRINT REPORT1 REPORT REPORT1 LINE REGION BRANCH EMPNAME
SORTWRK receives all PERSNL records sorted by REGION major and BRANCH minor. JOB reads SORTWRK automatically because INPUT names it explicitly; if INPUT were omitted, Broadcom default still picks the prior SORT output when present. REPORT definitions follow JOB as usual.
| Rule | Detail |
|---|---|
| DEFINE required | Each key must be defined in Library on the input file |
| Major-to-minor | First key dominates; later keys break ties within prior key value |
| Length limit | Keys under 256 bytes; variable-length keys invalid |
| Invalid types | Varying length, K, and M fields cannot be sort keys |
| D suffix | Descending for that key only; other keys still default ascending unless D |
| Installation cap | Maximum number of keys depends on site sort program |
Example hierarchy: REGION major sorts states or divisions; BRANCH minor sorts sites within region; DEPT more minor sorts departments within branch. Misordered keys produce logically sorted output that still fails CONTROL breaks in reports—always match SORT keys to REPORT CONTROL fields.
Default sort order is ascending for every key. Append D immediately after a field name for descending on that key alone: USING (PAY-GROSS D, EMP#) sorts highest pay first within each employee number tie (unusual tie key) or more realistically USING (REGION, PAY-GROSS D) for top earners by region. Mixing D on only some keys is common; do not assume D applies to all following keys unless each has D.
When you need subset sorting or light transformation before keys are applied, code BEFORE proc-name. The procedure immediately follows the SORT statement in source. Easytrieve feeds each input record to the procedure; execute SELECT to include a record in sort output. Without SELECT, the record is skipped. STOP in the procedure ends prescreening and triggers the sort program—useful to cap how many records enter sort when testing. Invalid statements in sort procedures include GET and PUT per PROC restrictions during SORT/REPORT processing.
12345678910SORT PERSNL TO SORTOUT USING (PAY-GROSS D) + NAME MYSORT BEFORE SORT1-PROC SORT1-PROC. PROC IF PERSNL:RECORD-COUNT GT 50 STOP ELSE SELECT END-IF END-PROC
Only the first fifty selected records sort—STOP simulates end-of-file on prescreening. Adjust logic for production caps carefully; STOP after SELECT on the same pass drops that record.
SIZE record-count hints expected record volume to the sort utility for workspace planning. WORK number-of-work-data-sets controls sort work data set allocation on z/OS. Values depend on installation sort defaults and DASD standards—coordinate with operations rather than guessing in development. Omit both when site defaults handle typical file sizes adequately.
SORT output is often a VIRTUAL file with COPY of input layout—no permanent dataset unless your JCL or FILE definition routes otherwise. VIRTUAL work files disappear when the activity ends, which protects DASD but requires JOB to consume sorted output in the same execution pass unless you deliberately write to a permanent file in FILE definition.
Easytrieve SORT invokes the system sort with Easytrieve record descriptors and E15/E35 style exits for BEFORE processing. Standalone DFSORT in JCL may sort faster for huge files with complex INCLUDE/OMIT—but you lose integrated SELECT in Easytrieve procedures and FIELD alignment from Library. Many shops keep Easytrieve SORT for report-prep files under a few million records and escalate to DFSORT for enterprise archives.
Explicit PROGRAM activities EXECUTE named SORT activities when batch flow needs conditional sorting—sort only when parameter flag set, for example. Implied PROGRAM runs every SORT in order for batch-only programs. SCREEN may EXECUTE SORT before JOB report when operator requests sorted extract online.
SORT is putting a stack of cards in order before you read them for a story. USING tells which part of each card matters first—like sorting by color then by number. D means biggest-first instead of smallest-first for that one rule. BEFORE is a helper who looks at each card and only hands over cards that get a SELECT sticker. The new pile is the TO file, and the next JOB reads that neat pile instead of the messy original.
1. SORT defines and initiates:
2. USING lists sort keys in:
3. D after a key field means:
4. SORT BEFORE procedure requires:
5. If JOB omits INPUT after a SORT activity: