COBOL WRITE AFTER ADVANCING: Control Report Page Breaks Without Losing Detail Lines
A batch report can have correct totals and still be operationally unusable when headings appear in the middle of detail, page numbers drift, or a final page is never closed cleanly. This tutorial shows a small, controlled pattern for `WRITE ... AFTER ADVANCING` page control and the evidence to review before changing an existing report.
A batch report can have correct totals and still be operationally unusable when headings appear in the middle of detail, page numbers drift, or a final page is never closed cleanly. This tutorial shows a small, controlled pattern for WRITE ... AFTER ADVANCING page control and the evidence to review before changing an existing report.
The problem is not just spacing
A report program normally writes to a line-sequential printer-style output file. AFTER ADVANCING n LINES asks the runtime to position the output after writing a record; AFTER ADVANCING PAGE starts a new page according to the file and compiler conventions in use. That sounds simple until page control is scattered through business logic.
Typical symptoms are easy to misread:
- The first detail line of a new page appears above its heading.
- A heading is printed twice after a subtotal.
- Page 2 is almost empty because a page break occurred after, rather than before, the last permissible detail line.
- A support change fixes visual layout but silently changes the report’s record count or skips a control-break total.
The central decision is whether the next detail fits before writing it. Do not decide after it has already been written. Treat page positioning as output control, separate from the business calculation that produced the detail.
This example is fictional: a daily exception report has a heading plus up to 55 printed detail lines per page. Its source records and amount rules are not important; the page-control sequence is.
A minimal page-control design
Keep three pieces of state explicit:
| State | Meaning | When it changes |
|---|---|---|
| WS-PAGE-NO | Number printed in the heading | Each time a new page heading is written |
| WS-LINES-USED | Detail lines already printed on the current page | After each detail write; reset after heading |
| WS-MAX-DETAIL-LINES | Detail capacity, excluding headings | A named constant, reviewed with the layout |
The heading routine owns the page advance and reset. The detail routine checks capacity first, then writes exactly one detail. This prevents the main loop from having several competing rules for line counts.
WORKING-STORAGE SECTION.
01 WS-PAGE-NO PIC 9(4) COMP VALUE 0.
01 WS-LINES-USED PIC 9(3) COMP VALUE 0.
01 WS-MAX-DETAIL-LINES PIC 9(3) COMP VALUE 55.
01 WS-FIRST-PAGE PIC X VALUE 'Y'.
PROCEDURE DIVISION.
PERFORM UNTIL END-OF-INPUT
READ EXCEPTION-FILE
AT END
MOVE 'Y' TO END-OF-INPUT
NOT AT END
PERFORM WRITE-DETAIL
END-READ
END-PERFORM
PERFORM WRITE-FINAL-TOTALS
CLOSE REPORT-FILE
GOBACK.
WRITE-DETAIL.
IF WS-FIRST-PAGE = 'Y'
PERFORM WRITE-PAGE-HEADING
MOVE 'N' TO WS-FIRST-PAGE
END-IF
IF WS-LINES-USED >= WS-MAX-DETAIL-LINES
PERFORM WRITE-PAGE-HEADING
END-IF
MOVE INPUT-ACCOUNT TO RPT-ACCOUNT
MOVE INPUT-AMOUNT TO RPT-AMOUNT
WRITE REPORT-RECORD AFTER ADVANCING 1 LINE
ADD 1 TO WS-LINES-USED.
WRITE-PAGE-HEADING.
ADD 1 TO WS-PAGE-NO
MOVE WS-PAGE-NO TO RPT-PAGE-NO
WRITE REPORT-RECORD FROM RPT-HEADING
AFTER ADVANCING PAGE
WRITE REPORT-RECORD FROM RPT-COLUMN-HEADINGS
AFTER ADVANCING 1 LINE
MOVE 0 TO WS-LINES-USED.
```
The first call starts a page and writes the heading. On later calls, the same routine advances to the next page before the column headings. The condition uses >=, not =, so an unexpected line-count change cannot allow a detail line beyond the intended capacity.
Local copybooks may define separate record layouts for heading, column headings, detail, and totals through REDEFINES. That is fine, but move only the relevant layout before each WRITE. A stale field from a prior layout can create confusing output that looks like a spacing defect.
Work through the boundary case
Assume 55 details fit after the heading and column heading. After detail 55, WS-LINES-USED is 55. Before detail 56, the program invokes WRITE-PAGE-HEADING, resets the count to zero, then writes detail 56. The new page therefore contains:
Page 0002
Account Reason Amount
------- ------------------- --------
A10482 LIMIT EXCEEDED 125.00
There is no special case for record 56 in the read loop. That matters because a report can receive 0, 1, 55, 56, or thousands of records. Page behavior should depend on output state, not on a particular input sequence number.
A common incorrect alternative is to write the detail, increment the count, and then page-break when the count reaches 55. That breaks after the last usable line, often leaving the next heading separated from its first detail in a way that becomes worse when totals or blank separators are added.
Decisions that need local confirmation
WRITE ... AFTER ADVANCING PAGE is not a universal visual-layout guarantee. Printer control behavior can depend on the FD definition, runtime, spool settings, and whether the output is a true printer file, a line-sequential dataset, or a downstream formatting process. Some sites use carriage-control conventions or report-writer facilities instead of this pattern.
Confirm these points before assuming a defect is in COBOL:
1. Identify the DD statement and dataset attributes for the actual report output, including any site-specific carriage-control requirement.
2. Check whether a scheduler, print transform, or distribution tool adds banners, headers, or page breaks after the program closes the file.
3. Establish whether RPT-HEADING, column headings, subtotal lines, and final totals each consume a printed line in the chosen design.
4. Find every WRITE REPORT-RECORD in the program. A one-off AFTER ADVANCING in an error branch can invalidate a simple count.
5. Determine whether a restart may append to an existing report. Restart output needs its own page and duplication rules; do not infer them from a clean run.
Failure cases worth testing
A useful test set is small but deliberate. For each test, retain the spool output and the program’s input and output counts.
| Input details | What to inspect |
|---:|---|
| 0 | No unintended page heading; final totals follow the agreed empty-report convention |
| 1 | Heading precedes the one detail; page number begins correctly |
| 55 | All details are on one page; no trailing empty page is created |
| 56 | Detail 56 is below a new heading on page 2 |
| 110 or 111 | Repeated boundary behavior; page numbers and detail counts remain consistent |
| Control-break near line 55 | Subtotal, heading, and next detail occur in the required order |
Do not judge only by page count. Reconcile the number of detail records written with the accepted input records, and separately reconcile control totals. A report layout correction must not conceal a change to selection or calculation logic.
Before promoting a report-layout change
- Compare the changed source with the current production member and identify all output writes affected.
- Compile and execute in the approved non-production environment with boundary-sized test input.
- Inspect the actual spool or rendered report, not just the program return code.
- Reconcile input, accepted, rejected, detail, and total counts according to the report’s control rules.
- Obtain review for any change that alters carriage control, DD attributes, restart behavior, or report-distribution routing.
- Do not rerun a production job merely to improve formatting. Establish whether it creates downstream files, DB2 updates, notices, or duplicate distribution first.
Practical takeaway
Reliable COBOL pagination comes from checking capacity before writing the next detail, concentrating page advancement in one routine, and proving the result with boundary-sized spool output. Keep the report’s layout rule visible, but verify the local print pipeline before treating a page-break change as safe.