DATASORT Operator (for Beginners)


DATASORT operator sort data records between header and trailer in the input file and copy them to an output file.

DATASORT operator copies one or more header/trailer records to the output file in their original order while sorting.

Syntax -

Sorting records skipping header and footer of 1 record each -

  DATASORT FROM(indd) TO(outdd) HEADER TRAILER USING(CTL1)
OR
  DATASORT FROM(indd) TO(outdd) FIRST LAST USING(CTL1)

Sorting records skipping m-lines header and n-lines footer -

  DATASORT FROM(indd) TO(outdd) HEADER(m) TRAILER(n) USING(CTL1)
OR
  DATASORT FROM(indd) TO(outdd) FIRST(m) LAST(n) USING(CTL1)

Sorting records skipping m-lines header and n-lines footer for variable-length VSAM file -

  DATASORT FROM(indd) TO(outdd) HEADER(m) TRAILER(n) USING(CTL1) VSAMTYPE(V)
OR
  DATASORT FROM(indd) TO(outdd) FIRST(m) LAST(n) USING(CTL1) VSAMTYPE(V)

Required Operands


  • FROM - FROM parameter specifies ddname of the input file.
  • TO - TO operand specifies 1 to 10 ddnames of output files.
  • USING - USING operand specifies the first 4-characters of the ddname (xxxxCNTL) for the DFSORT control statement file.
  • HEADER or FIRST - Specifies one header record is to be maintained in place.
  • HEADER(u) or FIRST(u) - Specifies u header records is to be maintained in place.
  • TRAILER or LAST - Specifies one trailer record is to be maintained in place.
  • HEADER(v) or FIRST(v) - Specifies v trailer records is to be maintained in place.

Optional Operands


  • VSAMTYPE - VSAMTYPE operand specifies the record format for a VSAM input file (F or V).

Example -


Scenario - Sort the employee data records (kept the header and footer) based on the country code (from the 60th position of length 2) to the output file.

INPUT - MATEPK.INPUT.PSFILE1

 BROWSE    MATEPK.INPUT.PSFILE                        Line 00000000 Col 001 080 
 Command ===>                                                  Scroll ===> CSR  
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
ENO  NAME          TECHNOLOGY          COMPANY   STATE     COUNTRY      
========================================================================
001  PAWAN         MAINFRAME           JPM       AP        IN           
003  SRIDHAR       SAS                 CG        OR        US           
004  VENKATESH     ABAP                CSC       CA        US           
006  PRASAD        HR                  INFOSYS   MI        US           
002  SRINIVAS      TESTING             ORACLE    TG        IN           
005  RAVI          HADOOP              CTS       FL        US           
007  RAJA          TESTING             IBM       CA        US           
========================================================================
TOTAL NUMBER OF RECORDS: 007                                            
**************************** Bottom of Data ****************************

JCL -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
//MATEPKDC JOB (123),'MATEPK',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),        
//             NOTIFY=&SYSUID                                           
//*                                                                     
//STEP01   EXEC PGM=ICETOOL                                             
//INDD     DD DSN=MATEPK.INPUT.PSFILE1,DISP=SHR                         
//OUTDD    DD DSN=MATEPK.OUTPUT.PSFILE1,                                
//            DISP=(NEW,CATLG,DELETE),VOLUME=SER=DEVHD4,                
//            SPACE=(TRK,(1,1),RLSE),UNIT=SYSDA,                        
//            DCB=(DSORG=PS,RECFM=FB,LRECL=80,BLKSIZE=800)              
//TOOLIN   DD *                                                         
  DATASORT FROM(INDD) TO(OUTDD) HEADER(2) TRAILER(2) USING(CTL1)        
/*                                                                      
//CTL1CNTL DD *                                                         
  SORT FIELDS=(60,2,CH,A)                                               
/*                                                                      
//TOOLMSG  DD SYSOUT=*                                                  
//DFSMSG   DD SYSOUT=*                                                  
//                                                                      
**************************** Bottom of Data ****************************

OUTPUT - MATEPK.OUTPUT.PSFILE1

----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
********************************* Top of Data **********************************
ENO  NAME          TECHNOLOGY          COMPANY   STATE     COUNTRY              
========================================================================        
001  PAWAN         MAINFRAME           JPM       AP        IN                   
002  SRINIVAS      TESTING             ORACLE    TG        IN                   
003  SRIDHAR       SAS                 CG        OR        US                   
004  VENKATESH     ABAP                CSC       CA        US                   
005  RAVI          HADOOP              CTS       FL        US                   
006  PRASAD        HR                  INFOSYS   MI        US                   
007  RAJA          TESTING             IBM       CA        US                   
========================================================================        
TOTAL NUMBER OF RECORDS: 007                                                    
******************************** Bottom of Data ********************************

TOOLMSG (SDSF SPOOL) - Verify TOOLMSG for the return code of the submitted job.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9
********************************* TOP OF DATA ********************************************
1ICE600I 0 DFSORT ICETOOL UTILITY RUN STARTED                                   
                                                                                
 ICE650I 0 VISIT http://www.ibm.com/storage/dfsort FOR ICETOOL PAPERS, EXAMPLES AND MORE 
                                                                                
 ICE632I 0 SOURCE FOR ICETOOL STATEMENTS:  TOOLIN                               
                                                                                
                                                                                
 ICE630I 0 MODE IN EFFECT:  STOP                                                
                                                                                
            DATASORT FROM(INDD) TO(OUTDD) HEADER(2) TRAILER(2) USING(CTL1)      
ICE606I 0 DFSORT CALL 0001 FOR SORT  FROM INDD     TO OUTDD    USING CTL1CNTL COMPLETED 
ICE602I 0 OPERATION RETURN CODE:  00                                            
                                                                                
                                                                                
 ICE601I 0 DFSORT ICETOOL UTILITY RUN ENDED - RETURN CODE:  00                  
******************************** BOTTOM OF DATA ******************************************

Explaining Solution -

  • INDD - Specifies the ddname for input file.
  • OUTDD - Specifies the ddname for output file.
  • TOOLIN DD * - Specifies the ICETOOL statements for DFSORT.
  • CTL1CNTL DD * - Specifies the DFSORT statements for processing.
  • TOOLMSG - Specifies where to write the ICETOOL processing messages.
  • DFSMSG - Specifies where to write the DFSORT processing messages.
  • COPY FROM(INDD) TO(OUTDD) - Copies all the records from INDD file to OUTDD file.
  • DATASORT FROM(INDD) TO(OUTDD) HEADER(2) TRAILER(2) USING(CTL1) - Filters the records from INDD by ignoring 2 header and 2 trailer records.
  • SORT FIELDS=(60,2,CH,A) – DFSORT condition in CTL1 to sort the records from 60th position of 2 length.