In Mainframe JCL, to sort a dataset, you need to know the usage of the DFSORT utility. This post shares ten best examples to understand the functionality of it.
10 DFSORT Examples.
Go through each example from the question 1 to 10 in sequence. Use those in your project, verify the results. Check the concept behind it, it is sure your knowledge improves a lot.
1. How to copy data set in DFSORT?
SORT FIELDS=COPY
FIELDS - The input data set is copied to the output data set
without sorting or merging.
2. How to compare Dates in DFSORT?
INCLUDE FORMAT=Y2T,
COND=(3,4,GE,Y’9901’,AND,
3,4,LE,Y’0312’,OR,
3,4,LE,Y’0000’)
This example illustrates how to only include records in which: A C’yymm’ date field in bytes 3 through 6 is between January 1999 and December 2003 OR INCLUDE Control Statement.
Note that the century window in effect will be used to interpret the Y’9901′ and Y’0312′ date constants, as well as real dates in the C’yymm’ date field.
However, the century window will not be used to interpret the Y’0000′ special indicator constant or special indicators in the C’yymm’ date field.
INCLUDE COND=(2,3,Y2X,LT,36,5,Y2T)
This example illustrates how to only include records in which a P’dddyy’ date field in bytes 2 through 4 is less than a Z’yyddd’ date field in bytes 36 through 40.

Note that the century window in effect will be used to interpret real dates in the P’dddyy’ and Z’yyddd’ date fields. However, the century window will not be used to interpret special indicators in the P’dddyy’ and Z’yyddd’ date fields
3. How to Sort dataset in DFSORT?
SORT FIELDS=(7025,4,A,5048,8,A),FORMAT=ZD,EQUALS
FIELDS – The major control field begins on byte 7025 of each record, is 4 bytes long, contains zoned decimal data (FORMAT=ZD), and is to be sorted in ascending sequence.
The second control field begins on byte 5048, is 8 bytes long, has the same data format as the first field, and is also to be sorted in ascending order.
FORMAT – FORMAT=ZD is used to supply ZD format for the p,m,s fields and is equivalent to specifying p,m, ZD,s for these fields.
With FORMAT=f, you can mix p,m,s and p,m,f,s fields when that’s convenient such as when all or most of the fields have the same format (although you can always code p,m,f,s for all fields and not use FORMAT=f, if you prefer).
For example, the following are also valid uses of the FORMAT=f parameter:
SORT FORMAT=BI,FIELDS=(21,4,A,5,4,PD,A,31.3,1.4,A,52,20,A)
SORT FIELDS=(16,4,A,22,8,BI,D,3,2,A),FORMAT=FI
EQUALS – specifies that the sequence of equal collating records is to be preserved from input to output.
4. How to use DFSORT Include and Exclude?
5. How to get Current Date from DFSORT?
Operand Constant April 19, 2001,
04:52:45 PM
DATE1 C'yyyymmdd' C'20010419'
DATE1(c) C'yyyycmmcdd' C'2001/04/19'
DATE2 C'yyyymm' C'200104'
DATE2(c) C'yyyycmm' C'2001/04'
DATE3 C'yyyyddd' C'2001109'
DATE3(c) C'yyyycddd' C'2001/109'
DATE4 C'yyyy-mm-dd-hh.mm.ss'
C'2001-04-19-16.52.45'
INCLUDE COND=(1,13,CH,GT,DATE4) - Here DATE4 is a timsestamp
6. How to represent Hex in DFSORT?
Hex Value Length
--- ---- ------
Valid X'FF' FF 1
Valid X'BF3C' BF3C 2
Valid 3X'00000F' 00000F00000F00000F 9
Valid 4000X'FFFF' FF repeated 8000 times 8000
7. How to use IFTHEN, INREC in DFSORT?
7a. How to use Lookup table in DFSORT?
OPTION COPY,Y2PAST=1985
INREC FIELDS=(SEQNUM,4,BI,
8,5,ZD,TO=PD,
31,2,PD,TO=FI,LENGTH=2,
15,6,Y2TP,
25,3,CHANGE=(1,C’L92’,X’01’,C’M72’,
X’02’,C’J42’,X’03’),
NOMATCH=(X’FF’))
This example illustrates how a sequence number can be generated, how values in one numeric or date format can be converted to a nother format, and how a lookup table can be used.
The reformatted input records will look as follows:
Position Contents 1-4 A binary sequence number that starts at 1 and increments by 1. 5–7 A PD field containing the converted ZD field from input positions 8 through 12.
8–9 An FI field containing the converted PD field from input positions 31 through 32.
10–14 A P’yyyymmdd’ date field containing the C’yymmdd’ date field from input positions 15-20 transformed according to the specified century window of 1985-2084.
15 A BI field containing X’01’, X’02’, X’03’ or X’FF’ as determined by using a lookup table for the input field in positions 25-27.
The SORT statement can now refer to the “sort” field in the reformatted input records.
The OUTREC statement is used to restore the records to their original format.
8. How to remove Duplicates in DFSORT?
SUM FIELDS=(5,5,ZD,12,6,PD,21,3,PD,35,7,ZD)
SUM FORMAT=ZD,FIELDS=(5,5,12,6,PD,21,3,PD,35,7)
SUM FIELDS=(5,5,ZD,12,6,21,3,35,7,ZD),FORMAT=PD
SUM FIELDS=NONE ......removes duplicates
The permissible field formats are shown under the description of ‘f’ for fields.
Default: None.
FORMAT=f must be specified if any field is specified as p,m rather tha n p,m,f.
9. How to use IFTHEN overlay in DFSORT?
10. How to Use Packed or Zoned decimal in DFSORT?
Related Posts
You must be logged in to post a comment.