OUTREC

OUTREC control statement is used to reformat (adds, deletes, or reformats fields) each record after they are sorted, merged, or copied by specifying all of its items one by one. This statement supports a wide variety of parsing, editing, and reformatting tasks. We can even add spaces/zeroes/any character into the output record based on the requirement.

                     .-,----------.                          
                     V            |                          
>>-OUTREC--+-PARSE=(---definition-+-)--------------------+-><
           |                   .-,----.                  |   
           |                   V      |                  |   
           '---+-+-FIELDS=-+-(---item-+-)------------+---'   
               | '-BUILD=--'                         |       
               |           .-,----.                  |       
               |           V      |                  |       
               +-OVERLAY=(---item-+-)----------------+       
               |           .-,----.                  |       
               |           V      |                  |       
               +-FINDREP=(---item-+-)----------------+       
               | .-,---------------.                 |       
               | V                 |                 |       
               '---IFTHEN=(clause)-+--+------------+-'       
                                      ‘-IFOUTLEN=n-'         

INREC and OUTREC do the same, but the only difference is the way reformatting is done.

INREC: Specifies how records are reformatted before they are sorted, copied, or merged. INREC is useful in case of the large input files. When INREC is used reformatting of records is done BEFORE the sort.

OUTREC: Specifies how records are reformatted after they are sorted, copied, or merged. The final output will be the same. When it is used reformatting of records is done AFTER the sort.

You can create the reformatted OUTREC records in one of the following ways using unedited, edited, or converted input fields.

OUTREC BUILD or FIELDS:

Reformat each record by specifying all of its items one by one. Build gives you complete control over the items you want in your reformatted OUTREC records and the order in which they appear. You can delete, rearrange, and insert fields and constants. 

SORT FIELDS=(1,5,CH,A)                                             
OUTREC FIELDS=(1,39,40,8,ZD,EDIT=(SII,III,IIT),SIGNS=(,-))  
  • OUTREC FIELDS=(1,39,..) copies the first 39 bytes from the input file to the output as it is.
  • OUTREC FIELDS=(..,40,8,ZD,EDIT=(SII,III,IIT),SIGNS=(,-)) converts the 8 digit ZD to M12(SII,III,IIT) and displays sign only for negative values.

Example:

Remove the “()” from data in the first 29 bytes, remove spaces between the data, and separate the data with ‘,’.

OPTION COPY                                                    
OUTREC FIELDS=(1,80,SQZ=(SHIFT=LEFT,PREBLANK=C’()',MID=C','))  

Explanation:

  • OUTREC FIELDS=(1,80,SQZ=(SHIFT=LEFT,..)) – Squeezes the data in 1-80 bytes to the left.
  • OUTREC FIELDS=(1,80,SQZ=(..,PREBLANK=C'()’,..)) – Blanks out the ()
  • OUTREC FIELDS=(1,80,SQZ=(.., MID=C’,’)) – the data which had spaces in between separated by ‘,’.

Example:

Align the data in the first 29 bytes to LEFT and replace () with <>.

OPTION COPY                                        
OUTREC FIELDS=(1,29,JFY=(SHIFT=LEFT,PREBLANK=C'()',
               LEAD=C'<',TRAIL=C'>'),30,30)  

Explanation:

  • OUTREC FIELDS=(1,29,JFY=(SHIFT=LEFT,..),..) – Justifies the data in the first 29 bytes to the left.
  • OUTREC FIELDS=(1,29,JFY=(..,PREBLANK=C'()’,..),..) – blank out the ().
  • OUTREC FIELDS=(1,29,JFY=(.., LEAD=C'<‘,TRAIL=C’>’),..) – adds the C’<’ as a lead and C’>’ as a trail.
  • OUTREC FIELDS=(..,30,30) – Copies the input file data from the 30th byte of length 30 copies to output as it is.

Example:

Replace the “math” with “mathematics”.

SORT FIELDS=(1,5,CH,A)                                             
OUTREC FIELDS=(1,29,30,4,CHANGE=(11,C'math',C'mathematics'),        
               4X,45,30)  

Explanation:

  • OUTREC FIELDS=(1,29,..) – Copies the first 29 bytes of data from the input file to output as it is.
  • OUTREC FIELDS=(..,30,4,CHANGE=(11,C’math’,C’mathematics’),..) – The “math” text starting from the 30th byte of length 4 in the input file should replace with “mathematics” of length 11 while writing it to the output file.
  • OUTREC FIELDS=(..,4X,..) – add 4 spaces from 40th byte.
  • OUTREC FIELDS=(..,45,30) – copies the input file data from 45th byte of length 30 as it is to output starts at 45th byte.

Example:

Convert the date from mmddccyy to ccyymmm(julian date).

SORT FIELDS=(1,5,CH,A)    
OUTREC FIELDS=(1,54,55,8,Y4W,TOJUL=Y4T)

Explanation:

  • OUTREC FIELDS=(1,54,..) copies first 54 bytes of input file data to output as it is.
  • OUTREC FIELDS=(..,55,8,Y4W,TOJUL=Y4T)- data from 55th byte of length 8 will be converted to Y4T Julian date format.

Example:

Add two days, two years to the date in the input file.

SORT FIELDS=(1,5,CH,A)                                             
OUTREC FIELDS=(1,54,55,8,Y4W,ADDDAYS,+2,TOJUL=Y4T(/),              
              5X,55,8,Y4W,ADDYEARS,+2,TOJUL=Y4T(/))

Explanation:

  • OUTREC FIELDS=(1,54,..) copies the first 54 bytes from the input file to output as it is.
  • OUTREC FIELDS=(..,55,8,Y4W,ADDDAYS,+2,TOJUL=Y4T(/),..) – adds +2 days to the date in the input file and converts it to Julian date before writing it to the output file from 55th position.
  • OUTREC FIELDS=(..,5X,..) – adds 5 spaces from 63rd position
  • OUTREC FIELDS=(..,55,8,Y4W,ADDYEARS,+2,TOJUL=Y4T(/)) – adds +2 years to the date in the input file and converts it to Julian date before writing it to output file from 68th position.

Example:

Multiply the marks with 10 and store them in the same record.

SORT FIELDS=(1,5,CH,A)               
OUTREC FIELDS=(1,48,45,3,ZD,MUL,+10) 

Explanation:

  • OUTREC FIELDS=(1,48,..) copies the first 48 bytes of input file data as it is to output.
  • OUTREC FIELDS=(..,45,3,ZD,MUL,+10) – data starting from the 45th byte of length 3 will be multiplied by +10 and stored it in the same record as a continuation.

Example:

Convert the first five bytes ZD to FS in the input file.

SORT FIELDS=(1,5,CH,A) 
OUTREC FIELDS=(1,5,ZD,TO=FS,LENGTH=6,X,6,73) 

Explanation:

  • OUTREC FIELDS=(1,5,ZD,TO=FS,LENGTH=6,..) converts the first five bytes of ZD from the input file to FS of 6 bytes and writes it to output.
  • 7th byte will be placed as a space in the output file.
  • OUTREC FIELDS=(..,6,73) copies the input file data from the 6th byte to the output file from the 8th byte onwards as it is.

Example:

Let’s insert the below data types between the fields in the output file.

  • Zeroes
  • Blanks
  • Strings

Zeroes

You can use Z or 1Z to specify a single binary zero. You can use nZ to specify n binary zeros.

   SORT FIELDS=(1,5,CH,A)
   OUTREC BUILD=(1,5,4Z,6,4)

Explanation:

This sort card will insert 4 binary zeroes between the first and second fields of your output file.

Blanks

You can insert blanks before, between, or after fields. You can use X or 1X to specify a single blank. You can use nX to specify n blanks. To insert 10 blanks, write 10X before the first field. To insert 5 blanks, write 5X between the two fields.

   SORT FIELDS=(1,5,CH,A)
   OUTREC BUILD=(20X,1,5,10X,6,4)

Explanation:

This sort card will insert spaces in the first 20 bytes, then the fields 1 to 5 from the input file are moved to 21 thru 25, 26 thru 36 will have blanks and then input file fields from position 6 to 10 is moved to output file positions 37 to 41.

Strings

To insert a character string to your output include C ‘ your string ’  as part of your OUTREC, you can include any EBCDIC character between single quotes. To include a single apostrophe in the string, you must specify it as two single apostrophes example, to include the word “Tom’s” you need to specify  C’Tom”s’.

 SORT FIELDS=(1,5,CH,A)
 OUTREC BUILD=(C'Run Date:',1,10,C'|',C'Run Time:’,11,4)

Explanation:

If your input file record is ‘2015/04/0415:30′ the output will be “Run Date:2015/04/04|Run Time:13:30″.

OUTREC OVERLAY

Reformat each record by specifying just the items that overlay specific columns. Overlay lets you change specific existing columns without affecting the entire record. Example:

OPTION COPY   
OUTREC OVERLAY=(30:30,4,TRAN=LTOU, 
               45:45,3,ZD,MUL,+10,TO=ZD,LENGTH=4)

Explanation:

  • OUTREC OVERLAY=(30:30,4,TRAN=LTOU,..) – Converts the data lower to upper from 30th position of length 4 and writes to output from 30th position.
  • OUTREC OVERLAY=(..,45:45,3,ZD,MUL,+10,TO=ZD,LENGTH=4) – the data from 45th byte multiplies with 10 and writes the result to output of the length 4 from 45th position.

The below OVERLAY will extend the records.

Example:

OPTION COPY   
OUTREC OVERLAY=(30:30,4,TRAN=LTOU, 
               85:45,3,ZD,MUL,+10,TO=ZD,LENGTH=4)

Explanation:

  • OUTREC OVERLAY=(30:30,4,TRAN=LTOU,..) – Converts the data lower to upper from 30th position of length 4 and writes to output from 30th position.
  • OUTREC OVERLAY=(..,85:45,3,ZD,MUL,+10,TO=ZD,LENGTH=4) – the data from 45th byte multiplies with 10 and writes the result to output of the length 4 from 85th position.

Example:

SORT FIELDS=(1,5,ZD,A)  
OUTREC OVERLAY=(60:SEQNUM,2,ZD,START=5,INCR=5)

Explanation:

SORT FIELDS=(1,5,ZD,A) – The input file will be sorted first and written to the output.

OUTREC OVERLAY=(60:SEQNUM,2,ZD,START=5,INCR=5) – Generates the sequence number of length 2 from 60th byte. The sequence number starts at 5 and is incremented by 5 each time.

OUTREC FINDREP

If you want to replace or remove data anywhere in records, the FINDREP parameter of the OUTREC statement needs to use instead. FINDREP indicates doing a find and replace operation. IN identifies the constant (the “find” constant) and OUT identifies the constant (the “replace” constant). Example:

OPTION COPY      

OUTREC FINDREP=(IN=C'small',OUT=C'SMALL')

Explanation:

OUTREC FINDREP=(IN=C’small’,OUT=C’SMALL’) – finds the text “small” in the entire input file with the “SMALL” and writes to the output.

OUTREC IFTHEN clauses

Reformat different records in different ways by specifying how build, overlay, find/replace, or group operation items are applied to records that meet given criteria. IFTHEN clauses let you use sophisticated conditional logic to choose how different record types are reformatted. Example:

OPTION COPY                                                        
OUTREC IFTHEN=(WHEN=INIT,BUILD=(1:1,80)),                          
IFTHEN=(WHEN=(30,5,CH,EQ,C'small'),OVERLAY=(45:C'***')),      
IFTHEN=(WHEN=NONE,BUILD=(1:1,80))   

Explanation:

  • OUTREC IFTHEN=(WHEN=INIT,BUILD=(1:1,80)),..) – Copies the 80 bytes of data from the input file to output as it is.
  • IFTHEN=(WHEN=(30,5,CH,EQ,C’small’),OVERLAY=(45:C’***’)) – overlays the marks of the student with ‘***’ who belongs to small.
  • IFTHEN=(WHEN=NONE,BUILD=(1:1,80)) – If no matches to conditions specified in WHEN, copy the 80 bytes of data from the input file to output as it is.

OUTREC PARSE

PARSE can be used for many different types of variable fields including delimited fields, comma-separated values (CSV), tab-separated values, blank-separated values, keyword-separated fields, null-terminated strings, and so on. You can assign up to 1000 parsed fields (%0-%999) to the variable fields you want to extract.

Note that if all of the fields in your records have fixed positions and lengths, you don’t need to use PARSE. But if any of the fields in your records have variable positions or lengths, you can use PARSE to treat them as fixed parsed fields in BUILD or OVERLAY. You can mix p,m fields (fixed fields), and %nn fields (parsed fields) in BUILD and OVERLAY.

INPUT FILE
N12345¦HMOMEDICAL¦P1234¦103¦ABCDE¦SAMDAVID¦CA¦20210101¦99991231¦A
N11133¦PPO¦A1200¦123¦ABCDE¦KIM DAVID¦CA¦20210101¦99991231¦T
N12399¦CALCARE¦P1001¦199¦ABC¦ANNA DAVID¦CA¦20210101¦99991231¦A
N00300¦DENTAL¦P1299¦999¦XYZ¦CHERYL CHRIS¦CA¦20210101¦99991231¦A
 //STEP01    EXEC  PGM=SORT           
 //SORTIN    DD DISP=SHR,DSN=I/P File  
 //SORTOUT   DD DSN=O/P File,
 //          DISP=(NEW,CATLG,DELETE),       
 //          UNIT=TESTDA,SPACE=(TRK,(10,10),RLSE) 
 //          DCB=(LRECL=80,BLKSIZE=0,RECFM=FB,DSORG=PS)      
 //SYSOUT    DD SYSOUT=*        
 //SYSPRINT  DD SYSOUT=* 
 //SORTWK01 DD SPACE=(CYL,20),UNIT=SYSDA
 //SORTWK02 DD SPACE=(CYL,20),UNIT=SYSDA
 //SORTWK03 DD SPACE=(CYL,20),UNIT=SYSDA          
 //SYSIN     DD *                                         
   SORT FIELDS=COPY             
   OUTREC PARSE=(%01=(ENDBEFR=C'¦',FIXLEN=5),     
                 %02=(ENDBEFR=C'¦',FIXLEN=10),       
                 %03=(ENDBEFR=C’¦’,FIXLEN=05),
                 %04=(ENDBEFR=C’¦',FIXLEN=03),
                   %=(ENDBEFR=C’¦’),                       
 ………
 ………
 ………                 
                 %10=(ENDBEFR=C'¦',FIXLEN=1)),             
          BUILD=(%01,%02,%03,…,%10)           
 /*
OUTPUT  
 N12345HMOMEDICALP1234103SAM DAVID      CA 20210101 99991231 A
 N11133PPO       A1200123KIM DAVID      CA 20210101 99991231 T
 N12399CALCARE   P1001199ANNA DAVID     CA 20210101 99991231 A
 N00300DENTAL    P1299999CHERYL CHRIS.  CA 20210101 99991231 A 

The %01 parsed field is used to extract the first variable field into a 5-byte fixed parsed field. ENDBEFR=C’¦’ tells DFSORT to stop extracting data at the byte before the next comma (the comma after the first variable field). FIXLEN=5 tells DFSORT that the %01 parsed field is 5 bytes long.

The % parsed field is used to skip the variable field without extracting anything for it. BUILD operand is used to construct the output record.

Example

The following control statements will transform records containing a field of format cyymmdd to the format yyymmdd.

SORT FIELDS=(1,7,BI,A)      
OUTFIL OUTREC=(1,1,CHANGE=(2,      * change C'c' as follows:
                      C'0',C'19',  *   C'0' to C'19' 
                      C'1',C'20',  *   C'1' to C'20' 
                      C'2',C'21'), *   C'2' to C'21' 
                      NOMATCH=(C'99')
                       2,6)                * copy C'yymmdd' 

The OUTREC control statement in SORT is a powerful tool for customizing the output of a sort operation. It enables data professionals to manipulate records, rearrange fields, and perform calculations, all within the sorting process. As with any tool, it’s essential to practice and experiment with different scenarios to fully grasp the potential of OUTREC in SORT. Whether you’re reformatting dates, concatenating fields, or performing conditional replacements, OUTREC empowers you to mold your data exactly as needed.

IBM Reference: Click Here

Scroll to Top