OUTFIL Updating counts and totals in trailer
OUTFIL Updating counts and totals in trailer
We can use the OUTFIL statement to generate trailers with updated counts and totals. OUTFIL's IFTRAIL parameter is used to update the count and total values in an existing trailer (last) record to reflect the actual data records in the file.
Syntax -
//SYSIN DD *
OUTFIL FNAMES=DDname-n,INCLUDE=(..),
IFTRAIL=(HD=YES,TRLID=(starting_position,length,format,Character/String),
TRLUPD=(…))
/*
HD=YES | Specifies the first record is a header record. If the input file has a header, HD=YES need to use to avoid treating the header record as a data record. |
TRLID=(logexp) | Specifies the trailer record using the same type of logical expression used for INCLUDE.
TRLID=(logexp) identifies the trailer record as having a 'T' in position 1. The identified trailer record is treated as the last record; it is not treated as a data record. |
TRLUPD | Specifies the fields in the data records to be used to update counts and totals. c:, COUNT=, COUNT+n=, COUNT-n=, TOT= and TOTAL= can be used in TRLUPD in the same way they are used in TRAILER1. TRLUPD indicates the counts and totals to be updated. |
Examples -
Scenario1 - Adding Record Count in Trailer.
//SYSIN DD *
SORT FIELDS=COPY
OUTFIL FNAMES=SORTOUT,
TRAILER1=('TOTAL RECORDS: ',COUNT=(M11,LENGTH=7))
/*
Adds a trailer line at the end of the output file with the text 'TOTAL RECORDS: ' followed by the total number of records, formatted as a 7-digit number using M11 format.
Scenario2 - Adding Total for a Numeric Field in Trailer.
//SYSIN DD *
SORT FIELDS=COPY
OUTFIL FNAMES=SORTOUT,
TRAILER1=('TOTAL SALES: ',TOTAL=(10,5,ZD,LENGTH=10))
/*
Adds the total sum of the 5-byte zoned decimal field starting at position 10 in the records, formatted as a 10-digit number. The trailer contains the text 'TOTAL SALES: ' followed by the sum of the sales field.