DFSORT – Simple Sort with Multiple Columns Using Sort FIELDS

This tutorial will teach you the very basics of IBM’s DFSORT with multiple columns. DFSORT’s main purpose is to sort, however, it does a lot more than the sort – basically, SORT can do almost any type of data processing.

DFSORT Multiple Columns

Let’s say you have a sequential dataset (PS) with the below data. It has 3 fields –

  • employee name
  • employee salary
  • department that employee belongs to

INPUT DATA:

----+----1----+----2----+----3----+----4----+----5
SAM   11234 ACCOUNTS
JOHN  78916 IT
STEVE 62541 ACCOUNTS
TIM   90013 HR

Now we want to sort it based on department, employee name then the salary.

To achieve this we need to tell SORT from which columns each of the fields are starting and their sort order.

To sort the department column which starts from column 13 in ascending order we need to feed below to SORT:

(13,8,CH,A)
  • 13 = Starting column
  • 8 = Length of the string
  • CH = Character type data
  • A = Ascending Order

Similarly for employee name and salary, we can put the followings:

Putting it all together, SORT input would be –

(13,8,CH,A,1,5,CH,A,7,5,CH,A)

Complete JCL:

//YOUR.JOB.CARD.HERE
//*
//STEP1 EXEC PGM=SORT
//** You can give your input PS file here instead of in stream data
//SORTIN DD *
SAM   11234 ACCOUNTS
JOHN  78916 IT
STEVE 62541 ACCOUNTS
TIM   90013 HR
/*
//** You can give your OUTPUT PS file here instead of in stream data
//SORTOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(13,8,CH,A,1,5,CH,A,7,5,CH,A)
/*

OUTPUT:

----+----1----+----2----+----3----+----4----+----5
SAM   11234 ACCOUNTS
STEVE 62541 ACCOUNTS
TIM   90013 HR
JOHN  78916 IT

Drop a comment if you like this article about DFSORT with multiple columns or face any problems using the above code.

Ref: https://www-40.ibm.com/servers/resourcelink/svc00100.nsf/pages/zOSV2R3sc236878/$file/icea100_v2r3.pdf