DFSORT – Simple sort with multiple columns using SORT FIELDS

1 min


A tutorial on DFSORT with multiple columns.

Example of DFSORT with multiple columns

This tutorial will teach you the very basics of IBM’s DFSORT. 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.

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 or face any problem using the above code.

For more mainframe tutorials, visit https://mainframe.debugpoint.com.


We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!

Join our Telegram channel and stay informed on the move.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


Arindam

Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, LinkedIn, or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x