Easytrieve SORT Statement

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.

Progress0 of 0 lessons

Statement Format

text
1
2
3
4
5
6
7
SORT 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 as an Activity

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:

text
1
2
3
4
5
6
7
8
9
10
11
12
FILE 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.

USING Key Fields Explained

Sort key rules
RuleDetail
DEFINE requiredEach key must be defined in Library on the input file
Major-to-minorFirst key dominates; later keys break ties within prior key value
Length limitKeys under 256 bytes; variable-length keys invalid
Invalid typesVarying length, K, and M fields cannot be sort keys
D suffixDescending for that key only; other keys still default ascending unless D
Installation capMaximum 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.

Ascending Versus Descending

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.

BEFORE Prescreen Procedures

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.

text
1
2
3
4
5
6
7
8
9
10
SORT 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 and WORK Parameters

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.

Virtual Output Files

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.

SORT Versus DFSORT Standalone

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.

EXECUTE and PROGRAM Interaction

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.

Common SORT Mistakes

  • Key field OFFSET/LENGTH mismatch with FILE layout—garbled sequence.
  • Using variable-length field as key—compile or sort error.
  • BEFORE proc without SELECT on kept records—empty sort file.
  • Forgetting JOB INPUT after SORT when multiple files exist in Library—wrong default input.
  • CONTROL break fields not matching USING order—reports look unsorted.
  • Expecting SORT inside JOB body—it is its own activity block.

Testing SORT

  1. DISPLAY first and last keys on sorted file after JOB—verify sequence.
  2. Compare record count input vs output with and without BEFORE filtering.
  3. Test one key with D suffix—confirm highest-first within groups.
  4. Run with empty input file—confirm graceful EOJ and FINISH if coded on following JOB.
  5. Align REPORT CONTROL with USING keys on multi-break report.

Explain It Like I'm Five

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.

Exercises

  1. Write SORT with two ascending keys and NAME parameter.
  2. Add D to the second key only; explain expected order.
  3. Write BEFORE proc that SELECTs REGION = 20 only.
  4. Chain SORT to JOB INPUT explicitly and state default if INPUT omitted.
  5. List three field types that cannot be sort keys.

Quiz

Test Your Knowledge

1. SORT defines and initiates:

  • A SORT activity that sequences a file by keys
  • A REPORT TITLE
  • SCREEN layout
  • PARM options

2. USING lists sort keys in:

  • Major-to-minor order
  • Random order
  • Descending only
  • Alphabetic label order

3. D after a key field means:

  • Descending sort on that key
  • Delete file
  • DISPLAY debug
  • DEFER open

4. SORT BEFORE procedure requires:

  • SELECT for each record to include in sort output
  • STOP EXECUTE always
  • REPORT LINE
  • SQL CONNECT

5. If JOB omits INPUT after a SORT activity:

  • Default input is the SORT output file
  • No file reads
  • SCREEN runs
  • Compile fails
Published
Read time17 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 SORT StatementSources: Broadcom Easytrieve 11.6 SORT Statement, SORT Activities, Sorting FilesApplies to: Easytrieve SORT activity