Sunday, 28 July 2013

JCL SORT: DFSORT Syntax, Examples and Common SYSIN Cards


JCL SORT DFSORT SYSIN cards for ascending descending and multi-field sorts
JCL SORT DFSORT SYSIN cards for ascending, descending and multi-field sorts.


Sequential files are still commonly used in mainframe shops to store transaction records. Then, before the records in these files can be processed, they often need to be sorted or merged into an appropriate sequence. For instance, sales transactions may need to be sorted into salesperson, customer, or item number sequence before they can be used to prepare reports or update master files. 

To do this sorting or merging, you use the sort/merge utility that comes with OS/390. As you will see, you normally use JCL to run this utility as a standalone program. However, you can also use this utility to do an internal sort or internal merge within an application program. 

The JCL requirements for sorting


Sort the records in a file, the sort/merge utility (or program) reads the records from one or more input files, sorts them based on the data in one or more control fields (or sort keys), and writes the sorted records to an output file. 

If you want to sort two or more files at the same time, you can concatenate them like this: 

//SORTIN DD DSNAME=RC01.INVOICE.TRANS.APRIL,DISP=SHR 
//       DD DSNAME=RC01.INVOICE.TRANS.MAY,DISP=SHR
//       DD DSNAME=RC01.INVOICE.TRANS.JUNE,DISP=SHR

This assumes of course that the control field is in the same location in all of the files.

Although the JCL for this job step provides for only one work file, you can provide for more than one. Either way, the space allocation for the work files should be about twice the combined sizes of the input files. That extra space lets the sort/merge program work more efficiently while it does the sorting.


JCL Sort Utility
JCL Sort.

The DD statements required by a sort

DD name Use
SORTLIB A partitioned data set that contains the modules required by the sort/merge utility. Normally, SYS1.SORTLIB.
SYSOUT The messages produced by the sort/merge utility. Normally, a SYSOUT data set.
SORTIN The input file or files to be sorted. If more than one file is to be sorted, you can concatenate them.
SORTOUT The sorted output file. It can be a new non-VSAM file with DISP=(NEW,KEEP) or DISP=(NEW,CATLG), an extension of an existing non-VSAM file with DISP=MOD, or an existing VSAM file.
SORTWKnn The work files required by the sort, where nn is a consecutive number starting with 01. One or two files are usually adequate, and the total DASD space allocated to the work files should be about twice the input file size. You can specify up to 255 work files.
SYSIN The sort control statements. Normally, an instream data set.

Common JCL SORT SYSIN Cards

The SYSIN DD statement tells DFSORT which fields to sort and which sequence to use. In a fixed-length file, the first number is the starting byte, the second number is the field length, the format tells DFSORT how to read the data, and A or D sets ascending or descending order.

Ascending character sort

//SYSIN   DD *
  SORT FIELDS=(9,5,CH,A)
/*

This sorts on a 5-byte character field that starts in position 9. Use this when a customer code, item code, state code, or short name field must run from low value to high value.

Descending character sort

//SYSIN   DD *
  SORT FIELDS=(9,5,CH,D)
/*

The same field can be sorted in reverse sequence by changing A to D. This is useful when the newest code, highest alphabetic range, or reverse report order must appear first.

Multi-field sort

//SYSIN   DD *
  SORT FIELDS=(1,10,CH,A,20,8,ZD,D)
/*

This sorts first by a 10-byte character key in ascending order, then by an 8-byte zoned decimal field in descending order. For example, a report can group records by account number and put the largest amount first inside each account.

For filtering records before or after a sort, see JCL SORT INCLUDE and OMIT examples. If you are still setting up the full job, start with the JCL tutorial for JOB, EXEC and DD statements.

Example: Sample SORT JCL.


//RCORT01 JOB  (...JOB CARD..) 
//SORT    EXEC PGM=SORT
//SORTLIB DD DSNAME=SYS1.SORTLIB,DISP=SHR
//SYSOUT  DD SYSOUT=*
//SORTIN  DD DSNAME=RCKT10.INVOICE.TRANS,DISP=SHR
//SORTOUT DD DSNAME=RCKT10.INVOICE.TRANS.SORTED,DISP=(NEW,CATLG),
//                  UNIT=SYSDA,VOL=SER=MPS8BV,
//                  SPACE=(CYL,(10,5))
//SORTWK01 DD UNIT=SYSDA,SPACE=(CYL,(20,5))
//SYSIN    DD * 

  SORT FIELDS=(9,5,CH,A)
/*


JCL SORT FAQ

What does SYSIN do in a JCL SORT step?

SYSIN supplies the DFSORT control statements. In the sample job, the SORT statement in SYSIN tells DFSORT which field starts at byte 9, how many bytes to read, which data format to use, and whether the output should be ascending or descending.

How do I read SORT FIELDS=(9,5,CH,A)?

Read it as: start at position 9, sort 5 bytes, treat the field as character data, and sort in ascending order. If the key field moves in the input layout, change the starting position and length to match the new copybook or record layout.

What is the difference between A and D in DFSORT?

A means ascending sequence and D means descending sequence. For character keys, ascending puts lower character values first. For numeric keys such as zoned decimal fields, descending puts the largest numeric values first.

Can JCL SORT use more than one key field?

Yes. Add each field to the SORT FIELDS list in priority order. DFSORT sorts by the first key first, then uses the second key when two records have the same value in the first key.

When should I use INCLUDE or OMIT with SORT?

Use INCLUDE when only matching records should go to SORTOUT. Use OMIT when matching records should be dropped. Filtering before the sort can reduce the number of records DFSORT has to order.


No comments:

Post a Comment

New In-feed ads