Vous êtes sur la page 1sur 19

SORT

Definition:
A sort rearranges the record in a specified sequence in a dataset. The different control
statements for sort are as follows:
Control Statements:
1. SORT FIELDS
2. SUM FIELDS
3. OUTFIL
4. INCLUDE
5. OMIT
6. MERGE
7. INREC
8. OUTREC
9. SKIPREC
10. STOPAFT
11. REPORT
A sample SORT JCL structure is as follows:

*****************************************************************
//STEP01 EXEC PGM=SORT
//SYSPRINT
DD SYSOUT=*
//SYSOUT
DD SYSOUT=*
//SORTIN
DD DSN=<input file>, DISP=SHR
//SORTOUT
DD DSN= =<output file>,
//
DISP= (NEW, CATLG, DELETE),
//
UNIT=DISK
//SORTOFXX
DD DSN =
//SORTXSUM
DD DSN =
//SORTWKNN DD DSN = .
//SYSIN DD *
*****************************************************************

ID: 275452

Let us consider a sample input as shown below:

SORT FIELDS:
Syntax:
//SYSIN DD *
SORT FIELDS = (1, 5, CH, A)
/*
In the above syntax of SORT, sorted the records depended on the key which is provided.
(1, 5, CH, A) -- It represents key starts at col 1 and its length is 5.
CH Character.
A Ascending Order.
The output for the above conditions is as follows:

ID: 275452

SUM FIELDS:
Syntax 1:
//SYSIN DD *
SORT FIELDS = (1, 5, CH, A)
SUM FIELDS = NONE
/*

The above syntax eliminates duplicate records.


The output dataset eliminates duplicate records as shown below:

Syntax 2:

//SYSIN DD *
SORT FIELDS=(1,5,CH,A)
SUM FIELDS=(36,4,ZD)
/*
In the above syntax first the SORT FIELDS sorts the records from 1st position of length 5
in ascending order . SUM FILEDS gives a unique record for all the duplicates present in the
input data and totals for the specified summary fields.

Input:

ID: 275452

Here in the above input file the first record A1000 repeated twice. So at the column 36 of
length 4 value will be summed up and the duplicate (second record) will be eliminated.
Output:

SPLITTING THE INPUT FILE:

ID: 275452

Sort provides to split the input file into one or more output files depending on the conditions.
Syntax:
//SORTOF1 DD DSN=DATASET1,
//
DISP = (NEW, CATLG, DELETE), UNIT=SYSDA,
//
SPACE = (TRK, (10, 5), RLSE)
//SORTOF2 DD DSN=DATASET2,
//
DISP = (NEW, CATLG, DELETE), UNIT=SYSDA,
//
SPACE = (TRK, (10, 5, RLSE)
//SYSIN DD *
SORT FIELDS=COPY
OUTFIL FILES=1, INCLUDE= (7, 7, CH, EQ, C' SAMPATH ')
OUTFIL FILES=2, INCLUDE= (7, 3, CH, EQ, C' RAM )
OUTFIL FILES=3,SAVE
/*
Here
SORT FIELDS = COPY SPECIFIES THAT, COPY THE RECORDS FOR THAT
GIVEN CONDITION
INCLUDE (condition) to select records based on the condition specified.

According to the input file here the data will be taken from 7st position with
length 5 and also from 7th position with length 3 will search for SAMPATH and RAM in
the input file.
1.

If the condition specified in INCLUDE satisfies then the record will be copied on to
the specified dataset which is defined in SORTOF1, because the condition we
specified on OUTFIL FILES = 1.
2. If the condition specified in INCLUDE satisfies then the record will be copied on to
the specified dataset which is defined in SORTOF2, because the condition we
specified on OUTFIL FILES = 2.
3. SAVE collects the records which are not selected by the OUTFIL statements. Here in
this case OUTFIL FILES = 1 and OUTFIL FILES = 2 selects the records with
SAMPATH and RAM and the remaining records are present in OUTFIL FILES = 3
The following are the two outputs :

ID: 275452

For multiple conditions, generally used options are AND and OR:
1.
2.
3.
4.
5.
6.

AND - all conditions must be met


OR - any one of several conditions must be met
ANDs are evaluated before ORs
Expressions inside parentheses are evaluated first
The number of tests is limited only by available storage
Characters '&' and '|' can be used for 'AND' and 'OR' respectively

Syntax:

//SYSIN DD *
SORT FIELDS=COPY
INCLUDE COND=(7,7,CH,EQ,C'SAMPATH',OR,7,7,CH,EQ,C'RAM')
/*

Explanation: In this condition, output dataset contains only the records from position 7 of
length 7 having values as mentioned in the condition.
Input:

ID: 275452

Output:

COPYING THE ELIMINATED DUPLICATE RECORDS INTO ANOTHER


DATSET:
Syntax:
//SORTIN DD
DSN=DATASET1, DISP=SHR
//SORTOUT DD DSN=DATASET2,
//
DISP= (NEW, CATLG, DELETE),
//
UNIT=SYSDA,
//
SPACE= (CYL, (10, 5), RLSE),
//
DCB= (RECFM=FB, LRECL=50, DSORG=PS)
//SORTXSUM DD DSN=DATASET3,
//
DISP= (NEW, CATLG, DELETE),
//
UNIT=SYSDA,
//
SPACE= (CYL, (10, 5), RLSE),
//
DCB= (RECFM=FB, LRECL=50, DSORG=PS)
//SYSIN DD *
SORT FIELDS= (1, 5, CH, A)
SUM FIELDS=NONE,XSUM
/*

SUM FIELDS = NONE Eliminates duplicate records.


XSUM This option copies all the records eliminated in the sort process and then
copies all those records into another dataset defined at SORTXSUM step.
The following output explains about XSUM:
Input File:

ID: 275452

Output File:
For SUM FIELDS = NONE

For XSUM:

Here we can clearly identify that, record A100 and G2110 has multiple occurrences. So
the duplicates are eliminated and placed into another dataset.
OMIT Condition:
The OMIT control statement discards the records of a particular condition. This
condition is used to delete unwanted records.
Syntax:
//SYSIN DD *
SORT FIELDS= (1, 5, CH, A)
OMIT COND= (15, 7, CH, EQ, C'CHENNAI')
/*

ID: 275452

Output: The output for the condition will result in discarding all the records having the
records with CHENNAI.

MERGE Condition:
Merge condition is used to merge one dataset which is in sorted order with another dataset
Input 1:

ID: 275452

Input 2:

Output:

INREC Condition:
INREC control statement reformats the file before sorting or merging. INREC
adds, deletes or reformats the records before it is sorted or merged, so the performance of
sort will be increased.
Syntax:

ID: 275452

//SYSIN DD *
SORT FIELDS=COPY
INREC FIELDS=(1:1,5,10:15,4)
/*

Explanation:
In the above syntax, the sort copies the records from input and reformats the input
file. Here the sort selects records from position 1 of length 5 of the input file and
keeps that records from position 1 in the output file. Similarly selects the records
at position 15th of length 4 and places that record at 10th position in the output file.
The following details will give the detail information about INREC control
statement:
Input:

Output:

According to the syntax as shown above the input file has been reformatted by
selecting only those records present at 1 of length 5 and at position 15 of length 4.

OUTREC
Condition:

OUTREC control statement reformats the file after sorting or merging. OUTREC also
adds, deletes, or reformats the records after sorting.
Syntax:
ID: 275452

//SYSIN DD *
SORT FIELDS=COPY
OUTREC FIELDS=( 1,25,X,27:C'-->CITY')
/*

Explanation:
In the above syntax, the sort copies the records from input and reformats
the input file. Here the sort copies the records from position 1 to 25 of the input
file and place those records in the output file. Here X represents one space. As we
discussed that OUTREC reformats the data after sorting. So here at column 27
one extra record will be added. The following details will give the detail
information about OUTREC control statement:
Input:

Output:

According to the syntax as shown above the input file will be added with an extra
column from 27th position.
Reformatting the file using BUILD:
Using BUILD we can reformat each record by specifying its items one by one.
BUILD gives the flexibility to reformat the records as per the requirement and we can
arrange records in any order. We can rearrange, delete and insert fields or any constants
symbols into the record.

ID: 275452

Syntax:
//SYSIN DD *
SORT FIELDS = (1,5,CH,A)
OUTREC BUILD = (5,20,5C'*',5X,7,19,C'-->NEW CITY')
/*

INPUT:

OUTPUT:

Explanation:
For the above sort card, the SORTOUT contains the following:
1. In the output 1-20 contains, the records from input file from 5th position of length 20
are selected.
2. Next from 21-25th position, output file will have 5 asterisks.
3. From 26-30, output file contains 5 spaces
ID: 275452

4. And at 31st position, the records from the input file from 7th position of length 19 are
selected
5. At 50th position, the output contains characters NEW CITY.
Reformatting the file using OVERLAY
Using OVERLAY, you reformat each record by specifying just the items that
overlay specific columns. OVERLAY lets you change specific existing columns without
affecting the entire record.
Syntax:
//SYSIN DD *
SORT FIELDS=(1,5,CH,A)
OUTREC OVERLAY=(7:7,3,TRAN=UTOL)
/*
INPUT:

OUTPUT:

Explanation:
In output positions at position 7 to 10, the characters from input positions 7-10 with any
uppercase letters translated to lowercase letters.
In all other output positions, the corresponding unchanged input characters.
Reformatting the file using FINDREP:
FINDREP reformates the records by replacing characters or constants anywhere in
the record with another characters or constants. The name itself says FIND and REPLACE.
Syntax:

ID: 275452

//SYSIN DD *
SORT FIELDS=COPY
OUTREC FINDREP=(IN=(C'RAM'),OUT=C'MOHAN')
/*

Explanation:
In the above syntax, whenever the characters RAM found in the input record, then
it will be replaced by MOHAN in the output records.
NOTE: If the characters in the output file exceed the input file, the remaining bytes will
be shifted to the right

INPUT:

OUTPUT:

For Multiple replacements:


Syntax:

//SYSIN DD *
SORT FIELDS=COPY
OUTREC FINDREP=(INOUT=(C'RAM',C'MOHAN',CSAMPATH,CKUMAR)
/*
ID: 275452

Explanation:
In the above syntax, whenever the characters RAM found in the input record, then
it will be replaced by MOHAN and SAMPATH will be replaced by KUMAR in the
output records.

IF THEN clause:
Using IFTHEN clause, we can reformat the records in different ways by specifying the
criteria we need to apply for the records. IFTHEN clauses let you use sophisticated
conditional logic to choose how different record types are reformatted. We can use IF
THEN in 5 different ways:

WHEN=INIT: Use one or more WHEN=INIT clauses to apply BUILD,


FINDREP or OVERLAY items to all of your input records.
WHEN=GROUP: Use one or more WHEN=GROUP clauses to propagate
fields, identifiers and sequence numbers within groups of records.
WHEN=(logexp): Use one or more WHEN=(logexp) clauses to apply
BUILD, FINDREP or OVERLAY items to the subset of your records that
satisfy a specified logical expression.
WHEN=ANY: Use a WHEN=ANY clause after multiple WHEN=(logexp)
clauses to apply additional BUILD, FINDREP or OVERLAY items to your
records if they satisfied the criteria for any of the preceding
WHEN=(logexp) clauses.
WHEN=NONE: Use one or more WHEN=NONE clauses to apply BUILD,
FINDREP or OVERLAY items to your records that did not meet the criteria
for any of the WHEN=(logexp) clauses.

Syntax:
//SYSIN DD *
SORT FIELDS=COPY
OUTREC IFTHEN=(WHEN=(7,7,CH,EQ,C'SAMPATH'),
BUILD=(1,24,X,26:C'OMNI')),
IFTHEN=(WHEN=(7,6,CH,EQ,C'PRASAD'),
BUILD=(1,24,X,26:C'PBIS')),
IFTHEN=(WHEN=(7,3,CH,EQ,C'RAM'),
BUILD=(1,24,X,26:C'ACATS')),
ID: 275452

IFTHEN=(WHEN=(7,7,CH,EQ,C'SANDEEP'),
OVERLAY=(7:7,7,TRAN=UTOL))
/*

Explanation:
1. From the above syntax, WHEN condition selects the mentioned selection criteria and
formats the output record by adding extra records at column position 26 using BUILD
2. For OVERLAY, record will be translated from uppercase to lowercase.
Input:

Output:

As shown in the output, the highlighted records at that last column were added for the
condition with BUILD control statement and for OVERLAY (6th record- 2nd column) has been
converted into lower case.

SKIPREC and STOPAFT:


SKIPREC and STOPAFT options are used to reduce the number of records to be
processed.
Syntax:
SKIPREC = N: Specifies that skip first n records before performing the sort.
STOPAFT = N: Specifies that read only n records from the input file.

ID: 275452

NUMERIC EDITING:
INREC, OUTREC and OUTFIL provides numeric editing. Sometimes if we
want to mask the date field in dd/mm/yyyy format, then DFSort provides a feature called
EDIT.
Syntax:
//SYSIN DD *
SORT FIELDS=COPY
OUTREC FIELDS=(1,26,27:27,8,ZD,EDIT=(TT-TT-TTTT))
/*
Input:

Explanation:
Let us consider from the above input file, the date in the last column has to be
masked. So in the above syntax the output file describes as follows:
1. The records from columns 1 to 26 will be copied and added in the output file.
2. At 27th column of length 8 (date field) will be edited (TT-TT-TTTT)
Output:

ID: 275452

ID: 275452

Vous aimerez peut-être aussi