COBOL by Example: Report Writer
Automating report generation with Report Writer module, defining page layouts in REPORT SECTION with RD (Report Definition), handling automatic page breaks and line counting, using INITIATE GENERATE and TERMINATE for declarative reporting, and implementing control footings for subtotals.
Code
IDENTIFICATION DIVISION.
PROGRAM-ID. REPORT-DEMO.
DATA DIVISION.
FILE SECTION.
FD PRINT-FILE REPORT IS SALES-REPORT.
REPORT SECTION.
RD SALES-REPORT
PAGE LIMIT 60 LINES
HEADING 1
FIRST DETAIL 3.
01 TYPE IS PAGE HEADING.
05 LINE 1 COLUMN 20 VALUE "Sales Report".
01 DETAIL-LINE TYPE IS DETAIL.
05 LINE PLUS 1.
05 COLUMN 5 PIC 9(5) SOURCE S-ID.
05 COLUMN 15 PIC $ZZ,ZZ9.99 SOURCE S-AMT.
WORKING-STORAGE SECTION.
01 S-ID PIC 9(5).
01 S-AMT PIC 9(5)V99.
PROCEDURE DIVISION.
OPEN OUTPUT PRINT-FILE.
INITIATE SALES-REPORT.
MOVE 1 TO S-ID MOVE 100.00 TO S-AMT.
GENERATE DETAIL-LINE.
MOVE 2 TO S-ID MOVE 250.50 TO S-AMT.
GENERATE DETAIL-LINE.
TERMINATE SALES-REPORT.
CLOSE PRINT-FILE.
STOP RUN.Explanation
The Report Writer module allows you to describe the physical layout of a report (headings, detail lines, footers) in the REPORT SECTION. COBOL then handles the tedious tasks of page breaks, line counting, page numbering, and even summing totals automatically.
Instead of writing WRITE statements and counting lines manually, you use INITIATE to start the report, GENERATE to print a detail line, and TERMINATE to finish. This declarative approach was very advanced for its time and separates the report layout from the program logic.
The RD (Report Definition) defines global attributes like page length. Inside, you define "Report Groups" with TYPE clauses. Common types include PAGE HEADING (prints at top of page), DETAIL (prints for each record), and CONTROL FOOTING (prints totals when a key changes).

