Blog enJCL

SORT in JCL: Main features and uses

In JCL (Job Control Language), you can use the SORT program to sort two files. Here’s a basic example of a JCL that performs the task of sorting two files:

Definition of SORT in JCL

The SORT statement sorts file records or table elements according to user-specified keys, making the sorted records available through an output procedure or in an output file.

SORT
IBM’s SORT statement

This example shows the basic structure of a SORT statement and its main characteristics.

//SORTJOB  JOB  (123),'Nombre del job',CLASS=A,MSGCLASS=X,MSGLEVEL=(1,1)
//STEP1    EXEC PGM=SORT
//SYSOUT   DD  SYSOUT=A
//SORTIN1  DD  DSN=your.input.file1,DISP=SHR
//SORTIN2  DD  DSN=your.input.file2,DISP=SHR
//SORTOUT  DD  DSN=your.output.file,DISP=(NEW,CATLG,DELETE),
//             UNIT=SYSDA,SPACE=(CYL,(5,5),RLSE)
//SYSIN    DD  *
  SORT FIELDS=(start_position,length,format,A/D)
/*

In this example:

  • //SORTJOB is the job statement, where you can specify information such as job name, job class, etc.
  • //STEP1 is the job step statement. In this case, we are using the SORT program.
  • //SYSOUT specifies the system output for SORT program messages.
  • //SORTIN1 and //SORTIN2 are the statements for the two input files you want to sort.
  • //SORTOUT is the statement for the sorted output file.
  • //SYSIN contains the control options for the SORT program. In this example, the SORT FIELDS option is used to specify the sorting fields.

Make sure to replace your.input.file1, your.input.file2, and your.output.file with the actual locations and names of your files. Also, adjust space allocations and other settings as per your requirements.

Main sorting parameters in JCL

The option SORT FIELDS=(start_position,length,format,A/D) specifies the fields by which sorting will be performed. You can adjust these parameters according to the actual fields in your files and the desired sorting order (ascending or descending).

Ascending (A) or Descending (D) Order:

  • A: Ascending order (default if not specified).
  • D: Descending order.

Example:

SORT FIELDS=(1,10,A)

Character (CH) or Numeric (PD) Sorting:

  • CH: Sorts character fields (default if not specified).
  • PD: Sorts numeric fields.

Example:

SORT FIELDS=(11,5,PD)

Date Format (YMD, DMY, MDY, etc.):

  • You can specify a date format for date fields.

Example:

SORT FIELDS=(21,8,YMD)

Skipping Specific Characters:

  • You can specify characters to skip during sorting.

Example:

SORT FIELDS=(1,10,A,SKIP=3)

Offset and Length Options:

  • Specify the starting position and length of the field.

Example:

SORT FIELDS=(16,3,A)

Case Sensitivity (IGNORECASE):

  • Ignore case differences during sorting.

Example:

SORT FIELDS=(1,10,A,IGNORECASE)

Handling White Spaces (SPACES):

  • Ignore white spaces during comparison.

Example:

SORT FIELDS=(1,10,A,SPACES)

Copying File with Same Format (COPY):

  • Copies the input file with the same format to the output file.

Example:

SORT FIELDS=COPY

These are just some of the most common options. The product documentation you are using (e.g., DFSORT or SyncSort) will provide more detailed information on specific available options and their implementation details. Make sure to refer to the documentation relevant to the product you are using.

Example of SORT in JCL

File sorting

//SORTJOB  JOB  (123),'Ordenar Ficheros',CLASS=A,MSGCLASS=X,MSGLEVEL=(1,1)
//STEP1    EXEC PGM=SORT
//SYSOUT   DD  SYSOUT=A
//SORTIN1  DD  DSN=your.input.file1,DISP=SHR
//SORTIN2  DD  DSN=your.input.file2,DISP=SHR
//SORTOUT  DD  DSN=your.output.file,DISP=(NEW,CATLG,DELETE),
//             UNIT=SYSDA,SPACE=(CYL,(5,5),RLSE)
//SYSIN    DD  *
  SORT FIELDS=(11,20,A,31,30,A)
/*

This example assumes that the fields for first name and last name start at positions 11 and 31, respectively. Make sure to adjust the positions and lengths according to the actual structure of your data.

In the statement SORT FIELDS=(11,20,A,31,30,A), the option A indicates ascending order. You can change it to D for descending order if desired.

Remember to replace your.input.file1, your.input.file2, and your.output.file with the actual locations and names of your files. Also, adjust space allocations and other settings according to your needs.

Please note that this is a basic example and you should tailor it to your specific requirements and the environment you are working in.

Copying two input files into one output file

To merge two files into a single one-step JCL in a Mainframe environment, you can use the SORT utility. Here’s an example of how you could do it:

//EJEMPLO JOB …
//STEP1 EXEC PGM=SORT
//SYSOUT DD SYSOUT=A
//SORTIN1 DD DSN=your.input.file1,DISP=SHR
//SORTIN2 DD DSN=your.input.file2,DISP=SHR
//SORTOUT DD DSN=your.output.file,DISP=(NEW,CATLG,DELETE),
// UNIT=DISCO,SPACE=(TRK,(1,1),RLSE)
//SYSIN DD *
SORT FIELDS=COPY
/*

In this example:

  • SORTIN1: Specifies the location of the first file (containing names and last names).
  • SORTIN2: Specifies the location of the second file (containing street addresses).
  • SORTOUT: Specifies the location of the combined file that will be created.

The parameter FIELDS=COPY indicates that the data should be copied without changing the order or structure. Both datasets will be copied into the output file.