Sort records between header and footer (DATASORT Operator)


DATASORT operator sorts the records between header and trailer in the input file and copy them to an output file. It copies one or more header/trailer records to the output file in their original order while sorting. The first n records (header records) and/or last n records (trailer records) are kept aside while sorting, and the data records between them are sorted.

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 - specifies ddname of the input file. It is mandatory when FROM operand is coded.
  • TO - specifies 1 to 10 ddnames of output files. TO and USING operands can code at the same time.
  • USING - specifies the first 4-characters of the ddname (xxxxCNTL) for the DFSORT control statement. XXXX name is the user-defined name. Either TO, USING, or both operands can code at the same time.
  • HEADER or FIRST or HEADER(u) or FIRST(u) - Specifies the number of header record(s) that should be copied to the output file. HEADER and FIRST are equivalent to HEADER(1) and FIRST(1). u value ranges from 1 to 1000000.
  • TRAILER or LAST or TRAILER(v) or FIRST(v)- Specifies the number of trailer record(s) that should be copied to the output file. TRAILER and LAST are equivalent to TRAILER(1) and LAST(1). v value ranges from 1 to 1000000.

Optional Operands


  • VSAMTYPE - specifies the record format for a VSAM input file. It should be either F (fixed-length) or V (variable-length) record processing.

Example -


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

INPUT - MATEPK.INPUT.PSFILE1

----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
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

JCL -

----+----1----+----2----+----3----+----4----+----5----+
...
//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)
/*
...

OUTPUT - MATEPK.OUTPUT.PSFILE1

----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
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