The SORT statement is how Easytrieve turns a messy extract into an ordered file your JOB can trust. It does not sort inside the read loop. It declares a SORT activity—a full processing phase that reads every record from an INPUT file, calls the installation sort utility with keys from USING, and writes a TO file in the new sequence. Payroll shops sort by department before control-break reports. Inventory feeds sort by warehouse then SKU. Compliance extracts sort by date then account. Beginners see SORT on a line and assume it works like COBOL inline SORT—Easytrieve SORT is closer to a mini JCL step embedded in the program. This chapter page walks the statement in program context: activity placement, INPUT and TO files, USING keys, BEFORE prescreen with SELECT, SIZE and WORK hints, VIRTUAL COPY work files, chaining to JOB INPUT, and how SORT differs from REPORT SEQUENCE. For parameter-by-parameter syntax, also see the SORT statement reference under Statements.
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]
Each piece has a job. input-file-name is the FILE you read—must support sequential processing. sorted-file-name receives output in the same record layout unless BEFORE changes lengths. USING parenthesizes keys major-to-minor. D after one key requests descending for that key only. COMMIT mirrors JOB commit semantics for recoverable online or SQL environments. SIZE tells the sort how many records to plan for when volume is not obvious. WORK controls sort work data set count on z/OS. BEFORE names a prescreen procedure. NAME documents the activity for EXECUTE and program maps.
| Activity | Role | Typical output |
|---|---|---|
| SORT | Reorder full file by keys | TO work or permanent file |
| JOB | Read records; PRINT; PUT; logic | Reports and extracts |
| SCREEN | Online terminal maps | Screen I/O |
| PROGRAM | EXECUTE named activities | Conditional flow |
Implied PROGRAM runs SORT and JOB in source order for batch programs without an explicit PROGRAM block. You may EXECUTE a named SORT only when a flag is set. SORT never replaces JOB—it prepares input JOB will read.
1234567891011121314FILE PERSNL FB(150 1800) %PERSNL FILE SORTWRK FB(150 1800) VIRTUAL COPY PERSNL SORT PERSNL TO SORTWRK USING (REGION, BRANCH, DEPT) NAME PAY-SORT JOB INPUT SORTWRK NAME PAY-JOB PRINT PAY-RPT REPORT PAY-RPT LINESIZE 132 CONTROL REGION BRANCH DEPT TITLE 01 'PERSONNEL BY REGION BRANCH DEPT' LINE 01 REGION BRANCH DEPT EMP# NAME GROSS
PERSNL is the nightly extract. SORTWRK is VIRTUAL with COPY PERSNL so field positions match without maintaining two layouts. SORT orders REGION major, BRANCH minor, DEPT more minor—all ascending here. JOB reads SORTWRK explicitly; naming INPUT avoids ambiguity when Library declares other files. REPORT CONTROL uses the same key order as USING so breaks fire when values change. Mismatch between USING and CONTROL is the top reason sorted jobs still look wrong on spool.
INPUT must be defined on FILE with sequential access—QSAM, VSAM ESDS-style sequential, VIRTUAL, and other organizations your release documents. Keys must be fixed fields from DEFINE on that file. TO is usually a separate FILE; classic guidance discourages TO equaling INPUT for VSAM or ISAM rewrite in place. VIRTUAL TO with COPY of INPUT is the beginner-safe pattern: same picture, new order, no catalog clutter. Permanent TO files need JCL DD with SPACE and DISP appropriate for retention.
Deep treatment of key choice lives on the Sort keys and Multi-key sorting pages in this chapter.
1234567SORT PERSNL TO SORTWRK USING (DEPT, EMP#) BEFORE DEPT20 DEPT20. PROC IF DEPT EQ 20 SELECT END-IF END-PROC
Every input record enters DEPT20 before the sort utility sees it. SELECT keeps department 20 rows; others drop. Invalid in sort procedures include GET, PUT, READ, and PRINT—I/O belongs elsewhere. DISPLAY to SYSPRINT is allowed for debugging counts. STOP ends prescreen early; use carefully in production. Broadcom documents no AFTER on SORT—post-sort work belongs in JOB.
SIZE record-count helps the sort allocate workspace when Easytrieve cannot infer volume from a prior activity. WORK 0 means your JCL supplies sort work DDs; WORK 1–31 requests dynamic work data sets. Values are site-specific—coordinate with operations. COMMIT ACTIVITY or NOACTIVITY pairs with TERMINAL options when online recoverable sorting matters; batch-only jobs often omit COMMIT entirely.
After SORT completes, the next JOB in source order typically consumes TO output. Explicit JOB INPUT sort-file-name NAME job-name documents intent for maintainers. If INPUT is omitted and the prior activity was SORT, default automatic input is the SORT output file. Multiple SORT or FILE declarations break that default—always name INPUT when the program is not trivial. FINISH on JOB after empty sort output should still behave cleanly when BEFORE filtered everything out.
SORT reorders the entire input file for any downstream consumer. REPORT SEQUENCE reorders only spooled report rows after PRINT for one REPORT. Use SORT when JOB logic, multiple reports, or file extracts need shared order. Use SEQUENCE when one report's print order is the only requirement and a full-file sort would waste CPU. The sorting overview compares both paths in a table.
SORT is a teacher who collects all the homework papers and stacks them in order before anyone reads them aloud. USING tells the teacher: sort by last name first, then by first name. The TO pile is the neat stack. JOB is the next class that reads only from the neat stack. BEFORE is a helper who throws away papers that fail a check unless they get a SELECT sticker.
1. SORT in Easytrieve is:
2. SORT input-file TO sorted-file means:
3. USING lists sort keys in:
4. After SORT, the next JOB without INPUT typically reads:
5. BEFORE on SORT requires SELECT when you want to: