Blog enJCL

OUTFIL in JCL: SORT to Divide a File


The OUTFIL in JCL is used with the SORT utility to create multiple output files from a single input file, based on specific criteria. This process is highly useful when we want to divide records in a file according to a certain condition, such as status, type, or any other identifying field. In this example, we’ll cover how to use OUTFIL in JCL SORT to split a file based on a particular position and value.

OUTFIL example in JCL

Suppose we have an input file, USER.FILE.ALL, with the following records:

00001JOHN      SMITH     STUDENT  
00002JANE      DOE       TEACHER  
00003ROBERT    BROWN     STUDENT  
00004EMILY     DAVIS     STUDENT  
00005MICHAEL   WILSON    TEACHER  
00006SARAH     JOHNSON   STUDENT  
00007DAVID     LEE       TEACHER  
00008LAURA     MARTIN    STUDENT  
00009DANIEL    GARCIA    TEACHER  
00010JESSICA   WHITE     STUDENT  

Each record has a status at position 26, indicating whether the person is a “STUDENT” or “TEACHER”. We want to separate these records into two files based on this position.

JCL Code Example

Below is a JCL example using SORT to divide USER.FILE.ALL into two files: USER.FILE.STUDENTS for students and USER.FILE.TEACHERS for teachers.

//SPLITFILE JOB (ACCT),'Divide Files',CLASS=A,MSGCLASS=X
//STEP1     EXEC PGM=SORT
//SYSPRINT  DD SYSOUT=*
//SORTIN    DD DSN=USER.FILE.ALL,DISP=SHR
//STUDENTS  DD DSN=USER.FILE.STUDENTS,DISP=(NEW,CATLG,DELETE),
//          UNIT=SYSDA,SPACE=(CYL,(5,5),RLSE),
//          DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//TEACHERS  DD DSN=USER.FILE.TEACHERS,DISP=(NEW,CATLG,DELETE),
//          UNIT=SYSDA,SPACE=(CYL,(5,5),RLSE),
//          DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//SYSOUT    DD SYSOUT=*
//SYSIN     DD *
  SORT FIELDS=COPY
  OUTFIL FNAMES=STUDENTS,INCLUDE=(26,8,CH,EQ,C'STUDENT')
  OUTFIL FNAMES=TEACHERS,INCLUDE=(26,8,CH,EQ,C'TEACHER')
/*

Explanation:

  • SORTIN: Specifies the input file, USER.FILE.ALL.
  • STUDENTS and TEACHERS: Define the output files where the records meeting the conditions will be stored. These will be saved as USER.FILE.STUDENTS and USER.FILE.TEACHERS.
  • SYSIN:
    • SORT FIELDS=COPY: Specifies that records should be copied without sorting.
    • OUTFIL FNAMES=STUDENTS,INCLUDE=(26,8,CH,EQ,C'STUDENT'): Filters records where the value in position 26,8 is “STUDENT”.
    • OUTFIL FNAMES=TEACHERS,INCLUDE=(26,8,CH,EQ,C'TEACHER'): Filters records where the value in position 26,8 is “TEACHER”.

After running this JCL, USER.FILE.STUDENTS will contain records with “STUDENT” in the specified position, and USER.FILE.TEACHERS will contain records with “TEACHER” in that same position.

Resulting Output Files

USER.FILE.STUDENTS:

00001JOHN      SMITH     STUDENT  
00003ROBERT    BROWN     STUDENT  
00004EMILY     DAVIS     STUDENT  
00006SARAH     JOHNSON   STUDENT  
00008LAURA     MARTIN    STUDENT  
00010JESSICA   WHITE     STUDENT  

USER.FILE.TEACHERS:

00002JANE      DOE       TEACHER  
00005MICHAEL   WILSON    TEACHER  
00007DAVID     LEE       TEACHER  
00009DANIEL    GARCIA    TEACHER  

With OUTFIL, separating data in JCL based on conditions is streamlined and efficient, especially for tasks involving large datasets with diverse records. This example illustrates the flexibility of JCL’s SORT utility for organizing data as needed.


Compatible Elements with OUTFIL

OUTFIL in JCL can be combined with several elements and options for filtering and manipulating data. Here are some of the most common ones:

INCLUDE/OMIT:

INCLUDE and OMIT allow you to select or exclude records based on specific conditions.

OUTFIL INCLUDE=(start,length,format,operator,value)
OUTFIL OMIT=(start,length,format,operator,value)

BUILD/OUTREC:

BUILD (or OUTREC) is used to reformat the output records by specifying the positions and formats of the fields.

OUTFIL BUILD=(newpos1:start1,length1,...)

REMOVECC/NOREMOVECC:

REMOVECC or NOREMOVECC control whether carriage control characters should be removed or kept in the output file.

OUTFIL REMOVECC
OUTFIL NOREMOVECC

Example Combining Multiple Elements

Below is an example of combining multiple elements in OUTFIL:

//STEP1    EXEC PGM=SORT
//SORTIN   DD DSN=USER.FILE.DATA,DISP=SHR
//SORTOUT  DD DUMMY
//OUT1     DD DSN=USER.FILE.STUDENTS,DISP=(NEW,CATLG,DELETE),
//             SPACE=(CYL,(1,1),RLSE),
//             DCB=(DSORG=PS,RECFM=FB,LRECL=35,BLKSIZE=350)
//OUT2     DD DSN=USER.FILE.TEACHERS,DISP=(NEW,CATLG,DELETE),
//             SPACE=(CYL,(1,1),RLSE),
//             DCB=(DSORG=PS,RECFM=FB,LRECL=35,BLKSIZE=350)
//SYSOUT   DD SYSOUT=*
//SYSIN    DD *
  SORT FIELDS=COPY
  OUTFIL FILES=01,INCLUDE=(26,7,CH,EQ,C'STUDENT'),BUILD=(1,35)
  OUTFIL FILES=02,INCLUDE=(26,8,CH,EQ,C'TEACHER'),BUILD=(1,35)
/*

In this example:

  • INCLUDE is used to filter specific records.
  • BUILD is used to reformat the output records.
  • FILES=01 and FILES=02 are used to specify multiple output files.

This demonstrates how you can effectively use OUTFIL to manage and process data in JCL by combining different elements for filtering, reformatting, and controlling the output format.