Tuesday, July 2, 2013

JCL - Some important SORT Functions.

This page will cover the very basics in SORT.  If u are not a newbie in  sort, then check this page to get some typical sort example. Sort Examples
Feel free to add your sort experience also! 

IBM DFSORT  has three Data processing functions.
a) Sort Data   b) Merges Data    c) Copies data

Basic Format of DFSORT is shown below.
SORT FIELDS = (Position-of-field, Length-of-field,Data Format,Sequence-to-be-sorted).

example:
SORT FIELDS=(2,5,CH,A)      /*A=Ascending,  D= Descending*/
Sort Fields may also look like:
SORT FILEDS = (21,5,A,5,2,A), FORMAT=CH
It implies that sorting is done on the major field which is coded first (21,5,A) and then the Secondary or Minor field (5,2,A). Character format for both being 'CH'


Character Formats used in SORT
FORMAT : Description
CH: Is used for for textual character string
ZD: Is used for Numeric Data
BI: Is used when Numeric data is stored in pure binary format
FL: Is used to store Floating Point numbers which are COMP1 and COMP2 format.
PD: Is used for packed decimal or COMP-3
CSL:Is used when Sign is stored as separate extra character and distinguished from
numeric data.


Mandatory SORT JCL Statement
//STEPNAME EXEC PGM = SORT      /* The Sort Module
//SORTIN DD                                         /*Records to be Sorted.Input dataset goes here
//SORTOUT DD                                     /* Sorted Rcords
//SYSIN DD                                           /* Sort Control Statements
//SYSOUT DD                                       /*Output Messages
//SORTLIB DD                                      /* Library for Sort/Merge Modules.Optional parameter
//SORTWKnn DD                                  /* Work data sets. Optional parameter. Required if we use
Sample JCL
//STEP02   EXEC PGM=SYNCSORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD DSN=TEST.SORTIN,DISP=SHR
//SORTOUT  DD DSN=TEST.SORT.OUTPUT,
//            DISP=(NEW,CATLG,DELETE),UNIT=DISK,
//            DCB=(RECFM=FB,LRECL=400,BLKSIZE=8800),
//            SPACE=(TRK,(350,200),RLSE)

                     Application of Few SORT JCLS:                              

1) Copy First few Record 

//STEP02   EXEC PGM=SYNCSORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD DSN=TEST.SORTIN.DATASET,DISP=SHR
//SORTOUT  DD DSN=TEST.SORT.OUTPUT,
//            DISP=(NEW,CATLG,DELETE),SPACE=(CYL,(200,500),RLSE),
//            DCB=(*.SORTIN),UNIT=DISK
//SYSIN    DD *
SORT FIELDS=COPY,
STOPAFT=200
/*
2) SORT DUPLICATES using SYNCSORT
//STEP02   EXEC PGM=SYNCSORT

//SYSOUT   DD SYSOUT=*

//SORTIN    DD DSN=TEST.SORTIN.DATASET,DISP=SHR

//SORTOUT  DD DSN=TEST.SORT.OUTPUT,
//            DISP=(NEW,CATLG,DELETE),UNIT=DISK,
//            DCB=(RECFM=FB,LRECL=400,BLKSIZE=8800),
//            SPACE=(TRK,(350,200),RLSE)
//SORTXSUM DD DSN=TEST.SORT.OUTPUT.DUPL,
//            DISP=(NEW,CATLG,DELETE),UNIT=DISK,
//            DCB=(RECFM=FB,LRECL=400,BLKSIZE=8800),
//            SPACE=(TRK,(350,200),RLSE)
//SYSIN    DD *
SORT FIELDS=(1,3,CH,A)
SUM FIELDS=NONE,XSUM
/*
SORTOUT will contain unique records.
SORTXSUM Will contain all the eliminated duplicates

3) Skip First 150 records and copy next 20
//STEP02   EXEC PGM=SYNCSORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD DSN=TEST.SORTIN.DATASET,DISP=SHR
//SORTOUT  DD DSN=TEST.SORT.OUTPUT,
//            DISP=(NEW,CATLG,DELETE),SPACE=(CYL,(200,500),RLSE),
//            DCB=(*.SORTIN),UNIT=DISK
//SYSIN    DD *
SORT FIELDS=COPY,
SKIPREC = 150,
STOPAFT=20
/* 
Skip the first 150 records. Then Copy 20 records and stop.
4) COPY BASED ON CONDITION(INCLUDE CONDITION IN SORT)
//STEP02   EXEC PGM=SORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD DSN=TEST.SORT.INPUT,DISP=SHR
//SORTOUT  DD DSN=TEST.SORT.OUTPUT,
//            DISP=(NEW,CATLG,DELETE),UNIT=DISK,
//            DCB=(RECFM=FB,LRECL=400,BLKSIZE=8800),
//            SPACE=(TRK,(350,200),RLSE)
//SYSIN    DD *
     SORT FIELDS=COPY
      INCLUDE COND=(1,5,CH,EQ,C'INDIA')
/*

Check for multiple condition
//SYSIN    DD *
     SORT FIELDS=COPY
      INCLUDE COND=((1,5,CH,EQ,C'INDIA'),AND,(10,5,CH,EQ,C'1500'))
/* 


MULTIPLE INCLUDE  CONDITIONS IN SYNCSORT USING  OUTFIL FILES=01,OUTFIL FILES=02 and so on

When we are using SYNCSORT, the syntax will be different like below. We dont use COND in the include statement.Use only INCLUDE in SYNCSORT.

 SORT FIELDS=COPY                                               
 OUTFIL FILES=01,                                               
 INCLUDE=((80,1,CH,EQ,C'A'),AND,(259,5,CH,EQ,C'00077'))         
                                                                
 OUTFIL FILES=02,                                               
 INCLUDE=((259,5,CH,EQ,C'00061'),AND,                           
         ((80,1,CH,EQ,C'A'),OR,(80,1,CH,EQ,C'T')),AND,          
         (228,8,CH,GE,C'20090401'),AND,(228,8,CH,LE,C'20140401'))

                                                                


5) OMIT CONDITIONS IN SYNCSORT
//STEP02   EXEC PGM=SYNCSORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD DSN=TEST.SORT.INPUT,DISP=SHR
//SORTOUT  DD DSN=TEST.SORT.OUTPUT,
//            DISP=(NEW,CATLG,DELETE),UNIT=DISK,
//            DCB=(RECFM=FB,LRECL=400,BLKSIZE=8800),
//            SPACE=(TRK,(350,200),RLSE)
//SYSIN    DD *
      SORT FIELDS=COPY
      OMIT COND=(10,4,CH,EQ,C'JACK')
/*
Or like
 SORT FIELDS=COPY
      OMIT COND=(10,4,CH,EQ,C'1500')

Characters which match the condition given (exmple: C'1500') will be skipped in the output file.
Multiple conditions:
SORT FIELDS=COPY                                       
OMIT COND=((34,2,CH,EQ,C'AB'),AND,(74,1,CH,EQ,C' '))

No comments:

Post a Comment