Mainframe SORT – Reference & Guide
Introduction
SORT utilities (DFSORT or SYNCSORT) are powerful tools on IBM Mainframes used for:
- Rearranging data in sequential/VSAM datasets
- Selecting/omitting records
- Summarizing, merging, joining datasets
- Reformatting records
They are used in JCL steps for data manipulation without writing COBOL/PL1 programs.
Basic JCL Structure
//JOBNAME JOB (ACCT),'SORT EXAMPLE'
//STEP01 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=[Link],DISP=SHR
//SORTOUT DD DSN=[Link],DISP=NEW,CATLG,UNIT=SYSDA,SPACE=(CYL,(5,5))
//SYSIN DD *
SORT FIELDS=(1,10,CH,A)
/*
Key DD Statements:
- SORTIN → Input dataset
- SORTOUT → Output dataset
- SYSIN → Control statements (defines sort/merge/filter logic)
- SYSOUT → Messages and statistics
Common Control Statements
1. Sorting Records:
SORT FIELDS=(1,10,CH,A)
2. Merging Datasets:
MERGE FIELDS=(15,5,CH,A)
3. Copying Records (No Sort):
SORT FIELDS=COPY
4. Filtering Records:
INCLUDE COND=(20,3,CH,EQ,C'ABC')
OMIT COND=(50,2,CH,EQ,C'99')
5. Summarizing Data:
SUM FIELDS=(30,5,BI)
6. Reformatting Output:
OUTREC FIELDS=(1,10,15,5,30,5)
7. Removing Duplicates:
SORT FIELDS=(1,10,CH,A)
SUM FIELDS=NONE
Advanced Features
- JOINKEYS → Join two datasets on a common key (like SQL JOIN)
- INREC / OUTREC → Modify record layout before/after sort
- WHEN=INIT / WHEN=GROUP → Grouping and sequencing
- ICETOOL → Reporting, counts, min/max, data analytics
Example – Remove Duplicates and Keep Highest Value
//SYSIN DD *
SORT FIELDS=(1,10,CH,A,11,5,BI,D)
SUM FIELDS=NONE
/*
Sort by key (1–10), then numeric field (11–15) descending.
Keeps only the highest value per key.
Best Practices
- Always check SYSOUT for sort statistics
- Use DFSORT symbols for readability
- For very large datasets, tune memory (REGION) and SORTWKxx datasets
References
- IBM DFSORT Guide
- SYNCSORT User Manual
- JCL Reference for DD statements