Section subtotals in DFSORT are the count and total lines that appear after each control-break group (e.g. after each department). You get them by sorting by the section key and using OUTFIL SECTIONS= with TRAILER3=. TRAILER3 is written once per section and typically contains the section key value, COUNT= (records in that section), and TOTAL= (sum of a numeric field in that section). This page covers common patterns: single-level and multi-level section breaks, using SKIP= for spacing, including the section key in the trailer line, and ensuring data is sorted correctly so subtotals are right.
For one level (e.g. department): sort by the key that defines the section, then use SECTIONS= with that key and TRAILER3=.
12345678SORT FIELDS=(30,5,CH,A) OUTFIL FNAMES=REPORT, SECTIONS=(30,5,SKIP=2L, TRAILER3=(1:'Dept ',30,5,' Count: ',COUNT=(M11,LENGTH=6), ' Total: ',TOTAL=(40,6,PD,LENGTH=12))), OUTREC=(1,80), TRAILER1=(1:'GRAND TOTAL ',COUNT=(M11,LENGTH=10), ' ',TOTAL=(40,6,PD,LENGTH=14))
30,5 is the section key (department). After each department, TRAILER3 prints “Dept xxxxx” (the key), the section count, and the section total. SKIP=2L adds two blank lines. TRAILER1 prints once at the end with the grand count and total.
The key in SECTIONS=(position,length,...) must match the sort order. So if you sort by (30,5,CH,A), use SECTIONS=(30,5,...). All records with the same value in 30,5 are in one section; when the value changes, TRAILER3 is written and a new section starts. If the data were not sorted by 30,5, the same department could appear in multiple chunks and each chunk would get its own TRAILER3, giving wrong subtotals.
To print the current section key (e.g. department code) in the trailer line, reference its position and length in the TRAILER3= list. For example 30,5 inserts the 5-byte value at position 30 (the section key). So you get a line like “Dept SALES Count: 25 Total: 15000” where SALES comes from the key.
SKIP=nL (or equivalent) inserts n blank lines after the section (after TRAILER3) before the next section. So sections are visually separated. Without SKIP, the next section’s first record follows immediately after the TRAILER3 line.
Some products support multiple break levels: e.g. region (outer) and department (inner). You sort by region, then department. SECTIONS= defines both keys; you may have TRAILER3 for the inner break and TRAILER4 (or similar) for the outer break. When the inner key changes, the inner trailer is written; when the outer key changes, the outer trailer is written. Data must be sorted by all keys in order. Exact syntax (TRAILER3/TRAILER4, key order) is product-dependent.
| Item | Check |
|---|---|
| Sort key | Sort by the same (position, length) used in SECTIONS=. |
| TRAILER3 content | Include section key (pos,len), COUNT=, TOTAL= with correct field position and format. |
| TRAILER1 | Use for grand total at end; separate from TRAILER3. |
| SKIP | Use SKIP=nL if you want blank lines between sections. |
Imagine a list of sales by store. You want a line after each store that says “Store X: 10 sales, total $500.” First you put all the same-store sales together (sort by store). Then after the last sale for store A you print “Store A: …” (that’s TRAILER3). Then you print two blank lines (SKIP), then the sales for store B. At the very end you print “All stores: 100 sales, $5000” (that’s TRAILER1). Section subtotal patterns are just that: sort by group, then after each group print a summary line.
1. What must be true about the data for SECTIONS= subtotals to be correct?
2. What does SKIP=2L in SECTIONS= do?
3. Where do you put the section key value (e.g. department name) in the TRAILER3 line?
4. Can you have more than one level of section break (e.g. region then department)?
5. What is written first after the last detail record of a section: TRAILER3 or the next section's data?