Reading — step 1 of 5
Learn
COBOL has a built-in report-generation feature: REPORT WRITER. It declaratively describes a report's structure and the runtime emits formatted output.
Most shops moved to manual formatting (PERFORM + edit pictures) for flexibility, but Report Writer remains in many production systems.
Anatomy
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT REPORT-FILE ASSIGN TO 'output.txt'.
DATA DIVISION.
FILE SECTION.
FD REPORT-FILE.
01 REPORT-LINE PIC X(80).
REPORT SECTION.
RD SALES-REPORT
CONTROLS ARE FINAL REGION DEPT
PAGE LIMIT 60 LINES
HEADING 1
FIRST DETAIL 5
LAST DETAIL 55.
01 TYPE PAGE HEADING.
02 LINE 1 COLUMN 25 PIC X(15) VALUE 'SALES BY DEPT'.
02 LINE 3 COLUMN 1 PIC X(10) VALUE 'REGION'.
02 LINE 3 COLUMN 12 PIC X(10) VALUE 'DEPT'.
02 LINE 3 COLUMN 23 PIC X(10) VALUE 'SALES'.
01 DETAIL-LINE TYPE DETAIL.
02 COLUMN 1 PIC X(10) SOURCE REGION-IN.
02 COLUMN 12 PIC X(10) SOURCE DEPT-IN.
02 COLUMN 23 PIC $$$,$$9.99 SOURCE AMOUNT-IN.
01 TYPE CONTROL FOOTING DEPT.
02 COLUMN 1 PIC X(15) VALUE 'DEPT TOTAL:'.
02 COLUMN 23 PIC $$$$,$$9.99 SUM AMOUNT-IN.
01 TYPE CONTROL FOOTING FINAL.
02 COLUMN 1 PIC X(15) VALUE 'GRAND TOTAL:'.
02 COLUMN 23 PIC $$$$$,$$9.99 SUM AMOUNT-IN.
PROCEDURE DIVISION.
OPEN OUTPUT REPORT-FILE.
INITIATE SALES-REPORT.
PERFORM UNTIL END-OF-INPUT
READ INPUT-FILE AT END SET END-OF-INPUT TO TRUE
GENERATE DETAIL-LINE
END-PERFORM.
TERMINATE SALES-REPORT.
CLOSE REPORT-FILE.
STOP RUN.
What's happening
RD(Report Description) — defines the report layoutPAGE LIMIT— page sizeTYPE PAGE HEADING— runs at top of each pageTYPE DETAIL— runs per recordTYPE CONTROL FOOTING— runs when a control field changes (auto-subtotals)INITIATE— start the reportGENERATE— emit one detail lineTERMINATE— finish, emit final footings
The runtime tracks:
- Page breaks
- Control breaks (when REGION or DEPT changes)
- Subtotals (the
SUMclauses accumulate automatically) - Headings on each page
Why it's clever
For 1980s mainframe reports, this declarative model was elegant — describe the shape, let the runtime handle pagination and subtotals. No manual page counters, no manual subtotal accumulators, no manual heading reprinting.
Why most modern code skips it
- Limited flexibility — what if you want HTML output? Subtotals on multiple keys with custom logic?
- Hard to debug — the runtime hides the work
- Modern preference: explicit code, predictable behavior
That said, maintaining COBOL means understanding REPORT WRITER. Many production reports were written in it — and rewriting them as procedural code is a significant migration.
Modern alternatives
- Procedural code with edit pictures (covered in Fundamentals)
- External report tools (Crystal Reports, etc.) consuming COBOL output
- Database-backed reporting (move data to SQL, run reports there)
- Modern programming languages alongside COBOL via JNI/CALL bridges
For Judge0 we can't easily run a report file, but the pattern is critical to know if you're touching mainframe code.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…