Anda di halaman 1dari 18

IBM SORT For Beginners

IBM SORT Code Snippets


This document is structured in a way such that there are code snippets (only the SORT card part) given for the various things you can achieve with a JCL Sort card. These code snippets can be readily used within your code. SDG 2/28/2009

Table of Contents
Introduction..............................................................................................................3 1) Search When Position Not Known

..................................................................................................................................4 2) 3) 4) 6) 7) 8) Skip Header Records........................................................................................4 Sort Certain Number Of Records.....................................................................5 Specifying the Format. ....................................................................................5 Using the INREC Statement.............................................................................6 Using the OUTREC Statement.........................................................................7 Using the Sum Fields Statement

5) Using Conditional Include Statements..............................................................6

..................................................................................................................................8 9) Split data on the basis of certain conditions

..................................................................................................................................9 10) Making Reports............................................................................................10 Points to Note.........................................................................................................12 Using The Edit Fields:...........................................................................................12 Formatting of the Date and Time Fields :..............................................................13 Accumulating Data at a page level........................................................................13 Accumulating data at the end of Report................................................................14 Dividing the report based on the values in certain fields.......................................15 Points to Note : ......................................................................................................16

Introduction
Following are the points within scope of this document: 1) 2) 3) 4) 6) 7) 8) 9) 10) Search When Position Not Known Skip Header Records. Sort Certain Number Of Records. Specifying the Format. Using the INREC Statement Using the OUTREC Statement Using the Sum Fields Statement Split data on the basis of certain conditions : Making Reports

5) Using Conditional Include Statements

All the SORT cards within this document use a flat file with an LRECL of 80. The record structure of the file is given below. Employee No Employee Name City FILLER Salary Remarks 6 25 10 1 5 33

1)

Search When Position Not Known

To Search for a particular string within a file when the exact position of that string within a record is not known and to write that record in the Output file the following SORT card can be used.
SORT FIELDS=COPY INCLUDE COND=(1,80,SS,EQ,C'FIND STRING')

The Sort card for this will need information like range of columns within which you want the search to be performed. To search in the whole file you can give 1 to the LRECL of the file as done above. We can also replace the EQ parameter by NE which will fetch us all the records which dont have the specified string.

2)

Skip Header Records.

When you want to sort a file there might be some header records which you may want to skip. This can be done with the following sort card.
SORT FIELDS=COPY, SKIPREC=9

The above sort card will copy the Input file to the Output file but will skip the first 9 records. Please note that SKIPREC is a parameter of the SORT verb and not a statement.

3)

Sort Certain Number Of Records.

When you want to sort a file you might need only a certain number of records in the Output file without having any specific condition. This can be done with the following sort card.
SORT FIELDS=COPY, STOPAFT=1000

The above sort card will copy the Input file to the Output file but will copy only the first 1000 records. Please note the STOPAFT is a parameter of the SORT verb and not a statement. These above two parameters i.e. SKIPREC and STOPAFT can also be combined together
SORT FIELDS=COPY, SKIPREC=9,STOPAFT=1000

The above sort card will skip the first 9 records and copy the next 1000 records to the Output file.

4)

Specifying the Format.


SORT FIELDS=(32,10,CH,A,43,5,CH,D)

The above sort card sorts the Input file first on the CITY field starting from position 32 in the ascending order and then on the SALARY field starting from position 43 in the descending order. As both the field type is Character we can also give the same Sort card in the following manner
SORT FIELDS=(32,10,A,43,5,D),FORMAT=CH

This is only possible when all the fields specified in the sort card are of the same type, if its of different types we have to specify individual format parameters for each of the fields.

5)

Using Conditional Include Statements


SORT FIELDS=(32,10,A,43,5,D),FORMAT=CH INCLUDE COND=((32,10,CH,EQ,C'PUNE',OR,43,5,CH,LE,C'05000'),AND, (1,80,SS,EQ,C'FIND STRING'))

The above SORT cards copies only those records from the Input file which matches the condition specified in the Include Condition. The condition states that the record coming on the Input file whose city is PUNE and FIND STRING is there on the record OR who has a salary of less than 5000 and FIND STRING is there on the record is to be included in the Output file.

6)

Using the INREC Statement

To filter/reformat the data coming on the input file and then sorting the filtered/reformatted data and writing it to the Output file 'INREC' can be used. This increases the performance of the sort as the records are first filtered/reformatted. Hence the whole file is not required to be sorted and only the filtered/reformatted fields are sorted depending on the condition. The sort in this case is performed on the Output file structure and not on the Input file structure.
INREC FIELDS=(1:1,6, 10:32,10, 21:43,5) SORT FIELDS=(10,10,CH,A)

In the above SORT card we include only the EMPLOYEE NO, CITY and the SALARY fields in the Output file. The file is then sorted on the basis of CITY Please note that the Output record structure is being . used for the SORT. Along with reformatting the values present in the Input file we can also add characters to the records using the INREC statement.
INREC FIELDS=(1:32,10, 15:1,6, 23:C'**', 25:43,5) SORT FIELDS=(1,10,CH,A)

The above Sort card will put ** at the position 23 in the formatted data of Output file before sorting. This will be done for all the records coming on the Input file.

Along with this the EDIT parameter can also be used to format the field values coming from the Input file. Some of the examples using the EDIT parameter are
INREC FIELDS=(1:32,10, 15:1,6, 23:C'**', 25:43,5,EDIT(TTT-TT) SORT FIELDS=(1,10,CH,A)

In the above sort card if the value at position 43 on the Input file is 00000 then it will be displayed as 000-00 on the Output file. We can have a comma (,) or any other character in place of the hyphen (-).
INREC FIELDS=(1:32,10, 15:1,6, 23:C'**', 25:43,5,EDIT(IIIIT) SORT FIELDS=(1,10,CH,A)

In the above Sort card if the value at position 43 on the Input file is 00001 then on the Output file 1 will be displayed. The preceding zeros are removed.

7)

Using the OUTREC Statement

To sort the whole input file and then filter/reformat the sorted data and writing it to the Output file 'OUTREC' can be used. Here the sort is done on the Input file Structure and not on the Output file structure.
SORT FIELDS=(1,6,CH,A) OUTREC FIELDS=(1:1,6, 10:32,10, 21:43,5)

This is similar to the INREC statement the only difference being that the sorting is performed on the Input Record Structure. The rest of the formatting features which are applicable to the INREC statement are also applicable to the OUTREC statement. When we use the INREC and the OUTREC statement the columns which the user has formatted comes as desired. If any value in between are not populated by the user then it is replaced with the blank character (Hex value: 40). But after the end of the user formatted columns the rest of the space till LRECL contains the Invalid/Non-display characters (HEX value: 00). This might cause problems in case if the Output file is going to be used in any COBOL programs. In order to overcome this problem we pass a blank character 7

(Hex value: 40) as the last column in the record of the Output file. Hence as a result all the blank values within the file have a HEX value as 40.
SORT FIELDS=(1,10,CH,A) OUTREC FIELDS=(1:10,10, 11:1,5, 80:1x)

The LRECL in the above case is 80 and hence a blank character has been moved to that particular column so that there are no Invalid/Non-Display characters in the Output files.

8)

Using the Sum Fields Statement

The Sum Fields statement can be used to sum the values of a particular field for all those records which have the same values for the fields given in the SORT card condition. A simple SUM FIELDS statement will look like.
SORT FIELDS=(32,10,CH,A) SUM FIELDS=(43,5,ZD)

The above SORT card will sum the values of the Salary field of all the records which have the same value for the City field. Thus on the Output file there will only be one record written per City. The City name and the Salary (cumulative value for that city) will be written and the rest of the fields will have the values of the first occurrence of that particular City. Here ZD means Zoned Decimal and the Sum Fields statement requires the type of a field for it to sum. It can also be Packed Decimal (PD). When using the Sum Fields statement you might observe the output always comes in the Signed format. In order to circumvent this to display the Output in the human readable format we can reformat the Output of the Sum filed statement using the EDIT Statement. .
SORT FIELDS=(32,10,CH,A) SUM FIELDS=(43,5,ZD) OUTREC FIELDS=(1:32,10, 15:43,5,ZD,EDIT(TTT-TT), 80:1X)

In the above Sort card the sum of fields will be formatted with the help of EDIT Statement and hence a value of 2500{ will look like 250-00.

9) Split data on the basis of certain conditions

The sort Program can also be used to split the data coming on the Input file into more than one output file depending on certain conditions
//STEP1 EXEC PGM=SORT //SORTLIB DD DSN=SYS1.SORTLIB,DISP=SHR //SYSPRINT DD SYSOUT=* //* //SORTIN DD DSN=input data set,DISP=SHR //* //SORTOF01 DD DSN=output data set 1, // DISP=(,CATLG), // DCB=*.SORTIN, // SPACE=(CYL,(10,10),RLSE) //* //SORTOF02 DD DSN=output data set 2, // DISP=(,CATLG), // DCB=*.SORTIN, // SPACE=(CYL,(10,10),RLSE) //* //SORTOF03 DD DSN=output data set 3, // DISP=(,CATLG), // DCB=*.SORTIN, // SPACE=(CYL,(10,10),RLSE) //* //SYSIN DD * SORT FIELDS=(1,6,CH,A) OUTFIL FILES=01, INCLUDE=(32,10,CH,EQ,C'PUNE') OUTFIL FILES=02, INCLUDE=(32,10,CH,EQ,C'BOMBAY') OUTFIL FILES=03, INCLUDE=(32,10,CH,EQ,C'DELHI') //*

The above JCL and its corresponding SORT card divide the Input File into 3 files. Output data set 1 will contain all records with PUNE as the City. Output data set 2 will contain all records with BOMBAY as the City. Output data set 3 will contain all records with DELHI as the City. Rest of the records will be dropped.

In the above SORT card please note that the suffixes of the Output file have to be supplied by the user. As shown above we have specified the Suffixes as OUTFIL FILES=01, OUTFIL FILES=02 and OUTFIL FILES=03 and hence the DD Name of the Output files are SORTOF01, SORTOF02 and SORTOF03. So if we change to say OUTFIL FILES=YY then the DD Name will be SORTOFYY

10)

Making Reports

The SORT Program can also be used to prepare Formatted Reports. This is one of the most powerful features of SORT and also one of the more under utilized once. We can save a lot of time by having the SORT program make reports for us instead of writing COBOL programs for it. We will be using the same record Structure as shown above to make the Report.
SORT FIELDS=(1,6,CH,A) OUTFIL OUTREC=(5:1,6, 21:7,25, 50:32,10, 65:43,5, 132:1X), HEADER2=(02:'REPORT NO:', 14:'RPT 1', 40:'COMPANY NAME', 60:'DATE:', 66:&DATE, 1/,40:'NAME OF REPORT', 60:'PAGE:', 66:&PAGE, 2/,05:'EMPLOYEE NO', 21:'EMPLOYEE NAME', 50:'CITY', 65:'SALARY', 1/), LINES=35

The report will look like


REPORT NO: 05/14/02 1 EMPLOYEE NO SALARY EMPLOYEE NAME CITY RPT 1 COMPANY NAME NAME OF REPORT DATE: PAGE:

10

000120 02000 000121 00300 000122 05000 000123 12000 000124 11000 000125 02000 000126 00500 000127 07000 000128 00700 000129 02000 000130 05000 000131 00300

NAME1 SURNAME1 NAME2 SURNAME2 NAME3 SURNAME3 NAME4 SURNAME4 NAME5 SURNAME5 NAME6 SURNAME6 NAME7 SURNAME7 NAME8 SURNAME8 NAME9 SURNAME9 NAME10 SURNAME 10 NAME11 SURNAME 11 NAME12 SURNAME 12

PUNE DELHI PUNE BOMBAY BOMBAY DELHI MOHALI PUNE MOHALI BOMBAY PUNE ARKONNAM

11

Points to Note
The report which is generated will be 133 characters long. In this case we put 1X at position 132 and not at the last position i.e. 133 as at the last position the new line character is present which is useful while printing the report and helps the printer to go to the new line. The &DATE will get the system date and display in the format mm/dd/yy. The LINES=35 will make the report to have a page break after every 35 lines. Thus after every 35 lines it will display the header section and then continue with the rest of the records. The &PAGE will calculate the page no i.e. after every 35 lines have been displayed the page no will be incremented by one. The 1/ and 2/ signifies the amount of lines to be skipped before the next line is to be written and till / doesnt arrive it is assumed to be the same line. The record format for the output record structure should be FBA. The Output DD should look like.

DSN=Output-file-name, DISP=(,CATLG,DELETE),SPACE=(CYL,(5,5),RLSE), DCB=(LRECL=133,BLKSIZE=1330,RECFM=FBA)

Using The Edit Fields:


We can use the EDIT parameters as shown in the INREC and OUTREC examples to format the Output record structure as the user may decide. Example: The salary field could be reformatted to remove the leading zeros.
65:43,5,ZD,EDIT(IIIIT)

This statement will cause the Salary to be displayed without the leading zeros. That is if the salary amount is 00200, it will be displayed as 200. All the rest of the EDIT options can be used in the similar way.

12

Formatting of the Date and Time Fields :


The date and time field which are displayed on the report can be formatted according to the user needs. The default values are displayed using &DATE and &TIME. If the system date 1st May 2002 and the System time is 1:30:00 PM then
&DATE &DATE(YMD/) &DATE(MYD-) &DATE(4DM-) &DATE(D4M/) &TIME &TIME(24:) &TIME(12:) &TIME(24.) &TIME(12-) : : : : : : : : : : 05/01/02 02/05/01 05-02-01 2002-01-05 01/2002/05 01:30:00 13:30:00 01:30:00 PM 13.30.00 01-30-00 PM

Accumulating Data at a page level.


Often while making of the reports there is a need to sum the data at page level. This can be done using the Trailer2 parameter.
TRAILER2=(45:'SALARY SUM : ', 65:TOTAL(43,5,ZD,EDIT(IIITT)),

The above clause will sum the salary values and give that total at the End of the page. So in the SORT card above it will add the Salary values of 35 employees and give the Total at the end of the page. Please note that the Salary values coming on the Input file can be formatted as required. This is because the data for the summation is taken from the Input file..

13

Accumulating data at the end of Report.


We usually also have the need to accumulate the data for some particular fields at the end of the report. This can be done using the Trailer1 parameter
TRAILER1=(40:'TOTAL SALARY SUM : ', 65:TOTAL(43,5,ZD,EDIT(IIITT)),

The above clause will sum up all the values coming in the Salary fields and will display it only once when all the records in the report has been displayed. Other parameters which can be used with Trailer1 and/or Trailer2. The other parameters which can be used in Trailer1 and Trailer2 are COUNT : Gives the count of records. TOTAL : Totals the values of records MAX : Gives the Maximum value of those records. MIN : Gives the Minimum value of those records. AVG : Gives the Average value for those records. All these parameters can be used in Trailer1 and/or Trailer2. If we add SUB in front of all the above parameters we get Running totals for those value. In the sense if we have page level differentiation then SUBTOTAL will give the total of the values coming on the Input file for that particular page. As a result of this in Trailer1 SUBTOTAL and TOTAL will give the same values. So is it for all the other parameters.

14

Dividing the report based on the values in certain fields.


The is usually a case needed when we have to report cumulative values not based on pages but based on the values in certain fields. Like suppose we have to give the cumulative value for the salaries paid to employees in one particular city. This can be done using the Section clause. In this we signify which field is to be checked. The check done is a binary check and the report header is repeated only if there is a difference in the previous value and the present value. Sort Card for getting the Cumulative Salary for a City.
SORT FIELDS=(32,10,CH,A) OUTFIL OUTREC=(5:1,6, 21:7,25, 50:32,10, 65:43,5,ZD,EDIT(IIITT), 132:1X), SECTIONS=(32,10,SKIP=P, HEADER3=(02:'REPORT NO:', 14:'RPT 1', 40:'COMPANY NAME', 66:'DATE:', 72:&DATE(YMD-), 1/,40:'REPORT FOR :',32,10, 66:'PAGE:', 72:&PAGE, 2/,05:'EMPLOYEE NO', 21:'EMPLOYEE NAME', 50:'CITY', 65:'SALARY', 1/), TRAILER3=(60:'------------',1/, 35:'SALARY SUM FOR ',32,10,' : ', 65:TOTAL(43,5,ZD,EDIT(IIITT)),1/, 60:'------------', 1/)), TRAILER1=(60:'************',1/, 40:'TOTAL SALARY SUM : ', 65:TOTAL(43,5,ZD,EDIT(IIITT)),1/, 60:'************', 1/)

The Section clause above asks the Sort program to check from 32 for 10 characters which is the position of the City field. For any change in the City the total for that City is written and the rest of the Page is skipped. The new page starts with the report header and continues in the same vein. Where Report For : is written in front the City name appears in the Report Header as also for Section Wise Summary Total. 15

Points to Note :
When the section statement is used we have to use HEADER3 and TRAILER3 for getting the Header and footer of each Section. In the Section Statement we specify the SKIP parameter which will skip a page when a section is over as done above or skip number of Lines you specify. This can be given as SKIP=4L which will skip 4 lines before it starts the Header of the Next Section. To display the value of the field for which a section is being written we can use the columns of the field. As done above the city is present in column 32. Hence we have used it for giving the title to the report and also while displaying the accumulated salary for a particular city. The report will be displayed in the format as shown in the Next Page. If along with the Section wise break up we also need the page wise break up we can use the Header2 and Trailer2 statements. Please remember to specify the line no and it will only be useful in case the SKIP parameter has been supplied lines as a parameter.

16

REPORT NO: RPT 1 DATE: 02-05-14 PAGE: 1 EMPLOYEE NAME NAME12 SURNAME 12

COMPANY NAME REPORT FOR :ARKONNAM CITY ARKONNAM ------SALARY SUM FOR ARKONNAM : ------COMPANY NAME REPORT FOR :BOMBAY EMPLOYEE NAME NAME4 SURNAME4 NAME5 SURNAME5 NAME10 SURNAME 10 CITY BOMBAY BOMBAY BOMBAY ------SALARY SUM FOR BOMBAY : ------COMPANY NAME REPORT FOR :DELHI EMPLOYEE NAME NAME2 SURNAME2 NAME6 SURNAME6 CITY DELHI DELHI -------SALARY SUM FOR DELHI : --------

EMPLOYEE NO SALARY 300 000131

----300 ----REPORT NO: RPT 1 DATE: 02-05-14 PAGE: 2

EMPLOYEE NO SALARY 000123 12000 000124 11000 000129 2000 ----25000 ----REPORT NO: RPT 1 DATE: 02-05-14 PAGE: 3

EMPLOYEE NO SALARY 000121 300 000125 2000 ---2300 ----

17

REPORT NO: RPT 1 DATE: 02-05-14 PAGE: 4 EMPLOYEE NAME NAME7 SURNAME7 NAME9 SURNAME9

COMPANY NAME REPORT FOR :MOHALI CITY MOHALI MOHALI -------SALARY SUM FOR MOHALI : -------COMPANY NAME REPORT FOR :PUNE EMPLOYEE NAME NAME1 SURNAME1 NAME3 SURNAME3 NAME8 SURNAME8 NAME11 SURNAME 11 CITY PUNE PUNE PUNE PUNE -------SALARY SUM FOR PUNE : -------******** TOTAL SALARY SUM : ********

EMPLOYEE NO SALARY 500 700 000126 000128

---1200 ---REPORT NO: RPT 1 DATE: 02-05-14 PAGE: 5

EMPLOYEE NO SALARY 000120 2000 000122 5000 000127 7000 000130 5000 ---19000 ---**** 47800 ****

18

Anda mungkin juga menyukai