DFSORT

DFSORT is a program used to sort, merge, and copy information. Can be invoked via SORT verb in COBOL program or as a job step in JCL.

  • Sorting is done by selecting specific columns and arrange them in a particular sequence.
  • Merging is done by combining the contents of two or more previously sorted data sets into one.
  • Copying is duplicating files. Example for backup.
  • Reformat data, add or remove fields, insert blanks and constants from input dataset.
  • Sort, merge, include or omit records according to the collating sequence mentioned below.
  • Concatenate datasets and sort.

Sorting sequence for character data and numeric data in ascending or descending order is different and has following priority

Character data:  sequenced from left to right; comprises aphabetic, alphanumeric and numeric characters. In ascending order:

+9 and then -9 (+ comes before -)

Numeric data: sequenced based on the numeric value, taking sign into consideration. In ascending order:

-9 and then +9 (-9 is less than +9)

SORT a dataset:

Character data and Numeric data are sorted differently.

The following fields are listed in ascending sequence for both character and numeric data.


Character data sequence are
+9, -9 (+ comes before -)
b9, -9 (Blank comes before -)
b999, 0009 (Blank comes before zero)

Numeric data sequence are
-9, +9 (-9 is less than +9)
-9, 9 (-9 is less than 9)
9, 999 (9 is less than 999)

Sort order of alphanumeric data (character data) is called its Collating sequence, which specifies the sort order of each character in the character set. Mainframe uses EBCDIC and the following is the sort order sequence


EBCDIC
blank
.<(+I=&!…….=“
a through z
A through Z
0 through 9

Lets consider the following example with Input file is PS with fixed length 

CustomerId starting at absolute byte 1, for 5 bytes long 

CustomerName at absolute byte 6, for 25 bytes long 

BusinessUnit at absolute byte 31, for 15 bytes long

Revenue at byte 46, for 9 bytes long

input dataset

To sort in ascending order of CustomerId, the JCL is:


//PROGPUBA JOB NOTIFY=USER
//STEP010 EXEC PGM=SORT
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=
//SORTOUT DD DSN=
//SYSIN DD *
  SORT FIELDS=(1,5,CH,A)
/*

The control statement supplies a Sort-Key implying: SORT FIELDS=(1,5,CH,A)

1 - Start sorting the record at the absolute byte address.

5 - Length, the number of bytes to be included in sorting.

CH - Format of sorting (EBCDIC character).

A - Sequence of sorting (ascending)

The sorted dataset will contain as shown

sorted dataset

The Sort control statement takes the form:


SORT FIELDS=(Sort-key1, Sort-key2, ……)
and each Sort-key will have the format 

(Start, Length, Format, Order)
Start Starting position in the record
Length Length of the data to be sorted
Format CH (EBCDIC character),

AC (ASCII character),

FS (Signed numeric character),

D1 (User-defined data type)
Order Ascending,

Descending

SUM FIELDS: 

The SUM control statement specifies that, whenever two records are found with equal sort or merge control fields, the contents of their summary fields are to be added, the sum is to be placed in one of the records, and the other record is to be deleted. If the EQUALS option is in effect the first record of summed records is kept. If the NOEQUALS option is in effect, the record to be kept is unpredictable.

Example JCL:


   File  Edit  Edit_Settings  Menu  Utilities  Compilers  Test  Help            
 -------------------------------------------------------------------------------
 EDIT       PROGPUB.AA.SOURCE(SRTJCL) - 01.00             Columns 00001 00072 
 ****** ***************************** Top of Data ******************************
 ==MSG> -Warning- The UNDO command is not available until you change            
 ==MSG>           your edit profile using the command RECOVERY ON.              
000001 //PROGPUBA JOB NOTIFY=PROGPUB                                           
000002 //S1 EXEC PGM=SORT                                                      
000003 //SYSPRINT DD SYSOUT=*                                                  
000004 //SYSOUT DD SYSOUT=*                                                    
000005 //SORTIN DD DSN=PROGPUB.AA.A1,DISP=SHR                               
000006 //SORTOUT DD DSN=PROGPUB.AA.A2,DISP=SHR                              
000007 //SYSIN DD *                                                            
000008   SORT FIELDS=(1,3,CH,D)                                                
000009   SUM FIELDS=(8,4,ZD)                                                   
000010 /*                                                                      
000011 //                                                                     
 ****** **************************** Bottom of Data ****************************

Inorder to remove duplicates from the output file use SUM FIELDS=NONE. It removes duplicates on fields specified in SORT FIELDS.

SORT and remove duplicates


   File  Edit  Edit_Settings  Menu  Utilities  Compilers  Test  Help            
 -------------------------------------------------------------------------------
 EDIT       PROGPUB.AA.SOURCE(SRTJCL) - 01.06              Columns 00001 00072 
 ****** ***************************** Top of Data ******************************
 ==MSG> -Warning- The UNDO command is not available until you change            
 ==MSG>           your edit profile using the command RECOVERY ON.              
 000001 //PROGPUBA JOB NOTIFY=PROGPUB                                           
 000002 //S1 EXEC PGM=SORT                                                      
 000003 //SYSPRINT DD SYSOUT=*                                                  
 000004 //SYSOUT DD SYSOUT=*                                                    
 000005 //SORTIN DD DSN=PROGPUB.AA.A1,DISP=SHR                               
 000006 //SORTOUT DD DSN=PROGPUB.AA.A2,DISP=SHR                              
 000007 //SYSIN DD *                                                            
 000008   SORT FIELDS=(1,3,CH,A,11,2,CH,D)                                      
 000009   SUM FIELDS=NONE                                                       
 000010 /*                                                                      
 000011 //                                                                      
 ****** **************************** Bottom of Data ****************************

Example JCLs:

Merge 2 files and sort:


   File  Edit  Edit_Settings  Menu  Utilities  Compilers  Test  Help            
 -------------------------------------------------------------------------------
 EDIT       PROGPUB.AA.SOURCE(SRTJCL) - 01.00             Columns 00001 00072 
 ****** ***************************** Top of Data ******************************
 ==MSG> -Warning- The UNDO command is not available until you change            
 ==MSG>           your edit profile using the command RECOVERY ON.              
 000001 //PROGPUBA JOB NOTIFY=PROGPUB                                           
 000002 //S1 EXEC PGM=SORT                                                      
 000003 //SYSPRINT DD SYSOUT=*                                                  
 000004 //SYSOUT DD SYSOUT=*                                                    
 000005 //SORTIN DD DSN=PROGPUB.AA.INDT1,DISP=SHR                               
 000006 //       DD DSN=PROGPUB.AA.INDT2,DISP=SHR                               
 000007 //SORTOUT DD DSN=PROGPUB.AA.OUTDT3,DISP=SHR                              
 000008 //SYSIN DD *                                                            
 000009   SORT FIELDS=(1,3,CH,A)                                                
 000010 /*                                                                      
 000011 //                                                                      
 ****** **************************** Bottom of Data ****************************

Sort and split files to different output files:


   File  Edit  Edit_Settings  Menu  Utilities  Compilers  Test  Help            
 -------------------------------------------------------------------------------
 EDIT       PROGPUB.AA.SOURCE(SRTJCL) - 01.15              Columns 00001 00072 
 ****** ***************************** Top of Data ******************************
 ==MSG> -Warning- The UNDO command is not available until you change            
 ==MSG>           your edit profile using the command RECOVERY ON.              
 000001 //PROGPUBA JOB NOTIFY=PROGPUB                                           
 000002 //S1 EXEC PGM=SORT                                                      
 000003 //SYSPRINT DD SYSOUT=*                                                  
 000004 //SYSOUT DD SYSOUT=*                                                    
 000005 //SORTIN DD DSN=PROGPUB.AA.INP,DISP=SHR                               
 000006 //SORTOF01 DD DSN=PROGPUB.AA.A2,DISP=SHR                             
 000007 //SORTOF02 DD DSN=PROGPUB.AA.A3,DISP=SHR                             
 000008 //SORTOF03 DD DSN=PROGPUB.AA.A4,DISP=SHR                             
 000009 //SYSIN DD *                                                            
 000010   SORT FIELDS=(1,3,CH,A)                                                
 000011    OUTFIL FILES=01,INCLUDE=(1,1,CH,EQ,C'A')                             
 000012    OUTFIL FILES=02,INCLUDE=(1,1,CH,EQ,C'V')                             
 000013    OUTFIL FILES=03,INCLUDE=(1,1,CH,EQ,C'J')                             
 000014 /*                                                                      
 000015 //                                                                      
 ****** **************************** Bottom of Data ****************************