Reports & Control Breaks
A control break occurs when the value of a key field changes between consecutive records of a sorted file.
24.1 Control-break processing
A control break occurs when the value of a key field changes between consecutive records of a sorted file. At each break you print subtotals and reset accumulators. Multi-level breaks (e.g., region → branch → rep) nest: a higher-level break forces all lower-level breaks first.
24.2 Complete example: sales report by region
Input (sales-sorted.txt, sorted by region): region 10, rep 20, amount 9 digits (7.2).
Key points: save the previous key, compare before processing each record, perform the break routine on change and once more at end of file, then print grand totals. For multi-page reports, keep a line counter: when it reaches the page size, increment a page counter and rewrite the headings (on z/OS printer files with WRITE ... AFTER ADVANCING PAGE). Report Writer does this automatically.
IDENTIFICATION DIVISION.
PROGRAM-ID. SALESRPT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-FILE ASSIGN TO "sales-sorted.txt"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT REPORT-FILE ASSIGN TO "sales-report.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD SALES-FILE.
01 SALES-REC.
05 SL-REGION PIC X(10).
05 SL-REP PIC X(20).
05 SL-AMOUNT PIC 9(7)V99.
FD REPORT-FILE.
01 REPORT-LINE PIC X(80).
WORKING-STORAGE SECTION.
01 WS-EOF PIC X VALUE "N".
88 END-OF-SALES VALUE "Y".
01 WS-PREV-REGION PIC X(10).
01 WS-REGION-TOTAL PIC 9(9)V99 VALUE 0.
01 WS-GRAND-TOTAL PIC 9(11)V99 VALUE 0.
01 WS-REC-COUNT PIC 9(7) VALUE 0.
01 HDR-1 PIC X(30)
VALUE "QUARTERLY SALES REPORT".
01 HDR-2.
05 FILLER PIC X(12) VALUE "REGION".
05 FILLER PIC X(22) VALUE
…24.3 Report Writer
The Report Writer module (part of COBOL-85, optional in later standards) generates reports declaratively. GnuCOBOL 3.x supports it; IBM Enterprise COBOL does not support it natively (IBM offers a separate Report Writer precompiler); Micro Focus supports it.
INITIATE, GENERATE, and TERMINATE handle headings, page breaks, control breaks, and SUM totals automatically. GROUP INDICATE prints the region only on the first detail line of each group.
FD REPORT-FILE
REPORT IS SALES-REPORT.
* ...
REPORT SECTION.
RD SALES-REPORT
CONTROLS ARE FINAL SL-REGION
PAGE LIMIT IS 60 LINES
HEADING 1
FIRST DETAIL 4
LAST DETAIL 56.
01 TYPE IS PAGE HEADING.
05 LINE 1.
10 COLUMN 1 PIC X(5) VALUE "PAGE ".
10 COLUMN 6 PIC ZZ9 SOURCE PAGE-COUNTER.
01 SALES-DETAIL TYPE IS DETAIL.
05 LINE PLUS 1.
10 COLUMN 1 PIC X(10) SOURCE SL-REGION
GROUP INDICATE.
10 COLUMN 13 PIC X(20) SOURCE SL-REP.
10 COLUMN 35 PIC Z,ZZZ,ZZ9.99 SOURCE SL-AMOUNT.
01 TYPE IS CONTROL FOOTING SL-REGION.
05 LINE PLUS 2.
10 COLUMN 13 PIC X(12) VALUE "REGION TOTAL".
10 COLUMN 33 PIC ZZZ,ZZZ,ZZ9.99 SUM SL-AMOUNT.
* A CONTROL FOOTING FINAL group would print the grand total
* ...
PROCEDURE DIVISION.
OPEN INPUT SALES-FILE OUTPUT REPORT-FILE
INITIATE SALES-REPORT
PERFORM UNTIL END-OF-SALES
READ SALES-FILE
…🧪 Part II Exercises
Write a program demonstrating every row of the MOVE rules table (§12.2), including a group move that corrupts a COMP-3 field.
Compute compound interest year by year for 10 years with COMPUTE ... ROUNDED, printing a table with edited amounts; add ON SIZE ERROR handling.
Implement a grading program with EVALUATE TRUE, then a shipping-cost calculator with EVALUATE ... ALSO ... (zone × weight band).
Print a calendar for any month (input YYYYMM) using INTEGER-OF-DATE and nested PERFORM VARYING.
Load 20 product codes and prices into a sorted table, then look up codes typed by the user with both SEARCH and SEARCH ALL; count comparisons.
Parse a CSV line with UNSTRING into five fields, validate the numeric ones with TEST-NUMVAL, and rebuild a pipe-free fixed-width record with STRING.
Write a word counter: read a text file (line sequential) and report lines, words, and characters, plus the frequency of each vowel using INSPECT.
Build an indexed customer file from a sequential input; then write a menu program to add, inquire, update, delete, and browse records with full FILE STATUS checking.
Sort an employee file by department (ascending) and salary (descending) with an INPUT PROCEDURE that drops terminated employees and an OUTPUT PROCEDURE that prints the result.
Produce a two-level control-break report (region → sales rep) with page headings, rep subtotals, region subtotals, and a grand total; then rewrite it with Report Writer in GnuCOBOL.