COBOL Tutorial

Progress0 of 0 lessons

COBOL Reporting

Reporting in COBOL means producing formatted output for people to read: printed reports, report files, or listings with headers, detail lines, totals, and page breaks. You can do this by writing lines to a file with WRITE and controlling the layout yourself, or by using the Report Writer feature (REPORT SECTION and verbs like INITIATE, GENERATE, TERMINATE). This page gives an overview of reporting approaches and when to use each.

Explain Like I'm Five: Reporting

A report is like a letter: it has a title at the top (header), a list of things (detail lines), and sometimes a total or summary at the bottom (footer). In COBOL you can write that "letter" by building each line (title, then each row of data, then totals) and writing it to a file or printer. Another way is to describe the report to COBOL (Report Writer), and it helps you print the right things in the right place.

Types of Reports

Reports often include: a report title and column headings (headers), one line per transaction or record (detail lines), subtotals when a group changes (control breaks), and grand totals at the end. Page breaks and page numbers are common. The data might come from sequential files, and the output is usually a sequential file (e.g. for printing or viewing).

Two Main Approaches

Reporting approaches in COBOL
ApproachDescriptionWhen to use
Explicit WRITEYou open a file, maintain line/page counts, build each line, WRITE it. Full control over layout and logic.Custom reports, full control, or when Report Writer is not used.
Report WriterDefine report in REPORT SECTION (RD, report groups). Use INITIATE, GENERATE, TERMINATE.Standard report layout; want declarative groups and automatic paging.

Explicit WRITE Approach

You define an output file (FD) and a record description for the report line (or several: header line, detail line, total line). You OPEN the file, then in your procedure you maintain a line counter and optionally a page counter. When you need a new page, you WRITE a header line (and perhaps ADVANCING PAGE), reset the line counter, and continue. For each detail record you build the detail line, WRITE it, and increment the line counter. At control breaks you write subtotal lines; at end of job you write grand totals and CLOSE the file.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
*> Simplified idea: one detail line type FD REPORT-FILE. 01 REPORT-LINE PIC X(132). PROCEDURE DIVISION. OPEN OUTPUT REPORT-FILE PERFORM PRINT-HEADER PERFORM UNTIL no-more-records READ IN-FILE ... MOVE ... TO REPORT-LINE WRITE REPORT-LINE *> Check for control break, new page, etc. END-PERFORM PERFORM PRINT-TOTALS CLOSE REPORT-FILE.

Report Writer Approach

Report Writer uses a REPORT SECTION in the DATA DIVISION. You define a report (RD) and report groups (e.g. TYPE PAGE HEADING, TYPE DETAIL, TYPE CONTROL FOOTING). The file is declared with REPORT clause. In the PROCEDURE DIVISION you INITIATE the report, then GENERATE detail or other groups (often in a loop), and finally TERMINATE the report. The system handles spacing and paging according to the report definition. For full syntax and options, see the Report Writer tutorial.

Step-by-Step: Simple Report with WRITE

  • Define an output file (FD) and record layouts for header, detail, and total lines.
  • OPEN OUTPUT the report file. Initialize line counter (e.g. 99 so first WRITE triggers a header) and any totals.
  • Before each WRITE, check if you need a new page (e.g. line count > 60); if so, WRITE header with ADVANCING PAGE and reset line count.
  • For each input record, build the detail line, WRITE it, increment line count. On control field change, write subtotal line(s) and reset subtotal accumulators.
  • After all input, write grand total line(s), CLOSE the file.

Best Practices

  • Use consistent column positions and edited PICTUREs so numbers and text align.
  • Print headers on every page and include page numbers when the report is multi-page.
  • Handle empty data (no records) so the report still has a header and a clear message or no misleading totals.

Test Your Knowledge

1. What is the main way to produce a simple report in COBOL without Report Writer?

  • Use INITIATE and GENERATE only
  • Open a file, move data to a record, WRITE the record; repeat with headers/footers as needed
  • Use DISPLAY for all lines
  • Use REPORT SECTION only

2. Report Writer typically uses which verbs?

  • READ and WRITE only
  • INITIATE, GENERATE, TERMINATE
  • OPEN and CLOSE only
  • MOVE and DISPLAY only

Related Concepts

Related Pages