JCLintermediate
Sorting Data with DFSORT
Use DFSORT control statements to sort, merge, copy, and reformat datasets entirely at the JCL/utility level without writing a COBOL program.
DFSORT sorts, filters, copies and reformats datasets from JCL, with no program to write or compile.
DFSORT is the industrial mail-sorting machine at a postal depot — instead of a clerk manually reading and re-stacking every letter by hand (a custom program), the machine reads a barcode (the sort key), routes and reorders thousands of pieces per minute, and can even discard mail that doesn't meet criteria on the way through.
Key Concepts
1
A great deal of batch logic that looks like it needs a program does not.
2
//SORTIN DD DSN=PROD.CUSTOMER.FILE,DISP=SHR
//SORTOUT DD DSN=PROD.CUSTOMER.SORTED,DISP=(NEW,CATLG,DELETE)
//SYSIN DD *
SORT FIELDS=(1,9,CH,A,20,8,PD,D)
INCLUDE COND=(30,2,CH,EQ,C'IN')
/*3
Reading a field specification. (1,9,CH,A) is position 1, length 9, character format, ascending.
(1,9,CH,A)
4
position, length, format, direction
CH character PD packed decimal (COMP-3)
ZD zoned decimal BI binary5
That example sorts by a 9-byte character key ascending, then by a packed-decimal field descending within
it.
6
INCLUDE and OMIT filter. INCLUDE COND= keeps only matching records; OMIT COND= drops them. Doing
this here rather than in a downstream program means the program never sees the records at all.
INCLUDEOMITINCLUDE COND=OMIT COND=
7
INREC and OUTREC reformat — reorder fields, insert constants, drop columns:
INRECOUTREC
8
OUTREC FIELDS=(1,9, 30,2, 10,8,PD,TO=ZD)9
That builds a new record from selected pieces of the old one, converting a packed field to zoned on the
way. No program, no copybook, no compile.
10
COPY instead of SORT does the reformatting or filtering without ordering anything, which is faster
when order does not matter.
COPYSORT
11
ICETOOL sits on top and handles the reporting-shaped tasks:
12
SELECT pick first, last, or duplicates
COUNT how many records match
STATS minimum, maximum, average of a field
SPLICE join records from two files on a key13
Deduplicating a file is one ICETOOL operator. As COBOL it is a program, a copybook, a compile and a test.
14
The judgement being tested. DFSORT is often right even when a program could do it.
15
Sorting before the program opens the file is nearly always faster than sorting inside it, because DFSORT is
built for exactly that and uses memory and disk accordingly. Filtering in the SORT step can remove an entire
downstream validation pass.
16
The general rule: if the logic is select, sort, filter or reformat, do it here. If it needs real business
logic, write the program — and let DFSORT hand it a smaller, sorted file.
17
What the interviewer is probing.1. "Why use DFSORT rather than sorting in a program?" Probing: the economics. Stalls:
"Convenience." Moves up: it is highly optimised and uses far less CPU, which on a metered
mainframe is a direct cost saving.
18
2. "What do INCLUDE and OMIT do?" Probing: the capability. Stalls: "Nothing." Moves up:
filter records before sorting, which often means the sort is just a filter and the data never needs
a program at all.
19
3. "What is OUTREC for?" Probing: the breadth. Stalls: "Output." Moves up: reformatting
records on the way out — rearranging, padding and converting fields, which replaces a lot of simple
COBOL.
20
4. "What does SUM FIELDS=NONE do?" Probing: the idiom. Stalls: "Adds up." Moves up: drops
duplicate keys, which is the standard way to deduplicate a file.