SORT Function and Examples
SORT
SORT Fields: The syntax is given below:SORT FIELDS=(p1,l1,f1,o1,p2,l2,f2,o2,.....) ¦
FIELDS=(p1,l1,o1,p2,l2,o2...),FORMAT=x
FIELDS={(} COPY {)} ¦ {,CKPT}
{,DYNALLOC{=(d ¦ ,n ¦ d,n ¦ OFF)}}
{,EQUALS ¦ ,NOEQUALS}
{,FILSZ={n ¦ En ¦ Un} }
{,SIZE={n ¦ En ¦ Un} }
{,SKIPREC=n}
{,STOPAFT=n}
p1/p2: Indicates the starting position byte of
the record from where sort applies. It is in the range from 1 to 4092 =>1 is the first byte for a fixed length record and 5 is the byte for a variable length record.
COPY: It simply copies
records from sortin to sortout . An example is to copy the contents of a tape
dataset to DASD/tape , say as a back-up
option for cases when the tape dataset records get deleted after some specified
retention time, or when the dataset might for some reason become unavailable.
Please refer PART1 and PART3 here.
l1/l2: gives the total field length .
f1/f2: is the format type for the field (ex :
CH,PD,BI,AC,ZD,FI,FL etc……)
o1/o2: is the mode of sort whether ascending(A) or
descending(D).
FORMAT: If the sort fields have the same format then
we can write the format type at the end instead of repeating for every sort
fields.
FILSZ OR SIZE: Tells
the number of records in the input file.
If 'En ' then the value is
an estimate (preferred).
If 'n' then the exact
number of records are to be specified.
SKIPREC: It skips
'n' records before starting to sort the given file.
STOPAFT: This
statement provides the option of stopping the sorting process after 'n' records.
CKPT:
Indicates that a checkpoint is desired at the end of volume of a
sortout data set when outfil is not used.
EQUALS/NOEQUALS : Equal
determines whether the sort will preserve the order of the records with
identical sort fields. NOEQUAL is the default.
If NOEQUALS is in effect, the record which is retained is
determined arbitrarily. If EQUALS is in
effect, the record which is read first is retained.
Example :
SORT FIELDS = (2,4,CH,A,15,30,CH,D)
SORT
FIELDS = (2,4,A,15,30,A),FORMAT=CH,STOPAFT=100
COPY FUNCTION EXAMPLE:
//**********************************************************************
//*
SIMPLE SORT
//**********************************************************************
//STEP01 EXEC PGM=SORT
//SORTIN DD DSN=SORT.FILE1,DISP=SHR
//SORTOUT DD DSN=SORT.FILE.OUT1,
//
DISP=(NEW,CATLG,DELETE),
//
UNIT=SYSDA,SPACE=(TRK,(05,05),RLSE),
//
DCB=(BLKSIZE=800,LRECL=80,RECFM=FBA)
//SYSIN
DD *
SORT
FIELDS=COPY
//SYSOUT DD SYSOUT=*
ALTSEQ FUNCTION:
This control
statement provides an alternate collating sequence for all the control fields
for which the format code AQ
has been specified on the SORT/MERGE or INCLUDE/OMIT or INCLUDE/
//******************************************************************
//* ALTSEQ CODE WILL REPLACE THE SPECIFIC CHARACTERS WITH THE
GIVEN
//* CHARACTERS.ITS MAIN PURPOSE IS TO REMOVE INVALID CHARACTERS.
//* IN THE BELOW MENTIONED CODE WE ARE REPLACING HEX VAL OF '..'
BY
//* HEX VALUE OF SPACES.
//* NOTE : ONLY HEX VALUE IS USED TO REPRESENT CHARACETRS
//******************************************************************
//STEP02 EXEC PGM=SORT
//SORTIN DD DSN=SORT.FILE2,DISP=SHR
//SORTOUT DD DSN=SORT.FILE.OUT2,
//
DISP=(NEW,CATLG,DELETE),
// UNIT=SYSDA,SPACE=(TRK,(05,05),RLSE),
//
DCB=(BLKSIZE=800,LRECL=80,RECFM=FB)
//SYSIN DD *
SORT FIELDS=COPY
ALTSEQCODE=(4B40)
OUTREC
FIELDS=(1,80,TRAN=ALTSEQ)
//SYSOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
INITIALIZE EMPTY VSAM FILE
//**********************************************************************
//*TO COPY VSAM FILE TO FLAT FILE AND TO PREVENT ABENDING OF
SUBSEQUENT
//* STEPS IF THE INPUT VSAM FILE IS EMPTY.
//**********************************************************************
//STEP03 EXEC
PGM=SORT,PARM='VSAMEMT=YES'
//SORTIN DD DSN=SORT.FILE3,DISP=SHR
//SORTOUT DD DSN=SORT.FILE.OUT3,
//
DISP=(NEW,CATLG,DELETE),
//
UNIT=SYSDA,SPACE=(TRK,(05,05),RLSE),
//
DCB=(BLKSIZE=800,LRECL=80,RECFM=FB)
//SYSIN DD *
SORT FIELDS=COPY
//SYSOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=* Please refer PART1 and PART3 here.
Good info.
ReplyDeleteFor more SORT sample jobs with clear explanation,refer http://clearmainframeinterview.blogspot.in/search/label/SORT
Sreenivas
http://clearmainframeinterview.blogspot.in/search/label/SORT