8/2/2019 The SORT Utility v1.1
1/32
SORT UTILITY
Anish_DosslinRestricted Page 1 27-Feb-2004
SORT UTILITY
Prepared by: Anish Dosslin
Date Created: February 19, 2004Date Modified: March 12, 2004
8/2/2019 The SORT Utility v1.1
2/32
SORT UTILITY
Revision
Number
Date Summary of Changes Reviewers/Authors
ID
1.0 February 2004 Original Draft Anish_Dosslin
1.0 February 2004 Review of document Uma_Patil
1.1 27 February 2004 Corrections after review
Added: CONVERT, FTOV
Anish_Dosslin
1.2 02 March 2004 Section on merge added Anish_Dosslin
Anish_DosslinRestricted Page 2 27-Feb-2004
8/2/2019 The SORT Utility v1.1
3/32
SORT UTILITY
1. INTRODUCTION - SORT ........... 41.1 NECESSARY STATEMENTS .....................................................................................4
1.1.1 STEP CARD SYNTAX ..........................................................................................4
1.1.2 SYSOUT STATEMENT ........................................................................................4
1.1.3 SORTIN STATEMENT .........................................................................................41.1.4 SYSIN STATEMENT ............................................................................................5
1.2 OPTIONAL STATEMENTS .........................................................................................6
1.2.1 SORTOUT STATEMENT .....................................................................................6
1.2.2 SORTOFXX STATEMENT ...................................................................................6
2 SORT CARD ............................ 62.1 INCLUDE ......................................................................................................................7
2.1.1 INCLUDE SYNTAX ..............................................................................................7
2.1.2 EXAMPLES ...........................................................................................................8
2.2 OMIT ............................................................................................................................14
2.3 INREC ..........................................................................................................................142.3.1 INREC SYNTAX .................................................................................................14
2.3.2 EXAMPLES ..........................................................................................................14
2.4 OUTREC ......................................................................................................................16
2.5 OUTFIL ........................................................................................................................16
2.5.1 OUTFIL SYNTAX ...............................................................................................16
2.5.10 CONVERT ..........................................................................................................20
2.5.11 FTOV ..................................................................................................................20
2.6 SORT ............................................................................................................................22
2.6.1 SORT CARD SYNTAX .......................................................................................22
2.7 MERGE ........................................................................................................................25
2.7.1 SYNTAX ...............................................................................................................252.7.2 EXAMPLE ............................................................................................................26
2.8 SUM .............................................................................................................................28
2.8.1 SYNTAX ...............................................................................................................28
2.8.4 EXAMPLES ..........................................................................................................29
3 EXAMPLES ............................ 303.1 EXAMPLE 1 ............................................................................................................30
3.2 EXAMPLE 2 ............................................................................................................31
3.3 EXAMPLE 3 ............................................................................................................32
Anish_DosslinRestricted Page 3 27-Feb-2004
8/2/2019 The SORT Utility v1.1
4/32
SORT UTILITY
1. INTRODUCTION - SORT
1.1 NECESSARY STATEMENTS
1. Step Card2. \\SYSOUT DD SYSOUT=*
3. \\SORTIN or SORTINxx
4. \\SYSIN DD
1.1.1 STEP CARD SYNTAX
\\Step_name EXEC PGM=SORT
The step card is where you define a STEP inside a JOB. You give the step a stepname (optional) then there is a positional parameter EXEC followed by PGM/PROC. If
the step is to execute a PROC then the PROC keyword is optional.
The step name can be a user defined 8-character name. It can be alphanumeric withnational characters, but cannot start with a number.
1.1.2 SYSOUT STATEMENT
The SYSOUT statement should be specified so that the output messages from theutility can be saved there. This If the SYSOUT statement is not specified then the
utility ABENDs with a user return code of 0016. This is illustrated in the figure givenbelow.
1.1.3 SORTIN STATEMENT
The SORTIN DD will contain the input file(s) that has to be sorted. The SORTIN DD
can be mentioned in two different ways.
Anish_DosslinRestricted Page 4 27-Feb-2004
8/2/2019 The SORT Utility v1.1
5/32
SORT UTILITY
1. SORTIN
2. SORTINxx
The first method is used when there in only one input file or if the input files can beappended one behind the other.
\\SORTIN DD DSN=TMP.EXAMPLE.INPUT.FILE1,DISP=SHR
The above statement takes one file as the input file.
\\SORTIN DD DSN=TMP.EXAMPLE.INPUT.FILE1,DISP=SHR
\\ DD DSN=TMP.EXAMPLE.INPUT.FILE2,DISP=SHR
\\ DD DSN=TMP.EXAMPLE.INPUT.FILE3,DISP=SHR\\ DD DSN=TMP.EXAMPLE.INPUT.FILE4,DISP=SHR
\\ DD DSN=TMP.EXAMPLE.INPUT.FILE5,DISP=SHRA number of files can be given under one SORTIN DD name by concatenating them
together. As a result the five files are appended one behind the other and consideredas one single file.
The second method: The SORTINxx DD statements are used for MERGE fields andalso used in OUTFIL control statements. A maximum of 100 DDs can be specifiedstarting from 00 to 99, some numbers may be skipped or neglected which makes no
difference. The output is not altered some of the intermediate numbers are missedor if the order of numbers is improper.
The SORTINxx DD DSNs should have the same LRECL and also they should be in theproper sequence for MERGE.
1.1.4 SYSIN STATEMENT
The SYSIN statement contains the actual SORT card. The sort card can be inline
(coded directly) in a JCL or can be contained in a member of a PDS.
1. SORT card coded in JCL
\\SYSIN DD *SORT FIELDS=COPY
\*
\* acts as the delimiter specifying the end of the SORT card on an inlinesort card.
2. SORT card coded in a member of a PDS
\\SYSIN DD DSN=TMP.EXAMPLES.PDS(CTLCARD1),DISP=SHR
The a sort card coded inside a member of a PDS is called a Control card. It
functions as the same way as the inline sort card.The sort card can be converted into a control card by copying the sort
statements between the SYSIN statement and the delimiter and saving
them in a separate member of a PDS. A delimiter should not be coded in acontrol card.
Anish_DosslinRestricted Page 5 27-Feb-2004
8/2/2019 The SORT Utility v1.1
6/32
SORT UTILITY
The JCL ABENDS with a system return code of 13 (S013 U0000) when thespecified control card member is not found as mentioned in the SYSIN DD.
1.2 OPTIONAL STATEMENTS
The most commonly used optional statements are,1. \\SORTOUT
2. \\SORTOFxx OR \\SORTOFx
There are other optional JCL statements such as SORTXSUM which will be dealt at alater stage.
1.2.1 SORTOUT STATEMENT
The SORTOUT DD will contain the output file which will have the sorted data.
\\SORTOUT DD DSN=TMP.EXAMPLE.OUTPUT.FILE1,DISP=(MOD,CALTG,CATLG)
1.2.2 SORTOFXX STATEMENT
The SORTOFx or SORTOFxx statement is used when the output files are determined
by the control card (i.e. by the OUTFIL FILES statement)
1.3 METHODS OF SPECIFYING OUTPUT FILES
The output file can be referred in three different ways.1. SORTOUT DD
2. SORTOFxx DD (with OUTFIL FILES)
3. DD_Name DD (With OUTFIL FNAMES)
SORTOUT DD is the most common method. The file which is mentioned in theSORTOUT DD statement and the output is stored there. If the output DD is
concatenated with more than one file, then the output records go to the first DD DSNand the other will have an empty file if they are created new or will have the same
data as before the SORT if the disposition was MOD
SORTOFxx DD: The output files can be mentioned in the SORTOFx or SORTOFxx DDnames, where x/xx can vary from 1/01 to 99. The exact output file DD has to be
explicitly mentioned in the OUTFIL FILE. This will be dealt later in the OUTFIL
section.
DD_Name DD (With OUTFIL FNAMES): Any DD, excluding special DD names like
SYSIN, SYSOUT, SORTIN, etc, can be converted into an output sort file using OUTFILFNAMES. The DD name is given within braces in the FNAMES. This will be dealt later
in the OUTFIL section.
2 SORT CARD
Anish_DosslinRestricted Page 6 27-Feb-2004
8/2/2019 The SORT Utility v1.1
7/32
SORT UTILITY
The sort card will contain the condition on which the input file is to be sorted on. Thesort card contains more features than just sorting, it can be used to add trailer,
increase/decrease the size of the input record, insert new fields and include/omit afew records depending on certain conditions.
These functions are taken care of by statements known as control statements. The
control statements tell how to process the files. There are 12 control statements.
ALTSEQSpecifies an alternate collating sequence for control fields with anAQ format.
END Signals the end of control statements in the control card
INCLUDE
Specifies the criteria which determine whether or not records are
included in the sort or the output file
INREC Reformats the input record before sort/merge process
MERGE Specifies merge control fields.
MODS Specifies user exit(s).
OMITSpecifies the criteria which determine whether or not records areomitted from the sort process and the output file
OUTFIL Describes the output file(s)
OUTREC Reformats the output record after sort/merge process.
RECORD Provides record information at various processing stages.
SORT
Defines a sort or copy process and specifies sort criteria
SUM Deletes records with equal control fields and summarizes numeric
fields on those records.
2.1 INCLUDE
The INCLUDE condition to include only the required records. This is achieved bycomparing a field with a constant or another field in the same record. The INCLUDE
and OMIT condition cannot appear in the same SORT card.
2.1.1 INCLUDE SYNTAX
INCLUDE COND=NONEALL
(Condition1,Logical operator,Condition2,Logical
operator,Condition3)
Logical Operator
AND, & Both AND and & can be used interchangeably as the logical AND
OR, | Both OR and | can be used interchangeably as the logical OR
ConditionX
Anish_DosslinRestricted Page 7 27-Feb-2004
8/2/2019 The SORT Utility v1.1
8/32
SORT UTILITY
There can be any number of conditions. The conditions can be separated by bracesto improve readability.
The syntax of the condition where the comparison is done with a constant contains 5
parts.
Starting Byte position, No. of bytes, Format, Operator, constant value
Example:
INCLUDE COND=(5,5,PD,EQ,11111)INCLUDE COND=(1,3,CH,EQ,CAL,&,5,5,PD,LT,50000)
Starting Byte position: This is the relative byte position in the record. The firstbyte is considered as 1 and so forth.
No. of Bytes: The number of bytes to be considered from the startingbyte position for the comparison. The starting byte
specified in the starting-byte-position field is alsoconsidered.
Format: The format is specifies the type of data contained in the
bytes that were selected for the process.The most commonly used formats are,1. BI Binary (1 to 4092 bytes)
2. PD Packed decimal (1 to 256 bytes)3. ZD Zoned decimal (1 to 256 bytes) The first 4 bits (nibble) of the
rightmost byte is the sign nibble. XF/C/E/A) denotes positive, XD/B denotesnegative
4. CH Character
5. SS Substring. It searches for the string mentioned in the constant field and
tries to find a match of it an anywhere inside the bytes specified for
processing then the record is considered for processingOperator: The operator mentioned below are the most commonly
used.1. EQ Equal
2. NE Not equal3. GT Greater than
4. GE Greater than or equal to5. LT Less than
6. LE Less than or equal toConstant Value: The constant value is compared with the bytes
mentioned of the processing.
1 COBELIX Character constant CH
2 X00122C constant (represents
122 in PIC S9(3)
COMP-3)
CH
3 X'40' constant (representsa SPACE)
CH
3 122 Numeric constant PD, BI, ZD
2.1.2 EXAMPLES
Sample Control Cards and their explanations:
Anish_DosslinRestricted Page 8 27-Feb-2004
8/2/2019 The SORT Utility v1.1
9/32
SORT UTILITY
A SAMPLE COPY BOOK: CPYSMPL1
1 WS-SAMPLE-CPYBOOK-1.
05 WS-NAME PIC X(15).
05 WS-EMPLOYEE-NUM PIC 9(06).
05 WS-AGE PIC S9(02) USAGE COMP.05 WS-DATE-OF-BIRTH PIC S9(09) USAGE COMP-3.05 WS-DESIGNATION PIC X(30).
05 FILLER PIC X(02).
Figure 1.1
INCLUDE COND=(1,15,CH,EQ,COBELIX) This will include the records that have
the characters OBELIX in the first fifteen bytes of the record, starting only from thefirst byte.
Anish_DosslinRestricted Page 9 27-Feb-2004
8/2/2019 The SORT Utility v1.1
10/32
SORT UTILITY
INCLUDE COND=(22,2,BI,NE,25) The starting byte is 22 and number of bytes, 2
corresponds to the starting byte position of WS-AGE field. As format specified isbinary (BI) and as a result the, only records that are not equal to 25 are included.
INCLUDE COND=(16,6,CH,GT,C025000) This condition includes the recordsthat has a value greater than 25000 in the specified byte locations. In the specified
byte location the format contained is of 9(08). As PIC 9(08) is interchangeable with
PIC X(08), the data is considered as character.The same can be achieved by INCLUDE COND=(16,6,ZD,GT,025000)
Anish_DosslinRestricted Page 10 27-Feb-2004
8/2/2019 The SORT Utility v1.1
11/32
SORT UTILITY
INCLUDE COND=(24,5,PD,EQ,19810626)INCLUDE COND=(24,5,CH,EQ,X019810626C) Both the conditions, include the
records that have the 19810626 in PD format in the specified byte locations.The second condition represents the comparison field in the hexadecimal format and
the Format is given as CH and not as PD.
The hexadecimal format comes useful when you have a compound key. For example,
you have a key as follows.
1 WS-KEY.
Anish_DosslinRestricted Page 11 27-Feb-2004
8/2/2019 The SORT Utility v1.1
12/32
SORT UTILITY
05 WS-YEAR PIC S9(05) COMP-3.05 WS-DEPARTMENT PIC X(03).
05 WS-ROLL-NUMBER PIC S9(03) COMP-3.
Lets say WS-YEAR = 1998, WS-DEPARTMENT = CSE and WS-ROLL-NUMBER = 2.This can be represented in hexadecimal format as X01998CC3E2C5002C.
The hexadecimal equivalent in 1998 (S9(05) COMP-3/PD) is X01998C, where C
represents positive. This occupies 3 bytes and hence 6 hexadecimal digits.
The hexadecimal equivalent in CSE is XC3E2C5, where XC3 represents C, XE2represents S and XC5 represents E.
The hexadecimal equivalent in 2 (S9(03) COMP-3/PD) is X002C, where C denotes
positive. This occupies 3 bytes and hence 4 hexadecimal digits.
INCLUDE COND=(1,1,CH,LE,CG,AND,22,2,BI,LE,25)INCLUDE COND=(1,1,CH,LE,CG,&,22,2,BI,LE,25) This sort card includes the
names those have the starting letter as a character less than G (i.e. A, B, C, D, E,F) and those corresponding age should be less than or equal to 25. The logical
operator is AND which can also be represented by &.
INCLUDE COND=(1,15,SS,EQ,CNIX,OR,29,30,SS,EQ,CSOFTWARE)
INCLUDE COND=(1,15,SS,EQ,CNIX,|,29,30,SS,EQ,CSOFTWARE) Thisinclude the records that have the string NIX in any of the first fifteen bytes or the
string SOFTWARE is present starting from any position in the thirty bytes mentionedin the second condition. The two conditions are linked by an OR. The OR can also be
represented by |.
Anish_DosslinRestricted Page 12 27-Feb-2004
8/2/2019 The SORT Utility v1.1
13/32
SORT UTILITY
The syntax of the condition where the comparison is done with another field in the
same record constant contains 5 parts.
Starting position of 1st field, No. of bytes, operator, Starting position of2nd field, No of bytes
Example:
INCLUDE COND=(2,1,NE,30,1),FORMAT=CH
This condition will check the second byte (2,1,NE,30,1)with the thirtieth byte(2,1,NE,30,1) and will include the records which have these two fields are not equal;
both the fields be considered in the character format.
Anish_DosslinRestricted Page 13 27-Feb-2004
8/2/2019 The SORT Utility v1.1
14/32
SORT UTILITY
If the any one of the fields under consideration has more bytes than the other, thenthe smaller field is padded either with ZERO in case of ZD, PD and BI or with SPACE
incase of CH.Let us consider COND=(2,1,NE,30,5),FORMAT=CH
In this case, a one byte character is being compared with a five byte character field.As one of the fields is smaller, the smaller field is taken and padded with
SPACE/ZERO so that both the fields occupy the same number of bytes.
2.2 OMIT
The OMIT COND operates in the same way as the INCLUDE COND, but the difference
is that the records that pass the condition are omitted in this case.
2.3 INREC
INREC is used to reformat the records before the sort process actually happens. Thisstatement can be used to add or delete fields, reformat one type of data to another,
etc.Note: This INREC process happens before the INCLUDE or OMIT conditions or before
the sort.
2.3.1 INREC SYNTAX
INREC FIELDS=(Starting position in reformatted record:Starting position in input record,
Number of bytes to be copied,Old field format [optional],
New field format [optional] )Note: The condition can be repeated multiple times with
different criteria
Another representation:
INREC FIELDS=(Starting position in reformatted record:n{X, Xhh..,C ,Z})
n Positive integer
X Inserts SPACE (repeated n times)Xhh.. Inserts a hexadecimal value (repeated n times)
C Inserts the string mentioned inside the quotes (repeated n times)
2.3.2 EXAMPLES
A SAMPLE COPY BOOK: CPYSMPL2
01 WS-SMPL-CPYBOOK-2.05 WS-EMPLOYEE-NUM PIC 9(06).
05 WS-NAME PIC X(15).05 WS-AGE PIC S9(03) USAGE COMP-3.
05 WS-DESIGNATION PIC X(12).
Anish_DosslinRestricted Page 14 27-Feb-2004
8/2/2019 The SORT Utility v1.1
15/32
SORT UTILITY
Example to convert the copy book CPYSMPL1 to CPYCMPL2:
INREC FIELDS=(1:16,6,
7:1,15,X'021D',
24:29,8,32:X,
33:3C'T')
This statement copies 6 bytes (1:16,6,) from the 16th byte from the input record
(1:16,6,) to the 1st byte (1:16,6,) in the output record. Similarly it copies 15
bytes (,7:1,15,) from the (,7:1,15,) 1st byte in the input record to the 15 bytepositions starting from the 7th position (,7:1,15,) in the output record.
The third condition is a simple insert of the hexadecimal value 021D. As the
corresponding field is a COMP-S field, the value inserted into all the records in theoutput file is -21 as the D in 021D stands for negative value representation.
The fourth condition copies eight bytes from the 19 th byte in the input file to eightbytes starting from the 24th byte in the output file. The fifth condition inserts aSPACE in the 32nd byte. The six condition inserts 3 characters T (33:3C'T') in the
output record starting from the 33rd position (33:3C'T').
If the size of the output record is not the same as the input record, then the LRECLparameter of the output file in the JCL should be made equal to the output record.
Sample JCL
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A,
// MSGCLASS=X,NOTIFY=&SYSUID
//BOKJCL04 EXEC PGM=SORT //SYSOUT DD SYSOUT=* //SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL1,
// DISP=SHR
//SORTOUT DD DSN=DDST.DOSSLA2.FILES.BOK.INREC, // DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA, // DCB=(LRECL=35,RECFM=FB,DSORG=PS)
//SYSIN DD *
INREC FIELDS=(1:16,6,7:1,15,
X'021D',24:29,8,
32:X,33:3C'T')
SORT FIELDS=COPY/*
Anish_DosslinRestricted Page 15 27-Feb-2004
8/2/2019 The SORT Utility v1.1
16/32
SORT UTILITY
2.4 OUTREC
The OUTREC works in the same way as the INREC control statement. It is used to
accomplish these tasks:
Delete or repeat portions of the input record in the same record.
Insert character strings between data fields in a record.
Insert binary zeros. Create a sequence number field.
Convert numeric data to printable format or to another numeric data format.
Perform arithmetic operations (multiply, divide, add, subtract) and minimum and
maximum functions with numeric fields and constants.
Convert data to printable hexadecimal format.
Select, realign, and reorder data fields.
Convert a variable length record input file to a fixed length record output file.
2.5 OUTFIL
The OUTFIL control statement is primarily used to write the output into multipleoutput files. It can also be used as a sort writer to write headers, trailers, etc
2.5.1 OUTFIL SYNTAX
OUTFIL FILES
FILES(XX)
FNAMES=(DD Name1, DD Name2 )ALL
NONEINCLUDE
OMITSTARTREC=n
ENDREC=nSAVE
SPLITOUTREC=(field1, field2 ... )
CONVERTFTOV
HEADER1=(field1, field2 ... )HEADER2=(field1, field2 ... )
TRAILER1=(field1, field2 ... )TRAILER2=(field1, field2 ... )
SECTIONS=(field1, field2 ... )LINES={n | ANSI | (ANSI,n)
NODETAIL
2.5.2 FILES(XX) (Optional) The files sub parameter can be used if there areSORTOFx or SORTOFxx statements and the XX in the FILES(XX) will represent
the number that is contained in the FILES(XX) control statement.
Anish_DosslinRestricted Page 16 27-Feb-2004
8/2/2019 The SORT Utility v1.1
17/32
SORT UTILITY
2.5.3 FNAMES (Optional) Mentions the output DD names that will be the output
files for this FNAMES control. The DD name is of 8 characters. For exampleFNAMES(DDNAME1,DDNAME2,DDNAME2) connects the OUTFIL control
statement with these output files.
2.5.4 INCLUDE/OMIT (Optional) The INCLUDE/OMIT conditions in OUTFIL hasthe same syntax as the INCLUDE COND/OMIT COND control statements. The
only difference is here, COND is not mentioned. The condition will look like
OUTFIL FNAMES=(DDNAME1)
INCLUDE=(1,5,CH,EQ,COBELIX)
2.5.5 STARTREC=n (Optional) The STARTREC specifies the nth record from
which the processing is to be done. All records found before n will beneglected.
2.5.6 ENDREC=n (Optional) The ENDREC specifies the nth record after which no
processing is done and the n+ records are neglected. ENDREC does not mean
that the processing is done for n records. It means that the processing stopsafter the nth record.
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A, // MSGCLASS=X,NOTIFY=&SYSUID
//BOKJCL05 EXEC PGM=SORT //SYSOUT DD SYSOUT=*
//SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL1,DISP=SHR //OPFILE DD DSN=DDST.DOSSLA2.FILES.BOK.OUTREC,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA,
// DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//SYSIN DD *SORT FIELDS=COPYOUTFIL FNAMES=OPFILE,
STARTREC=3,
ENDREC=5/*
The output should have only 3 records, i.e. starting from the 3rd records
(STARTREC=3 and ENDREC=5).The input file is given in figure 1.1. The output is
shown in the picture below.
Anish_DosslinRestricted Page 17 27-Feb-2004
8/2/2019 The SORT Utility v1.1
18/32
SORT UTILITY
2.5.7 SAVE (Optional) The save parameter is used to include the records that
were excluded in other OUTFIL groups. If SAVE is specified for an OUTFILgroup it gets the records that were excluded by the OUTFIL groups that did
not have the SAVE option.
Let us assume that five records were excluded in one of the OUTFIL groups.
Then there excluded records will be considered as input records for the
OUTFIL group that has SAVE option specified in it.
2.5.8 INREC/OUTREC (Optional) The INREC/OUTREC in OUTFIL control
statement is similar to the INREC FIELDS/OUTREC FIELDS control statement.The difference being the FIELDS is not mentioned in the JCL for OUTFIL.
2.5.9 SPLIT - The SPLIT parameter causes the output records to be split among thefiles in the OUTFIL. The records are distributed in rotation to the files
specified in the FILES parameter. The files can be referred to by the xx valuein their SORTOFx or SORTOFxx DD name.
Example:
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A, // MSGCLASS=X,NOTIFY=&SYSUID
//BOKJCL06 EXEC PGM=SORT //SYSOUT DD SYSOUT=*
//SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL1,DISP=SHR //SORTOF01 DD DSN=DDST.DOSSLA2.FILES.BOK.SPLIT01,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA,
// DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//SORTOF02 DD DSN=DDST.DOSSLA2.FILES.BOK.SPLIT02, // DISP=(NEW,CATLG,DELETE),
Anish_DosslinRestricted Page 18 27-Feb-2004
8/2/2019 The SORT Utility v1.1
19/32
SORT UTILITY
// SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA, // DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//SORTOF03 DD DSN=DDST.DOSSLA2.FILES.BOK.SPLIT03, // DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA, // DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//SYSIN DD *SORT FIELDS=COPY
OUTFIL FILES=(01,02,03),
SPLIT,STARTREC=2,
ENDREC=11
/*
As per the above given JCL, the records are distributed to all the three files inrotation. In other words, the first record that is processed is given to the first file,
the second to the second file, the third to the third file and the fourth to the first fileand so on.
DD Name: SORTOF01
DSN: DDST.DOSSLA2.FILES.BOK.SPLIT01
This file contains the 2nd, 5th, 8th and 11th record. As the SPLIT statement isprocessed along the STARTREC and ENDREC statements, the first record is not
processed at all. And the last record to be processed is the 11 th record in the inputfile.
DD Name: SORTOF02
Anish_DosslinRestricted Page 19 27-Feb-2004
8/2/2019 The SORT Utility v1.1
20/32
SORT UTILITY
DSN: DDST.DOSSLA2.FILES.BOK.SPLIT02
DD Name: SORTOF03DSN: DDST.DOSSLA2.FILES.BOK.SPLIT03
Note: Screening of records can be done using the INCLUE/OMIT conditions and also,the records can be reformatted with the OUTREC parameter. Report writingparameters like HEADER, TRAILERn can not be used with the SPILT option in
OUTFIL.
Note: For the same example if SPLIT is not specified, all the output datasets willhave the same records (all the records).
2.5.10CONVERT
CONVERT can be used to convert a variable blocked dataset to a fixed blocked
dataset. In this case the DCB of the output dataset has to have record length which
is four bytes lesser than the variable blocked dataset. The variable blocked file
contains control information like length, etc in the first four bytes of every record.
CONVERT can be used only with OUTREC. In the OUTREC, we need to mentions theconditions so that the 4 control bytes in the VB dataset are neglected.
2.5.11FTOV
FTOV converts a fixed block dataset to a variable block dataset. In this case the DCB
of the output dataset has to have record length which is four bytes more than thevariable blocked dataset.
Example for CONVERT and FTOV:
The following example illustrates the usage of COVERT and FTOV in the same JCL but
in two different steps. The first step copies the records from the input FB datasetwhich have the age field less than 30 and the second step copies the records whose
age field is equal to 22. The input file to the second step is the output file of the first
step.
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A, // MSGCLASS=X,NOTIFY=&SYSUID
//BOKJCL8A EXEC PGM=SORT //SYSOUT DD SYSOUT=*
//SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL1,DISP=SHR
Anish_DosslinRestricted Page 20 27-Feb-2004
8/2/2019 The SORT Utility v1.1
21/32
SORT UTILITY
//SORTOF1 DD DSN=DDST.DOSSLA2.FILES.BOK.CONVERT1, // DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(10,10),RLSE), // DCB=(LRECL=64,RECFM=VB,DSORG=PS)
//SYSIN DD *SORT FIELDS=COPY
OUTFIL FILES=1,FTOV,
INCLUDE=(22,2,BI,LT,30)
/* //BOKJCL8B EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CONVERT1,DISP=SHR //SORTOF1 DD DSN=DDST.DOSSLA2.FILES.BOK.CONVERT2,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),
// DCB=(LRECL=60,RECFM=FB,DSORG=PS) //SYSIN DD *
SORT FIELDS=COPY
OUTFIL FILES=1,INCLUDE=(26,2,BI,EQ,22),OUTREC=(1:5,60),
CONVERT/*
OUTPUT OF FIRST STEP: STEP BOKJCL8A FTOV
OUTPUT OF SECOND STEP: STEP BOKJCL8B CONVERT
Anish_DosslinRestricted Page 21 27-Feb-2004
8/2/2019 The SORT Utility v1.1
22/32
SORT UTILITY
2.6 SORT
The sort card specifies the exact sort criteria. The sort can be used to sort a field (agroup of consecutive byte locations) either in the ascending or descending order. It
can also be used to copy all the records without any change.
2.6.1 SORT CARD SYNTAX
SORT FIELDS=COPY
=(Starting Position, length, format, order of sort, )EQUALS/NOEQUALS
SKIPREC=nSTOPAFT=n
Starting position This is the starting position of the sort field
Length - this is the number of bytes from the starting position
that should be considered for sorting.Format - CH: Character
PD: Packed decimal (COMP-3)ZD: Zoned decimal
BI: Binary (COMP)
Order - Ascending / Descending (A/D)
The same can be repeated for different fields. If there are multiple sort criteria, therecords will be sorted first on the first criteria, then the second criteria and so on.
EXAMPLE
SORT FIELDS=(22,2,BI,D,16,6,ZD,A)
Note: The second SORT criteria refers to 6 bytes because of the copybooklayout. The employee number can be up to 6 digits.
Let us assume that there are 10 records in the file with data as given in the table
below. Only the fields being considered are given below.
Before sort After Sort
Age
Employee
Num Age
Employee
Num
(22,2,BI,D) (16,6,ZD,A) (22,2,BI,D) (16,6,ZD,A)
38 44444 45 12001
22 14141 38 44444
45 12001 33 12130
28 15178 28 15178
26 45365 28 54653
33 12130 26 45365
25 20045 25 20045
22 25155 22 14141
28 54653 22 25155
Anish_DosslinRestricted Page 22 27-Feb-2004
8/2/2019 The SORT Utility v1.1
23/32
SORT UTILITY
22 43522 22 43522
The sort is done so that first all the records that satisfy the first sort criteria aregrouped together. And the second sort is done within those grouped records. In this
example, the records with age field as 22 are grouped and then they are sorted in
the ascending order of the employee number.
2.6.2 EQUALS/NOEQUALS (Optional) The EQUALS option instructs SYNCSORT
to write the records with the same sort key in the same order as in the input file. Ifthe option is NOEQUALS the records may or may not be written in the same order as
the input file. NOEQUALS is the default option.
Example:
SORT FIELDS=(22,2,BI,D,16,6,ZD,A),EQUALS
Let us consider this sort card for the data given below. I have added two morecolumns to the input, name and the designation. I have added two more records
when compared to the previous sample input data. Both are records for the samekey, Age: 22 and Employee Num: 14141.
Before sort
Name AgeEmployeeNum Designation
1,15 (Not Key) (22,2,BI,D) (16,6,ZD,A)29,30 (NotKey)
Getafix 38 44444 SPM
Obelix 22 14141 PL
Unhygenix 45 12001 BM
Asterix 28 15178 PM
Quickfix 26 45365 PA
Vitalstatistix 33 12130 DM
Obelix 22 14141 CCM
Cacafonix 25 20045 PA
Dogmatix 22 25155 SE
Fulliautomatix 28 54653 PM
Obelix 22 14141 PM
Geriatrix 22 43522 SE
The input file has 12 records and three of the have the same sort key. In such a case
as the EQUALS parameter is specified, the records appear in the same order as theinput file. This may not be the same if the option is not specified.
After Sort
Name Age
Employee
Num Designation
1,15 (Not
Key) (22,2,BI,D) (16,6,ZD,A)
29,30 (Not
Key)
Anish_DosslinRestricted Page 23 27-Feb-2004
8/2/2019 The SORT Utility v1.1
24/32
SORT UTILITY
Unhygenix 45 12001 BM
Getafix 38 44444 SPM
Vitalstatistix 33 12130 DM
Asterix 28 15178 PM
Fulliautomatix 28 54653 PM
Quickfix 26 45365 PA
Cacafonix 25 20045 PAObelix 22 14141 PL
Obelix 22 14141 CCM
Obelix 22 14141 PM
Dogmatix 22 25155 SE
Geriatrix 22 43522 SE
2.6.3 SKIPREC=n (Optional) If SKIPREC is specified, the sort process will beonly after n records are skipped from the input file. Is INCLUDE/OMIT parameter is
specified, then these n records are omitted before the INCLUDE/OMIT processingtakes place.
2.6.4 STOPAFT=n (Optional) The STOPAFT parameter specifies the number ofrecords that should be considered for processing. This will be the first n records. IfSKIPREC and/or INCLUDE/OMIT is specified, this will be the first n records after
those processing is completed.
Example 1:
SORT FIELDS=(22,2,BI,D,16,6,ZD,A),SKIPREC=4
Intermediate step (before
SORT and after SKIPREC) After complete process
Age
Employee
Num Age Employee Num
(22,2,BI,D) (16,6,ZD,A) (22,2,BI,D) (16,6,ZD,A)
26 45365 33 12130
33 12130 28 54653
22 14141 26 45365
25 20045 25 20045
22 25155 22 14141
28 54653 22 14141
22 14141 22 25155
22 43522 22 43522
Example 2:
SORT FIELDS=(22,2,BI,D,16,6,ZD,A),
STOPAFT=5
Intermediate step (beforesort) After complete process
Age Employee Num Age Employee Num
Anish_DosslinRestricted Page 24 27-Feb-2004
8/2/2019 The SORT Utility v1.1
25/32
SORT UTILITY
(22,2,BI,D) (16,6,ZD,A) (22,2,BI,D) (16,6,ZD,A)
38 44444 45 12001
22 14141 38 44444
45 12001 28 15178
28 15178 26 45365
26 45365 22 14141
Example 3:
SORT FIELDS=(22,2,BI,D,16,6,ZD,A),SKIPREC=1,
STOPAFT=7
Intermediate step (beforesort) After complete process
Age Employee Num Age Employee Num
(22,2,BI,D) (16,6,ZD,A) (22,2,BI,D) (16,6,ZD,A)
22 14141 45 12001
45 12001 33 12130
28 15178 28 15178
26 45365 26 45365
33 12130 25 20045
22 14141 22 14141
25 20045 22 14141
2.7 MERGE
The MERGE control statement functions almost in the same way as SORT. It can be
used to merge input files and also copy into one output files from multiple input files.
2.7.1 SYNTAX
MERGE FIELDS=(Starting position, length, format, )
(Condition 1, operator, Condition 2 ),FORMAT=formatCOPY
STOPAFTSKIPREC
EQUALS/NOEQUALS
All the parameters work in the same way as they do for SORT. But there are somefactors in applying them.
1. COPY will work for multiple input files only is they or concatenated only with
the SORTIN statement. SORTINx/SORTINxx statements are not taken into
account if SORT FIELDS=COPY is specified.
2. For MERGE other than COPY, the input datasets should have already been
sorted in the same order in which the MERGE sort is going to be performed.
Else the JCL with ABEND with a MAXCC 16. It will also display a message inSYSOUT (OUT OF SEQUENCE SORTINxx)
3. SKIPREC/STOPAFT can be mentioned only for MERGE FIELDS=COPY
Anish_DosslinRestricted Page 25 27-Feb-2004
8/2/2019 The SORT Utility v1.1
26/32
SORT UTILITY
2.7.2 EXAMPLE
The following example uses the copybook CPYSMPL4.01 WS-SAMPLE-CPYBOOK-4.
05 WS-ROLL-NO PIC S9(05) COMP-3.
05 WS-FRST-YR-MRKS PIC S9(04) COMP.
05 WS-SCND-YR-MRKS PIC S9(04) COMP.05 WS-THRD-YR-MRKS PIC S9(04) COMP.05 WS-FRTH-YR-MRKS PIC S9(04) COMP.
SORTIN01
SORTIN02
SORTIN03
Anish_DosslinRestricted Page 26 27-Feb-2004
8/2/2019 The SORT Utility v1.1
27/32
SORT UTILITY
SORTIN04
The JCL looks like the following,
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A, // MSGCLASS=X,NOTIFY=&SYSUID
//BOKJCL09 EXEC PGM=SORT //SYSOUT DD SYSOUT=*
//SORTIN01 DD DSN=DDST.DOSSLA2.FILES.BOK.MERGE.SORTIN01,
// DISP=(SHR,DELETE,DELETE) //SORTIN02 DD DSN=DDST.DOSSLA2.FILES.BOK.MERGE.SORTIN02, // DISP=(SHR,DELETE,DELETE)
//SORTIN03 DD DSN=DDST.DOSSLA2.FILES.BOK.MERGE.SORTIN03, // DISP=(SHR,DELETE,DELETE)
//SORTIN04 DD DSN=DDST.DOSSLA2.FILES.BOK.MERGE.SORTIN04, // DISP=(SHR,DELETE,DELETE)
//SORTOUT DD DSN=DDST.DOSSLA2.FILES.BOK.MERGE.SORTOUT, // DISP=(NEW,CATLG,DELETE),
// SPACE=(TRK,(1,1),RLSE), // DCB=(LRECL=11,DSORG=PS,RECFM=FB)
//SYSIN DD *
MERGE FIELDS=(1,3,PD,A)/*
The output file is the merged and resorted file.
Anish_DosslinRestricted Page 27 27-Feb-2004
8/2/2019 The SORT Utility v1.1
28/32
SORT UTILITY
The merge sort will be successful only if the input datasets are presorted on theorder in which it is going to be merge-sorted.
2.8 SUM
The SUM control statement deletes the duplicate records that have the same sort
key when before they write it into the output file. In other words, the output file willhave only one record corresponding to one sort key. The records with the same key
are processed pair by pair.
If the SUM fields are numeric fields, then the final sum can be written into one recordprovided there is no overflow. The records omitted from the INPUT file can be
optionally written to another file. The SUM control cannot be used in conjunction withSORT FIELDS=COPY.
2.8.1 SYNTAX
SUM FIELDS=(Starting position, length, format, )
(Starting position, length, ),FORMAT=XNONE
XSUM
2.8.2 NONE (Optional) When NONE is specified, the number of records with thesame sort key is reduced to one without summarizing the numeric fields.
2.8.3 XSUM (Optional) When XSUM is specified, the omitted records can be
written to a dataset specified in a SORTXSUM DD statement. The BLKSIZE DCB ofSORTXSUM is determined by the system and will be appropriate.
Points to be noted:
If EQUALS is specified the first record will be retained. If NOEQUALS or
nothing is specified, then the record retained will be determined at random.The EQUALS parameter can be mentioned in SORT statement or can be
passed as a PARM.
The sort key cannot be SUMMED. So the byte locations of the sort key cannot
be a part of the SUM key. The sum fields should not overlap each other.
If arithmetic overflow occurs, then records are not summarized and both the
records are retained. Further if the same key is processed and records are
found, the records will be summarized provided overflow of the summed
fields do not occur. In order to prevent this, the field size (and hence therecord size) may be varied using the INREC statement.
Anish_DosslinRestricted Page 28 27-Feb-2004
8/2/2019 The SORT Utility v1.1
29/32
SORT UTILITY
2.8.4 EXAMPLES
A SAMPLE COPY BOOK: CPYSMPL3
1 WS-SAMPLE-CPYBOOK-3.
05 WS-NAME PIC X(15).
05 WS-EMPLOYEE-NUM PIC S9(07) USAGE COMP-3.05 WS-DATE-OF-BIRTH PIC S9(09) USAGE COMP-3.05 WS-AGE PIC S9(02) USAGE COMP.
05 WS-DESIGNATION PIC X(30).05 FILLER PIC X(04).
1. Example: Complete JCL
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A,
// MSGCLASS=X,NOTIFY=&SYSUID //BOKJCL07 EXEC PGM=SORT
//SYSOUT DD SYSOUT=* //SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL3,DISP=SHR
//SORTOUT DD DSN=DDST.DOSSLA2.FILES.BOK.SUM, // DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA, // DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//SYSIN DD *
SORT FIELDS=(16,4,PD,A,20,5,PD,A)SUM FIELDS=(25,2,BI)
/*
Anish_DosslinRestricted Page 29 27-Feb-2004
8/2/2019 The SORT Utility v1.1
30/32
8/2/2019 The SORT Utility v1.1
31/32
SORT UTILITY
//SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL1,// DISP=SHR
//OUTGE30 DD DSN=DDST.DOSSLA2.FILES.BOK.OUTGE30, // DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA, // DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//OUTLT30 DD DSN=DDST.DOSSLA2.FILES.BOK.OUTLT30, // DISP=(NEW,CATLG,DELETE),
// SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA,
// DCB=(LRECL=60,RECFM=FB,DSORG=PS) //SYSIN DD *
SORT FIELDS=(16,6,ZD,A)
OUTFIL FNAMES=OUTGE30,INCLUDE=(22,2,BI,GE,30)
OUTFIL FNAMES=OUTLT30,INCLUDE=(22,2,BI,LT,30)
/*
This JCL uses OUFIL control to select the output file. Hence the SORTOUT DD is not
necessary. The file is sorted in the ascending order of employee number and thendepending on the condition, the records are distributed among the two output files.
3.2 EXAMPLE 2
A sort JCL to sort the input file in term of the employee number and
then split the records into three files in an equal rotation basis (using
SPLIT). The input file layout is also modified from CPYSMPL1 toCPYSMPL2 using OUTREC control within the OUTFIL.
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A,
// MSGCLASS=X,NOTIFY=&SYSUID //BOKJCL02 EXEC PGM=SORT
//SYSOUT DD SYSOUT=* //SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.CPYSMPL1,
// DISP=SHR //SORTOF1 DD DSN=DDST.DOSSLA2.FILES.BOK.OUT1,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA,
// DCB=(LRECL=35,RECFM=FB,DSORG=PS) //SORTOF2 DD DSN=DDST.DOSSLA2.FILES.BOK.OUT2,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA,
// DCB=(LRECL=35,RECFM=FB,DSORG=PS) //SORTOF3 DD DSN=DDST.DOSSLA2.FILES.BOK.OUT3,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA,
// DCB=(LRECL=35,RECFM=FB,DSORG=PS) //SYSIN DD *
SORT FIELDS=COPYOUTFIL FILES=(1,2,3),
SPLIT,
OUTREC=(1:16,6,7:15C'A',X'025D',24:29,12)/*
Anish_DosslinRestricted Page 31 27-Feb-2004
8/2/2019 The SORT Utility v1.1
32/32
SORT UTILITY
The JCL contains one input file. As the sub parameter used to specify the output filesis OUTFIL FILES we have to use the SORTOFx or SORTOFxx DD statements to
specify the output file names. The LRECL of the output files DCB is 35 depending onthe output copybook structure.
The OUTREC sub-control moves the six bytes from the 16 th position in the input file
to the 6 positions in the output file 1:16,6
Then from the 7th
byte in the output copybook, 15 characters A are inserted using7:15CA. Tip: 15 Spaces can be inserted using 7:15X (in general representation
Starting position:nX).Then a hexadecimal value X025D is inserted which represents -25 in COMP-3
format.Then, 12 bytes from the 29 th position in the input file are copied to the 24th position
in the output file.
3.3 EXAMPLE 3
A sort JCL to sort the input file in the descending order of the employeenumber and then SUM the ages found in file. The file layout is based on
the copybook CPYSMPL1.
//TDDSE6BK JOB (0668,M3),'ANISH DOSSLIN',CLASS=A, // MSGCLASS=X,NOTIFY=&SYSUID
//DELFILES EXEC PGM=IEFBR14 //DD1 DD DSN=DDST.DOSSLA2.FILES.BOK.SORTOUT,
// DISP=(MOD,DELETE,DELETE) // SPACE=(CYL,(10,10),RLSE),
// DCB=(LRECL=60,RECFM=FB,DSORG=PS) //BOKJCL03 EXEC PGM=SORT
//SYSOUT DD SYSOUT=* //SORTIN DD DSN=DDST.DOSSLA2.FILES.BOK.DUPLICTE,
// DISP=SHR
//SORTOUT DD DSN=DDST.DOSSLA2.FILES.BOK.SORTOUT,
// DISP=(NEW,CATLG,DELETE), // SPACE=(CYL,(10,10),RLSE),UNIT=SYSDA, // DCB=(LRECL=60,RECFM=FB,DSORG=PS)
//SYSIN DD *
SORT FIELDS=(16,6,ZD,D)SUM FIELDS=(22,2,BI)
/*
The sort is done on the employee number found from the 16th position and is 6 byteslong.
Then the age field (2 bytes from the 22nd position) is summed up. The SUMming willnot work if the sort criteria are not specified because the adjacent records might not
have the same key. In order for the SUM condition to work all the sorted similar keyfields should be adjacent to each other.
The STEP given in dark gray can be used to delete the output file using a step, so
that we do not have to manually delete the output file (which was created in theprevious run) when we want to run the job again. If there are more than one output
files then add a DD corresponding to the other datasets that have to be deleted.It is better to code the DCB and SPACE parameters so that the step will function
properly whether the dataset exists or not.