BudiBadu Logo
Samplebadu

COBOL by Example: Report Writer

COBOL 2002

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).

Code Breakdown

6
REPORT IS SALES-REPORT. In the FD, this clause links the physical file to the logical report definition.
9
RD SALES-REPORT. Report Definition. Starts the description of the report's layout.
10
PAGE LIMIT 60 LINES. Tells the system when to trigger a page break and print a new header.
14
TYPE IS PAGE HEADING. This group is automatically printed at the top of every new page.
17
TYPE IS DETAIL. This group represents one row of data. It is printed whenever GENERATE is called.
19
SOURCE S-AMT. Binds the report column to the variable S-AMT. The system fetches the current value automatically.
28
INITIATE SALES-REPORT. Sets up counters and prepares the report engine.
31
GENERATE DETAIL-LINE. Prints the detail line using current variable values.
36
TERMINATE SALES-REPORT. Prints final footers and closes the report logic.