MainframeMaster

COBOL Tutorial

REPORT-WRITER - COBOL Report Writer

Progress0 of 0 lessons

Overview

COBOL Report Writer lets you declare the shape of a report once and then simply GENERATElines. It prints page headings, details, footings, and control breaks for you.

🖨️ Analogy

Think of a bakery template for cake boxes. You design the template (REPORT SECTION), then every cake (record) is packaged identically by the machine (Report Writer).

Syntax and Usage

Defining a Report

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DATA DIVISION. REPORT SECTION. RD SALES-RPT CONTROLS ARE CUST-ID PAGE LIMIT IS 60 LINES HEADING 1. 01 PAGE-HEADING TYPE PAGE HEADING. 05 FILLER PIC X(40) VALUE 'SALES REPORT'. 01 CUST-HEADING TYPE CONTROL HEADING CUST-ID. 05 FILLER PIC X(12) VALUE 'CUSTOMER: '. 05 CUST-ID-OUT PIC X(10). 01 DETAIL-LINE TYPE DETAIL. 05 ITEM PIC X(20). 05 QTY PIC 9(5). 05 AMT PIC 9(7)V99 SUM. 01 CUST-FOOT TYPE CONTROL FOOTING CUST-ID. 05 FILLER PIC X(12) VALUE 'CUST TOTAL:'. 05 CUST-TOTAL PIC 9(9)V99. 01 PAGE-FOOT TYPE PAGE FOOTING. 05 FILLER PIC X(10) VALUE 'Page:'. 05 PAGE-NO PIC 9(5).

RD defines the report. Groups (PAGE HEADING/FOOTING, CONTROL HEADING/FOOTING, DETAIL) describe printed lines.

Producing the Report

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PROCEDURE DIVISION. OPEN INPUT SALES-FILE OPEN OUTPUT REPORT-FILE INIT-REPORT. MOVE 0 TO PAGE-NO READ-LOOP. READ SALES-FILE AT END GO TO FINISH. MOVE CUST-ID-IN TO CUST-ID-OUT MOVE ITEM-IN TO ITEM MOVE QTY-IN TO QTY MOVE AMT-IN TO AMT GENERATE SALES-RPT GO TO READ-LOOP. FINISH. CLOSE SALES-FILE REPORT-FILE STOP RUN.

GENERATE triggers the next logical group based on control breaks and page limits.

Practical Examples

Control Break Totals

Accumulate AMT with SUM, print in CONTROL FOOTING when CUST-ID changes.

Non-Code Example

  • Store sales are grouped by customer
  • Each page prints a heading with the report title
  • When the customer changes, a subtotal line prints
  • Page footer shows the current page number

Best Practices and Pitfalls

Best Practices

  • Keep detail lines narrow enough for target printers
  • Use SUM and CONTROL groups instead of manual totals
  • Document page limits and margins
  • Test with edge cases: last page, empty groups

Common Mistakes

MistakeProblemFix
Manual totals everywhereDuplicated logicUse SUM and control break groups
Ignoring page sizeTruncated linesMatch PAGE LIMIT to device

Quick Reference

FeatureUsageExample
Report definitionREPORT SECTION / RDRD SALES-RPT ...
Generate outputGENERATE report-nameGENERATE SALES-RPT
Control breaksCONTROLS ARE fieldCONTROLS ARE CUST-ID

Test Your Knowledge

1. What COBOL division contains Report Writer definitions?

  • IDENTIFICATION
  • ENVIRONMENT
  • DATA (REPORT SECTION)
  • PROCEDURE

2. Which statement produces output for a report?

  • DISPLAY
  • WRITE
  • GENERATE report-name
  • PRINT

3. Which groups commonly exist in a report?

  • PAGE HEADING / DETAIL / PAGE FOOTING
  • ENV HEADING / DATA HEADING
  • CLASS HEADING / OBJECT FOOTING
  • FILE HEADING / FILE FOOTING

4. How are control breaks handled?

  • With PERFORM ONLY
  • With CONTROL clause and CONTROL HEADING/FOOTING
  • With ACCEPT
  • With MOVE

5. Which counters are frequently used alongside Report Writer?

  • LINE-COUNTER, PAGE-COUNTER
  • PIXEL-COUNTER
  • CACHE-COUNTER
  • INDEX-COUNTER

Frequently Asked Questions