COBOL Report Formatting
Printing reports is the other half of batch processing. A COBOL report is built from print lines: group
items in WORKING-STORAGE that contain FILLER literals for fixed text and edited fields for the values. You move
data into the fields and write (or display) the whole line.
Heading, detail and total lines
IDENTIFICATION DIVISION.
PROGRAM-ID. SIMPLE-REPORT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-HEADING-1.
05 FILLER PIC X(12) VALUE "TAD MARKET".
05 FILLER PIC X(24)
VALUE "STOCK VALUATION REPORT".
01 WS-HEADING-2.
05 FILLER PIC X(14) VALUE "PRODUCT".
05 FILLER PIC X(6) VALUE " QTY".
05 FILLER PIC X(10) VALUE " PRICE".
05 FILLER PIC X(12) VALUE " VALUE".
01 WS-DETAIL.
05 DL-NAME PIC X(14).
05 DL-QTY PIC Z,ZZ9.
05 FILLER PIC X VALUE SPACE.
05 DL-PRICE PIC $$,$9.99.
05 FILLER PIC X VALUE SPACE.
05 DL-VALUE PIC $$,$9.99.
01 WS-TOTAL-LINE.
05 FILLER PIC X(31) VALUE "TOTAL STOCK VALUE".
05 TL-VALUE PIC $$,$9.99.
01 WS-RULE PIC X(42) VALUE ALL "-".
01 WS-ITEM-DATA.
05 FILLER PIC X(25) VALUE "Espresso beans00120002450".
05 FILLER PIC X(25) VALUE "Paper filters 01500000399".
05 FILLER PIC X(25) VALUE "Grinder 00008018900".
01 WS-ITEMS REDEFINES WS-ITEM-DATA.
05 WS-ITEM OCCURS 3 TIMES.
10 IT-NAME PIC X(14).
10 IT-QTY PIC 9(5).
10 IT-PRICE PIC 9(4)V99.
01 WS-I PIC 9.
01 WS-VALUE PIC 9(7)V99.
01 WS-GRAND-TOTAL PIC 9(7)V99 VALUE 0.
PROCEDURE DIVISION.
DISPLAY WS-HEADING-1
DISPLAY WS-HEADING-2
DISPLAY WS-RULE
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 3
COMPUTE WS-VALUE = IT-QTY (WS-I) * IT-PRICE (WS-I)
ADD WS-VALUE TO WS-GRAND-TOTAL
MOVE IT-NAME (WS-I) TO DL-NAME
MOVE IT-QTY (WS-I) TO DL-QTY
MOVE IT-PRICE (WS-I) TO DL-PRICE
MOVE WS-VALUE TO DL-VALUE
DISPLAY WS-DETAIL
END-PERFORM
DISPLAY WS-RULE
MOVE WS-GRAND-TOTAL TO TL-VALUE
DISPLAY WS-TOTAL-LINE
STOP RUN.TAD MARKET STOCK VALUATION REPORT
PRODUCT QTY PRICE VALUE
------------------------------------------
Espresso beans 120 $24.50 $2,940.00
Paper filters 1,500 $3.99 $5,985.00
Grinder 8 $189.00 $1,512.00
------------------------------------------
TOTAL STOCK VALUE $10,437.00
Designing a report starts on (squared) paper: decide the column positions, then size the FILLER items to match.
The edited pictures do all the formatting.
Control breaks
A control break report prints a subtotal whenever a key field changes, for example a total per region. The input must be sorted by that key. The logic: remember the previous key; when the current key differs, print the subtotal for the previous group and reset it.
IDENTIFICATION DIVISION.
PROGRAM-ID. CONTROL-BREAK.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SALES-DATA.
05 FILLER PIC X(17) VALUE "EAST Ana 03000".
05 FILLER PIC X(17) VALUE "EAST Lea 07000".
05 FILLER PIC X(17) VALUE "NORTH Tom 05250".
05 FILLER PIC X(17) VALUE "WEST Kim 04500".
05 FILLER PIC X(17) VALUE "WEST Raj 09100".
01 WS-SALES REDEFINES WS-SALES-DATA.
05 WS-SALE OCCURS 5 TIMES.
10 SL-REGION PIC X(6).
10 SL-NAME PIC X(6).
10 SL-AMOUNT PIC 999V99.
01 WS-I PIC 9.
01 WS-PREV-REGION PIC X(6) VALUE SPACES.
01 WS-REGION-TOTAL PIC 9(5)V99 VALUE 0.
01 WS-GRAND-TOTAL PIC 9(6)V99 VALUE 0.
01 WS-DETAIL.
05 FILLER PIC X(2) VALUE SPACES.
05 DL-NAME PIC X(6).
05 DL-AMOUNT PIC ZZ,ZZ9.99.
01 WS-SUBTOTAL.
05 FILLER PIC X(8) VALUE " Total ".
05 ST-REGION PIC X(6).
05 ST-AMOUNT PIC ZZZ,ZZ9.99.
01 ED-GRAND PIC ZZZ,ZZ9.99.
PROCEDURE DIVISION.
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
IF SL-REGION (WS-I) NOT = WS-PREV-REGION
IF WS-PREV-REGION NOT = SPACES
PERFORM PRINT-SUBTOTAL
END-IF
MOVE SL-REGION (WS-I) TO WS-PREV-REGION
DISPLAY "Region " SL-REGION (WS-I)
END-IF
MOVE SL-NAME (WS-I) TO DL-NAME
MOVE SL-AMOUNT (WS-I) TO DL-AMOUNT
DISPLAY WS-DETAIL
ADD SL-AMOUNT (WS-I) TO WS-REGION-TOTAL WS-GRAND-TOTAL
END-PERFORM
PERFORM PRINT-SUBTOTAL
MOVE WS-GRAND-TOTAL TO ED-GRAND
DISPLAY "Grand total " ED-GRAND
STOP RUN.
PRINT-SUBTOTAL.
MOVE WS-PREV-REGION TO ST-REGION
MOVE WS-REGION-TOTAL TO ST-AMOUNT
DISPLAY WS-SUBTOTAL
MOVE 0 TO WS-REGION-TOTAL.Region EAST
Ana 30.00
Lea 70.00
Total EAST 100.00
Region NORTH
Tom 52.50
Total NORTH 52.50
Region WEST
Kim 45.00
Raj 91.00
Total WEST 136.00
Grand total 288.50
Two details are easy to forget: the last group has no following key change, so its subtotal is printed after the loop, and the subtotal must be reset after printing.
Writing a print file with ADVANCING
Real reports are written to a print file. WRITE … AFTER ADVANCING n LINES controls the vertical spacing, and
AFTER ADVANCING PAGE starts a new page. The program below writes a small report and then reads it back to show
what the file contains:
IDENTIFICATION DIVISION.
PROGRAM-ID. PRINT-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PRINT-OUT ASSIGN TO "summary.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD PRINT-OUT.
01 PRINT-LINE PIC X(30).
WORKING-STORAGE SECTION.
01 WS-EOF PIC X VALUE "N".
01 WS-LINE-NO PIC 99 VALUE 0.
PROCEDURE DIVISION.
OPEN OUTPUT PRINT-OUT
WRITE PRINT-LINE FROM "MONTHLY SUMMARY"
AFTER ADVANCING 1 LINE
WRITE PRINT-LINE FROM "Accounts opened: 12"
AFTER ADVANCING 2 LINES
WRITE PRINT-LINE FROM "Accounts closed: 3"
AFTER ADVANCING 1 LINE
CLOSE PRINT-OUT
OPEN INPUT PRINT-OUT
PERFORM UNTIL WS-EOF = "Y"
READ PRINT-OUT
AT END MOVE "Y" TO WS-EOF
NOT AT END
ADD 1 TO WS-LINE-NO
DISPLAY WS-LINE-NO ": " PRINT-LINE
END-READ
END-PERFORM
CLOSE PRINT-OUT
STOP RUN.01:
02: MONTHLY SUMMARY
03:
04: Accounts opened: 12
05: Accounts closed: 3
Every WRITE here says how far to move down before printing: one line means "the next line", two lines leave
one blank line in between. Because the first WRITE also advances one line from the top, the file starts with an
empty line. Be consistent: if some writes use ADVANCING and others don't, GnuCOBOL's line spacing becomes hard
to predict.
Page-oriented reports also count lines and print the headings again (with a page number) whenever the page is full.
Note: COBOL also has a declarative Report Writer (
REPORT SECTION,RD,GENERATE) that produces headings, control breaks and footings from a description. GnuCOBOL supports it, but most shops write reports by hand as shown here.
Test yourself
What triggers a subtotal in a control break report?
- Every fifth record
- A change in the value of the sorted key field
- The end of a page
- A negative amount
Why must the last subtotal be printed after the loop?
- It is optional
- No key change follows the last group, so the break logic never fires for it
- Because the loop skips the last record
- To reset the total
How do you leave a blank line before a line in a print file?
- WRITE … AFTER ADVANCING 2 LINES
- DISPLAY SPACE
- WRITE … BLANK
- SKIP 1