SUM with OUTFIL in One Step

You can use SUM FIELDS= and OUTFIL in the same DFSORT step. SUM runs after the sort and produces one record per group (summary records); that output is then what OUTFIL sees. So you can write the summary records to SORTOUT and at the same time use OUTFIL to add headers and trailers (e.g. TRAILER1 with COUNT= and TOTAL=) and write to one or more report datasets. This page explains the order of processing (sort → SUM → OUTFIL), what SORTOUT and OUTFIL FNAMES= receive, and how to combine SUM with OUTFIL for both a summary dataset and a report in one step.

OUTFIL Advanced Output Control
Progress0 of 0 lessons

Processing Order

When both SUM and OUTFIL are present, DFSORT runs: (1) Sort (and optional INREC), (2) SUM (collapse each group to one record), (3) OUTFIL (format and write to FNAMES=). So OUTFIL never sees the original detail records; it sees the summary records (one per group). SORTOUT also receives the summary records unless you use OUTFIL FNAMES=SORTOUT to replace what goes to SORTOUT with the OUTFIL output (e.g. summary records plus TRAILER1).

Typical Use: Summary Dataset and Report

A common pattern is: sort by key, SUM to get one record per group, then OUTFIL to write that summary to SORTOUT and to a report DD with a header and trailer:

text
1
2
3
4
5
6
7
SORT FIELDS=(1,5,CH,A) SUM FIELDS=(20,6,PD) OUTFIL FNAMES=(SORTOUT,REPORT), HEADER1=(1:'Summary by Department',/,5:DATE=(MD4-)), OUTREC=(1,80), TRAILER1=(1:'Groups: ',COUNT=(M11,LENGTH=6), ' Grand total: ',TOTAL=(20,6,PD,LENGTH=12))

SORTOUT and REPORT both get the summary records (one per department) plus the header and trailer. COUNT= in TRAILER1 is the number of summary records (groups). TOTAL= sums the 6-byte PD field at 20 across those records—which is the same as the grand total of the original amount field because each summary record holds the group total.

Two Outputs: Summary Only vs Report

You can split outputs: one DD gets only the summary records (no header/trailer), another gets the report (summary records + HEADER1 + TRAILER1). Use two OUTFIL statements with different FNAMES= and different options:

text
1
2
3
4
5
OUTFIL FNAMES=SUMMARY,OUTREC=(1,80) OUTFIL FNAMES=REPORT, OUTREC=(1,80), HEADER1=(1:'Report',/), TRAILER1=(1:'Total groups: ',COUNT=(M11,LENGTH=6))

SUMMARY gets 80-byte summary records only. REPORT gets the same records plus a one-line header and a trailer with the count of groups.

What TRAILER1 Count and Total Mean

After SUM, the record stream has one record per group. So COUNT= in TRAILER1 is the number of groups (summary records), not the original input record count. TOTAL= sums the specified field across the summary records. If that field in the summary record is the group total (from SUM), then the TRAILER1 total is the grand total across all groups. So for “grand total of amount,” TOTAL= is correct; for “total number of original records,” you would need a different approach (e.g. a separate step without SUM that only counts).

Explain It Like I'm Five

First we sort the cards and then we squish each pile (group) into one card that has the total for that pile. That’s SUM. Then we take those squished cards and we can put them in one box (SORTOUT) and also write a little title on top and a line at the bottom that says “how many piles” and “big total.” That’s OUTFIL. So one step: sorted, squished, and then either just the squished cards or the squished cards with a header and footer.

Exercises

  1. Write control statements that sort by 1,5 CH A, sum 20,6 PD, and write summary records to SORTOUT and to REPORT with TRAILER1 showing count and total of the summed field.
  2. Why does COUNT= in TRAILER1 give the number of groups (not original records) when SUM is used?
  3. If you need both a file of summary records and a file of all detail records with a grand total trailer, why might you need two steps?

Quiz

Test Your Knowledge

1. When you use both SUM FIELDS= and OUTFIL in the same step, what does SORTOUT receive?

  • Only the detail records
  • The SUM output—one record per group (summary records); OUTFIL can write the same or different output to other DDs
  • Only the trailer
  • Nothing; OUTFIL gets everything

2. Can OUTFIL FNAMES= point to the same dataset as SORTOUT?

  • No, never
  • Yes—you can have OUTFIL FNAMES=SORTOUT so the same summary records (possibly with different formatting) go to SORTOUT; typically you use different DDs for detail vs report
  • Only for COPY
  • Only if SUM is not used

3. What is the order of processing when both SUM and OUTFIL are present?

  • OUTFIL then SUM
  • Sort, then SUM (collapse to one per group), then OUTFIL processing applies to the SUM output—so OUTFIL sees summary records
  • Only OUTFIL runs
  • SUM and OUTFIL run in parallel

4. If you want both a detail report (all records + trailer) and a summary dataset (one per group), can you do it in one step?

  • No, you need two steps
  • Yes—use OUTFIL with two FNAMES: one OUTFIL without SUM so it gets the pre-SUM data (detail) with TRAILER1, and one OUTFIL with SUM output; but SUM applies to the whole step so you need to structure with OUTFIL writing before SUM or use two OUTFILs—one for detail (no SUM in path) and one for summary. In standard DFSORT, SUM applies before OUTFIL, so OUTFIL sees summary records. To get both detail and summary you typically use two OUTFILs: one that gets the data as it would be without SUM (if supported) or run two steps
  • Only with ICETOOL
  • SUM disables OUTFIL

5. What does TRAILER1 show when you use SUM and OUTFIL together?

  • The count of original input records
  • By default, the count and total over the records that OUTFIL sees—which after SUM are the summary records (one per group), so count = number of groups unless you use special options to preserve original count
  • Only the sum of one field
  • Nothing