When a report needs every record in key order before JOB logic runs—not just ordered spool lines on paper—you reach for external sort. In Easytrieve that means the SORT activity: a processing phase declared in your program that reads a sequentially processable input file, applies USING keys, optionally prescreens rows through a BEFORE procedure with SELECT, and writes a TO output file whose layout matches input unless lengths change. On z/OS batch, Broadcom interfaces to the installation sort program—DFSORT, Syncsort, or another licensed product—through E15 and E35 exits rather than reimplementing merge algorithms inside the report generator. External sort also describes the common pattern of running DFSORT in JCL before the Easytrieve step, producing a pre-sorted extract with no SORT statement in source at all. Beginners confuse this with REPORT SEQUENCE; external sort rewrites files for the whole job stream while SEQUENCE only reorders what one report PRINT spooled. This page teaches SORT syntax, TO and WORK files, SIZE estimation, BEFORE filtering, chaining into JOB, JCL preprocessing, installation sort tuning pointers, and operational pitfalls when input and output datasets collide.
1234567891011FILE PERSNL FB(150 1800) REGION 1 2 A BRANCH 4 4 A EMP-NAME 10 20 A PAY-NET 50 5 P 2 FILE SORTWRK FB(150 1800) VIRTUAL COPY PERSNL SORT PERSNL TO SORTWRK USING (REGION, BRANCH, PAY-NET D) SIZE 50000 BEFORE FILTER-HIGH-PAY
SORT names the input file PERSNL and output SORTWRK. USING lists composite keys major to minor; D after PAY-NET sorts pay descending within each region-branch group. SIZE hints record count to the sort utility when input was not produced by a prior Easytrieve activity— underestimation can cause sort failure or extra passes; overestimation wastes work space. BEFORE FILTER-HIGH-PAY names a procedure that must follow the SORT statement in source.
Easytrieve is a report generator that calls sort—it is not a replacement for DFSORT. When SORT executes, the product prepares control information from USING keys and FILE definitions, then invokes the site sort with standard exits. Your operations team tunes SORTWK DD allocations, SORTMAX, FASTSRT, and similar parameters in the sort product manual, not in Easytrieve Language Reference. Equal-key ordering among duplicates follows installation sort rules; Easytrieve retains all duplicate keys unless BEFORE excludes rows. Tie-breaker keys in USING make order deterministic when business rules require stable sequencing among equal pay amounts.
12345678SORT PERSNL TO SORTWRK USING (REGION, BRANCH) BEFORE SCREEN-REC SCREEN-REC. PROC IF PAY-NET LT 500 EXIT END-IF SELECT END-PROC
Easytrieve calls SCREEN-REC for each input record before it enters the sort utility. SELECT marks the record for inclusion. EXIT without SELECT drops the row from sort output—equivalent to filtering before external sort runs. SELECT twice on the same pass still writes one copy. SELECT followed by STOP on the same pass excludes the record. Invalid in sort procedures: GET, PRINT, REPORT, WRITE, and other statements Broadcom lists as forbidden because they fight the prescreen contract—only the current input record is in scope.
| Pattern | Typical FILE declaration | Notes |
|---|---|---|
| Scratch work file | FILE SORTWRK VIRTUAL COPY input | Discarded at job end unless RETAIN; no JCL DD required |
| Permanent reorder | FILE OUTFILE FB(...) with JCL DD | Downstream jobs consume sorted dataset |
| In-place sort | SORT FILEA TO FILEA | Risky; verify backup and consumer expectations |
1234567SORT PERSNL TO SORTWRK USING (REGION, BRANCH, EMP-NAME) JOB INPUT SORTWRK NAME PAYROLL-RPT IF FIRST-DUP REGION BRANCH * count duplicate groups after external sort END-IF PRINT RPT1
Activities execute in source order unless PROGRAM EXECUTE redirects flow. JOB INPUT SORTWRK is explicit; if omitted, default INPUT may pick SORTWRK as TO of the immediately preceding SORT. Multiple SORT activities in one program reset which file JOB defaults to—document intent when maintainers add another SORT later. REPORT definitions may still use SEQUENCE for a different print order even when JOB reads sorted input—use sparingly to avoid double sort cost.
1234567//SORTSTEP EXEC PGM=SORT //SORTIN DD DSN=PROD.PAYROLL.EXTRACT,DISP=SHR //SORTOUT DD DSN=&&SORTED,UNIT=SYSDA,SPACE=(CYL,(50,10)), // DISP=(NEW,PASS),DCB=(RECFM=FB,LRECL=150) //SYSIN DD * SORT FIELDS=(1,2,A,4,4,A,10,20,A),FORMAT=BI /*
The Easytrieve step reads &&SORTED with no SORT activity in source. This is external sort in the job stream sense—DFSORT owns tuning and work data sets. Choose JCL sort when the same extract feeds Easytrieve, COBOL, and a warehouse loader; choose in-program SORT when BEFORE SELECT logic is tightly coupled to Easytrieve fields and changes with every compile. Hybrid jobs DFSORT for coarse order and Easytrieve SORT for a smaller filtered subset are rare but valid when volumes differ wildly.
| Parameter | Purpose |
|---|---|
| USING | Sort keys; D suffix for descending on that key |
| TO | Output file receiving sorted records |
| SIZE | Estimated record count for sort work space |
| WORK | Alternate work file for sort passes |
| BEFORE | Prescreen procedure name requiring SELECT |
| COMMIT | Checkpoint-related processing per release docs |
Broadcom documents BEFORE for prescreening; there is no AFTER parameter on SORT—post-sort logic belongs in the following JOB or in report procedures. Variable-length input sorted to maximum defined length is a special case: verify downstream COPY and spool expectations when LRECL changes.
| Scenario | Recommended path |
|---|---|
| Multiple reports need same key order | One SORT TO file, JOB reads sorted input |
| JOB uses FIRST-DUP / LAST-DUP on adjacent records | External SORT before compare logic |
| Downstream job step needs sorted permanent file | SORT TO cataloged DS or JCL DFSORT |
| Only one report needs order, file order irrelevant | Internal SEQUENCE may suffice |
| Need INREC/OUTREC/SUM in sort | JCL DFSORT or ICETOOL, not Easytrieve SORT alone |
Picture a library with thousands of books scattered on carts. External sort is hiring the library's official sorting machine—the big one in the basement that every department shares—to rebuild entire shelves in call-number order before anyone reads. You push cartloads through the machine; it outputs new carts in perfect order. Easytrieve's SORT activity is your department asking that machine to run during your program. JCL DFSORT is the same machine running before your department arrives, leaving pre-sorted carts at the door. Internal SEQUENCE, by contrast, is only sorting the few books you pulled for today's story hour. When the whole library needs order for many teams, use the basement machine once. When only your story-hour pile needs order, sorting just that pile is enough.
1. On z/OS batch, Easytrieve SORT activity invokes:
2. SORT activity writes output to:
3. BEFORE proc-name on SORT requires you to:
4. JCL DFSORT before Easytrieve with no SORT activity is:
5. JOB omits INPUT after SORT when: