Easytrieve SORT Statement

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.

Progress0 of 0 lessons

Full Statement Shape

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]

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.

SORT as an Activity Block

SORT versus other activities
ActivityRoleTypical output
SORTReorder full file by keysTO work or permanent file
JOBRead records; PRINT; PUT; logicReports and extracts
SCREENOnline terminal mapsScreen I/O
PROGRAMEXECUTE named activitiesConditional 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.

Complete Sort-Then-Report Example

text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FILE 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 and TO File Rules

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.

USING — Quick Key Rules

  • Keys must be defined in Library on the input file layout.
  • Major-to-minor: first key dominates sort groups.
  • Maximum key length under 256 bytes; no varying-length keys.
  • Invalid key types include V varying, K, and M per Broadcom.
  • D suffix applies only to the key immediately before it.
  • Installation caps how many keys one SORT may list.

Deep treatment of key choice lives on the Sort keys and Multi-key sorting pages in this chapter.

BEFORE Prescreen and SELECT

text
1
2
3
4
5
6
7
SORT 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, WORK, and COMMIT

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.

Chaining SORT to JOB

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 Versus REPORT SEQUENCE

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.

Common SORT Statement Mistakes

  • Coding SORT inside JOB body instead of as its own activity.
  • Key field offset mismatch with FILE layout—garbled order.
  • BEFORE proc without SELECT on kept rows—empty TO file.
  • USING order unlike REPORT CONTROL—breaks look random.
  • Variable-length field as key—compile or sort failure.
  • Forgetting JOB INPUT when multiple files exist in Library.

Testing Your SORT Statement

  1. Compare input and output record counts without BEFORE filtering.
  2. DISPLAY first and last key values on sorted file in JOB.
  3. Verify CONTROL report breaks align with USING keys.
  4. Test BEFORE SELECT with a known subset count.
  5. Run empty input—confirm clean job end without ABEND.

Explain It Like I'm Five

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.

Exercises

  1. Write SORT with two ascending keys and NAME parameter.
  2. Add VIRTUAL COPY layout for TO file.
  3. Chain explicit JOB INPUT to SORT output.
  4. Write BEFORE proc that SELECTs one region only.
  5. List when you would choose SEQUENCE instead of SORT.

Quiz

Test Your Knowledge

1. SORT in Easytrieve is:

  • Its own activity block, not a verb inside JOB
  • A synonym for PRINT
  • Only valid in SCREEN
  • A JCL DD name

2. SORT input-file TO sorted-file means:

  • Read input sequentially and write reordered output
  • Open SQL cursor
  • Format TITLE lines
  • Delete VSAM cluster

3. USING lists sort keys in:

  • Major-to-minor order
  • Random order
  • Compile order only
  • JCL order

4. After SORT, the next JOB without INPUT typically reads:

  • The SORT TO output file
  • Always SYSPRINT
  • The original input again
  • Nothing

5. BEFORE on SORT requires SELECT when you want to:

  • Keep a record in sort output
  • Open printer
  • Skip DEFINE
  • Compile macro
Published
Read time18 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 SORT activity statementSources: Broadcom Easytrieve 11.6 Language Reference SORT, Activity ProcessingApplies to: Easytrieve SORT activity and sort-then-JOB workflows