MainframeMaster

JCL Tutorial

The SORT Utility in JCL

Progress0 of 0 lessons

Introduction to DFSORT

DFSORT (Data Facility Sort) is a high-performance sorting, merging, copying, and data manipulation utility for IBM mainframes. It is an essential tool for batch processing in the mainframe environment, allowing you to efficiently process large volumes of data. DFSORT is also known as ICEMAN, its program name.

Key Capabilities

  • Sort data in ascending or descending order
  • Merge multiple sorted files
  • Copy and selectively include/exclude records
  • Reformat and transform data
  • Produce summary reports with statistical functions
  • Handle various record formats and data types

When to Use DFSORT

  • Preparing data for report generation
  • Reorganizing files for better performance
  • Combining data from multiple sources
  • Data extraction and transformation
  • Data validation and cleansing
  • Creating extract files for further processing

Basic SORT JCL Example

Here's a simple example of JCL to sort a sequential file by a character field:

jcl
1
2
3
4
5
6
7
8
9
10
11
12
//SORTJOB JOB (ACCT),'SORT EXAMPLE',CLASS=A,MSGCLASS=X //* //STEP010 EXEC PGM=SORT //SYSOUT DD SYSOUT=* //SORTIN DD DSN=MY.INPUT.FILE,DISP=SHR //SORTOUT DD DSN=MY.SORTED.FILE, // DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,5),RLSE), // DCB=(RECFM=FB,LRECL=80,BLKSIZE=27920) //SYSIN DD * SORT FIELDS=(1,10,CH,A) /*

This example sorts the records in MY.INPUT.FILE based on a character (CH) field starting at position 1 for a length of 10 bytes in ascending (A) order. The sorted output is written to MY.SORTED.FILE.

Required DD Statements

DFSORT requires several DD statements to function properly:

DD NameDescriptionRequired?
SYSOUTMessages and statistics produced by DFSORTYes
SORTINInput dataset (for SORT operations)Yes (for SORT)
SORTOUTOutput dataset where sorted data is writtenYes (unless using OUT DD)
SYSINControl statements defining the sort operationYes
SORTWKnnWork files for sorting (nn is 01-32)Optional (dynamically allocated)
SORTINnnInput files for MERGE operations (nn is 01-16)Yes (for MERGE)

SORT Control Statement

The SORT control statement specifies the fields to sort on and the order of the sort:

plaintext
1
2
3
4
5
6
7
SORT FIELDS=(start,length,format,order,...) Where: - start = Starting position of the field (first byte is position 1) - length = Length of the field in bytes - format = Data format (CH, BI, FI, PD, ZD, etc.) - order = Sort order (A for ascending, D for descending)

Common Field Formats

FormatDescriptionExample
CHCharacterNames, addresses, alphanumeric data
BIBinaryFullword/halfword binary fields
FIFixed-pointSigned binary numbers
PDPacked decimalCOMP-3 fields in COBOL
ZDZoned decimalDISPLAY numeric fields in COBOL
ACASCII characterASCII text (rather than EBCDIC)

Advanced SORT Examples

Multiple Sort Fields

jcl
1
2
3
//SYSIN DD * SORT FIELDS=(10,5,CH,A,25,4,PD,D,5,3,ZD,A) /*

This example sorts records by three fields:
1. Characters in positions 10-14 (ascending)
2. Packed decimal in positions 25-28 (descending)
3. Zoned decimal in positions 5-7 (ascending)

Including Only Specific Records

jcl
1
2
3
4
5
//SYSIN DD * SORT FIELDS=(1,10,CH,A) INCLUDE COND=(15,3,CH,EQ,C'ABC',AND, 25,2,BI,GT,X'0005') /*

This example sorts records but only includes those where positions 15-17 contain 'ABC' AND the binary value in positions 25-26 is greater than 5.

Reformatting Records with OUTREC

jcl
1
2
3
4
5
6
7
8
9
//SYSIN DD * SORT FIELDS=(1,10,CH,A) OUTREC FIELDS=(1,20, /* First 20 bytes */ C'CUSTOMER: ', /* Literal */ 21,15, /* Next 15 bytes */ 45,10, /* 10 bytes from pos 45 */ X'40', /* Hex constant (space) */ 55,8) /* Last 8 bytes */ /*

This example sorts records and then reformats the output by combining selected fields and constants. The OUTREC statement creates a new record layout for the output file.

Merging Pre-sorted Files

jcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//MERGEJOB JOB (ACCT),'MERGE EXAMPLE',CLASS=A,MSGCLASS=X //* //STEP010 EXEC PGM=SORT //SYSOUT DD SYSOUT=* //SORTIN01 DD DSN=SORTED.FILE.ONE,DISP=SHR //SORTIN02 DD DSN=SORTED.FILE.TWO,DISP=SHR //SORTIN03 DD DSN=SORTED.FILE.THREE,DISP=SHR //SORTOUT DD DSN=MERGED.OUTPUT.FILE, // DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(15,5),RLSE), // DCB=(RECFM=FB,LRECL=80,BLKSIZE=27920) //SYSIN DD * MERGE FIELDS=(1,10,CH,A,25,5,PD,A) /*

This example merges three pre-sorted files into a single output file. The MERGE control statement specifies the sort fields, which must match how the input files were originally sorted.

DFSORT Functions and Features

INCLUDE/OMIT

Filter records based on conditions:

plaintext
1
2
INCLUDE COND=(5,3,CH,EQ,C'YES') OMIT COND=(1,1,BI,EQ,X'FF')

INCLUDE keeps matching records, OMIT removes them.

INREC/OUTREC

Reformat records:

plaintext
1
INREC FIELDS=(1,10,20,15,C'FIXED TEXT')

INREC reformats before sorting, OUTREC after sorting.

SUM

Summarize duplicate records:

plaintext
1
2
SORT FIELDS=(1,10,CH,A) SUM FIELDS=(25,4,ZD,30,6,PD)

Adds specified fields when records have identical keys.

OPTION

Control DFSORT processing:

plaintext
1
2
OPTION DYNALLOC=SYSDA,MAINSIZE=MAX, EQUALS=YES,FILSZ=E50000

Sets memory usage, work file allocation, etc.

DFSORT Best Practices

Performance Tips

  • Specify FILSZ to provide record count estimate
  • Use DYNALLOC for automatic work file allocation
  • Apply INCLUDE/OMIT early to reduce data volume
  • Use INREC to reduce record size before sorting
  • Allocate sufficient REGION parameter
  • Avoid unnecessary reformatting and conversions

Troubleshooting

  • Use OPTION NOBLKSET for compatibility issues
  • Check SYSOUT for error messages and statistics
  • Monitor return codes (0=success, 16=failure)
  • Verify control statement syntax carefully
  • Use STOPAFT=n to test with sample records
  • Ensure sufficient SORTWK space for large sorts

Frequently Asked Questions

What is the SORT utility in JCL?

The SORT utility (DFSORT or ICEMAN) is a program that allows you to sort, merge, copy, or transform data in mainframe environments. It can handle various data sources including sequential files, VSAM files, and PDS members. SORT is highly optimized for performance and can process large volumes of data efficiently.

What are the required DD statements for SORT?

The required DD statements for SORT are:

  • SYSOUT - For messages and statistics
  • SORTWK01-nn - Work files (usually allocated automatically)
  • SORTIN - Input data
  • SORTOUT - Output data

Additional optional DD statements include SYSIN (for control statements if not inline) and SORTINnn/SORTOUTnn for merge operations.

How do I specify sort fields in DFSORT?

Sort fields are specified using the SORT control statement with the FIELDS parameter:

plaintext
1
SORT FIELDS=(start,length,format,order,...)

Where:

  • start is the starting position (1-based)
  • length is the field length
  • format is the data type (CH for character, BI for binary, etc.)
  • order is A for ascending or D for descending

Multiple fields can be specified within the parentheses, separated by commas.

What is the difference between SORT and MERGE in DFSORT?

SORT reads all input data from a single source (SORTIN) and sorts it according to specified keys. MERGE combines multiple pre-sorted input sources (SORTIN01, SORTIN02, etc.) into a single sorted output. The key difference is that MERGE requires each input to already be sorted in the same order as the merge operation.

How can I include or exclude records in DFSORT?

You can include or exclude records using the INCLUDE and OMIT statements:

plaintext
1
2
INCLUDE COND=(logic_expression) OMIT COND=(logic_expression)

Logic expressions can use relational operators (EQ, NE, GT, LT, GE, LE) and logical operators (AND, OR). For example, INCLUDE COND=(5,4,CH,EQ,C'ABCD') includes only records where positions 5-8 contain "ABCD".

Can DFSORT modify data during processing?

Yes, DFSORT can modify data during processing using the OUTREC or INREC statements. These allow you to:

  • Reformat records by rearranging fields
  • Remove fields
  • Add constants and literals
  • Pad with spaces or zeros
  • Perform arithmetic operations
  • Convert data types

INREC reformats records before sorting, while OUTREC reformats after sorting but before output.

How can I improve SORT performance in JCL?

To improve SORT performance:

  1. Allocate sufficient SORTWK space
  2. Use REGION parameter to provide adequate memory
  3. Specify DYNALLOC for automatic work space allocation
  4. Use FILSZ to provide the record count
  5. Minimize data read by using INCLUDE/OMIT early
  6. Use INREC to reduce record size before sorting
  7. Consider SORTINnn for pre-sorted data whenever possible