SORT FIELDS
Puts the records of a file in order, by a field you pick.
Also written SORT, DFSORT SORT
Why it matters. Banks sort payments by account before they post them. This one line does it for any file.
See it
Each key has four parts: start, length, format, order.
Syntax
SORT FIELDS=(start,length,format,order) SORT FIELDS=(start,length,order),FORMAT=format SORT FIELDS=COPY
- start
- The first byte of the field, counting from 1.
- length
- How many bytes the field is.
- format
- How to compare it: CH text, ZD zoned decimal, PD packed decimal, BI binary.
- order
- A is lowest first. D is highest first.
- COPY
- Do not sort. Copy the records in their order.
- EQUALS
- Option that keeps records with equal keys in their input order.
Try it SORT FIELDS
- 1
You're in the SYSIN lines of job SORTTXN. Write the statement that sorts the payments by the account number: 8 characters of text, starting in column 10, lowest first.
SYSIN SORT FIELDS=(10,8,CH,A) - 2
Submit the job. It is the member SORTTXN in your DEV JCL library.
SUBMIT 'ZB0001.DEV.JCL(SORTTXN)' - 3
Read the output of the job.
OUTPUT SORTTXN
Remember
- SORT FIELDS=(start,length,format,order).
- CH is text. A is lowest first, D is highest first.
- SORT FIELDS=COPY means keep the order and just copy.
The usual mistake
SORT FIELDS=(10,8,A)
The format is missing. Each field needs start, length, format and order. If you leave the format out of the field, you must give it once for the whole statement, as FORMAT=CH. Without either, DFSORT cannot tell how to compare the field.
SORT FIELDS=(10,8,CH,A)
On real z/OS
Real DFSORT names the product and release in ICE000I and prints more lines (such as ICE250I and ICE193I). MF/OS runs a subset of DFSORT and writes its own product name there. Numeric formats read digits from text here, not real packed or binary bytes. Records with equal keys keep their input order here, as with EQUALS on z/OS.
Where can I practise SORT FIELDS?
Right here, at ZedBank. You take a junior job at a simulated bank and use SORT FIELDS in real work, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- DFSORTThe standard sort utility, used far beyond sorting to select, merge, reformat and summarise records at speed.
- DD statementThe JCL statement that connects a name inside a program to a real dataset outside it.
- Job stepOne program execution within a job. A job is simply a sequence of steps that run in order.
- JCLJob Control Language: the language used to tell the mainframe what programs to run, in what order, and with what files.
- SYSOUTOutput sent to the spool rather than to a dataset, where you can read it after the job finishes.