SORT FIELDS


The SORT FIELDS statement is used to specify how records in a file should be sorted based on one or more fields. We define the starting position, length, data type, and sort order (ascending or descending) for each key field. If multiple fields are coded, the processing order is from left to right.

Syntax -

//SYSIN DD * 
    SORT FIELDS=(field1_start_pos_in_input_file, field1_length, 
	     field1_format, field1_sorting_order, .....)
/*

Syntax -

SORT FIELDS=(starting position, length, data format, A/D)
field1_start_pos_in_input_fileSpecifies the starting position of field1 to be compared
field1_lengthSpecifies the length of the field1 to be compared
field1_formatSpecifies the data type of the field1.
  • CH (Character): For alphanumeric fields.
  • ZD (Zoned Decimal): For numeric fields stored in zoned decimal format.
  • PD (Packed Decimal): For numeric fields stored in packed decimal format.
  • BI (Binary): For numeric fields stored in binary format.
A or DSpecifies the sorting order. A for ascending and D for descending.

Examples -


Scenario1 - Simple Sort on a Character Field.

//SYSIN   DD  *
  SORT FIELDS=(1,10,CH,A)
/*

This sorts the dataset based on the first 10 characters in ascending order.

Scenario2 - Sort on a Zoned Decimal Field

//SYSIN   DD  *
  SORT FIELDS=(5,4,ZD,D)
/*

This sorts the dataset based on the 4-byte zoned decimal field at position 5 in descending order.

Scenario3 - Sort on Multiple Fields

//SYSIN   DD  *
  SORT FIELDS=(1,5,CH,A,10,3,ZD,D)
/*

The first 1000 records are written to the dataset MATEPK.OUTPUT.FILE1. The remaining records are written to the dataset MATEPK.OUTPUT.FILE2.