Capstone A: the report and control breaks
The report is the reason the file exists, and it is one pass: read a record, print it, add it to a subtotal, and when the region changes print the subtotal and start the next one. At the end, one grand total and a count of what was skipped. That is a control-break report, the oldest pattern in business data processing.
The shape of the report
The file is sorted by region: N1, N1, S2, S2, E3, E3. A control break needs that order and cannot work without it, which is why the sort lesson comes before this one in any course that teaches reports honestly.
C00001 Ada Lovelace 1234.56
C00002 Grace Hopper 2345.67
region N1 subtotal 3580.23
C00003 Alan Turing 456.78
C00004 Katherine Johnson 5678.90
region S2 subtotal 6135.68
C00005 Edsger Dijkstra 789.01
C00006 Barbara Liskov 1234.56
region E3 subtotal 2023.57
grand total 11739.48 over 6 customers, 2 skipped
Detail lines are indented, subtotal lines start at the left margin, and the grand total is the last line. The layout is not decoration: a reader scanning a thousand lines needs the subtotals to stand out, and a program that prints a parseable file instead of a report writes a header and fixed columns.
The program
IDENTIFICATION DIVISION.
PROGRAM-ID. REPORT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUS-F ASSIGN TO "customers.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-STATUS.
DATA DIVISION.
FILE SECTION.
FD CUS-F.
01 CUSTOMER-REC.
05 CUST-ID PIC X(6).
05 CUST-NAME PIC X(20).
05 CUST-REGION PIC X(2).
05 CUST-AMOUNT PIC 9(7)V99.
WORKING-STORAGE SECTION.
01 WS-STATUS PIC XX.
01 WS-EOF PIC X VALUE "N".
01 WS-SEED-LINE PIC X(37).
01 WS-LAST-REGION PIC X(2) VALUE SPACES.
01 WS-SUBTOTAL PIC 9(9)V99 VALUE 0.
01 WS-GRAND PIC 9(9)V99 VALUE 0.
01 WS-COUNT PIC 9(4) VALUE 0.
01 WS-SKIPPED PIC 9(4) VALUE 0.
01 WS-COUNT-OUT PIC Z(3)9.
01 WS-SKIP-OUT PIC Z(3)9.
01 WS-AMT-OUT PIC Z(6)9.99.
01 WS-SUB-OUT PIC Z(7)9.99.
01 WS-GRAND-OUT PIC Z(8)9.99.
01 WS-DETAIL PIC X(60).
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT CUS-F
IF WS-STATUS = "35"
PERFORM SEED-DATA
END-IF
CLOSE CUS-F
OPEN INPUT CUS-F
PERFORM UNTIL WS-EOF = "Y"
READ CUS-F
AT END MOVE "Y" TO WS-EOF
NOT AT END
PERFORM HANDLE-RECORD
END-READ
END-PERFORM
IF WS-LAST-REGION NOT = SPACES
PERFORM PRINT-BREAK
END-IF
CLOSE CUS-F
MOVE WS-GRAND TO WS-GRAND-OUT
MOVE WS-COUNT TO WS-COUNT-OUT
MOVE WS-SKIPPED TO WS-SKIP-OUT
DISPLAY "grand total " FUNCTION TRIM(WS-GRAND-OUT)
" over " FUNCTION TRIM(WS-COUNT-OUT) " customers, "
FUNCTION TRIM(WS-SKIP-OUT) " skipped"
STOP RUN.
SEED-DATA.
OPEN OUTPUT CUS-F
MOVE "C00001Ada Lovelace N1000123456" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00002Grace Hopper N1000234567" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00003Alan Turing S2000045678" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00004Katherine Johnson S2000567890" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00005Edsger Dijkstra E3000078901" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00006Barbara Liskov E3000123456" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00007Bad Region X9000010000" TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
MOVE "C00008Short " TO WS-SEED-LINE
WRITE CUSTOMER-REC FROM WS-SEED-LINE
CLOSE CUS-F.
HANDLE-RECORD.
IF CUST-AMOUNT IS NOT NUMERIC
ADD 1 TO WS-SKIPPED
EXIT PARAGRAPH
END-IF
EVALUATE CUST-REGION
WHEN "N1" CONTINUE
WHEN "S2" CONTINUE
WHEN "E3" CONTINUE
WHEN OTHER
ADD 1 TO WS-SKIPPED
EXIT PARAGRAPH
END-EVALUATE
IF CUST-REGION NOT = WS-LAST-REGION
IF WS-LAST-REGION NOT = SPACES
PERFORM PRINT-BREAK
END-IF
MOVE CUST-REGION TO WS-LAST-REGION
END-IF
MOVE CUST-AMOUNT TO WS-AMT-OUT
MOVE SPACES TO WS-DETAIL
STRING CUST-ID " " FUNCTION TRIM(CUST-NAME) " "
FUNCTION TRIM(WS-AMT-OUT) DELIMITED BY SIZE INTO WS-DETAIL
END-STRING
DISPLAY " " FUNCTION TRIM(WS-DETAIL)
ADD 1 TO WS-COUNT
ADD CUST-AMOUNT TO WS-SUBTOTAL
ADD CUST-AMOUNT TO WS-GRAND.
PRINT-BREAK.
MOVE WS-SUBTOTAL TO WS-SUB-OUT
DISPLAY "region " FUNCTION TRIM(WS-LAST-REGION)
" subtotal " FUNCTION TRIM(WS-SUB-OUT)
MOVE 0 TO WS-SUBTOTAL.
Above the listing stand IDENTIFICATION DIVISION. and PROGRAM-ID. REPORT.. The validation of the previous lesson is reduced to the two rules the report needs, because a record it cannot total has no place in a total: HANDLE-RECORD skips an amount that is not numeric or a region that is not one of the three, adds one to WS-SKIPPED and leaves the paragraph with EXIT PARAGRAPH.
The run
cobc -x -free -Wall -o report report.cob
./report
echo $?
C00001 Ada Lovelace 1234.56
C00002 Grace Hopper 2345.67
region N1 subtotal 3580.23
C00003 Alan Turing 456.78
C00004 Katherine Johnson 5678.90
region S2 subtotal 6135.68
C00005 Edsger Dijkstra 789.01
C00006 Barbara Liskov 1234.56
region E3 subtotal 2023.57
grand total 11739.48 over 6 customers, 2 skipped
0
Status 0 this time, not 4: the report skipped what it could not use and printed the counts, and a report that prints its skips is a report whose reader can decide what to do. The validation step of lesson 128 is what returns 4, because it is the step that decides whether the data may go on.
The control break
Three variables do the work: WS-LAST-REGION, which holds the region of the group being accumulated, WS-SUBTOTAL, and WS-GRAND. The paragraph compares the region of the record just read with the saved one:
HANDLE-RECORD.
IF CUST-AMOUNT IS NOT NUMERIC
ADD 1 TO WS-SKIPPED
EXIT PARAGRAPH
END-IF
EVALUATE CUST-REGION
WHEN "N1" CONTINUE
WHEN "S2" CONTINUE
WHEN "E3" CONTINUE
WHEN OTHER
ADD 1 TO WS-SKIPPED
EXIT PARAGRAPH
END-EVALUATE
IF CUST-REGION NOT = WS-LAST-REGION
IF WS-LAST-REGION NOT = SPACES
PERFORM PRINT-BREAK
END-IF
MOVE CUST-REGION TO WS-LAST-REGION
END-IF
MOVE CUST-AMOUNT TO WS-AMT-OUT
MOVE SPACES TO WS-DETAIL
STRING CUST-ID " " FUNCTION TRIM(CUST-NAME) " "
FUNCTION TRIM(WS-AMT-OUT) DELIMITED BY SIZE INTO WS-DETAIL
END-STRING
DISPLAY " " FUNCTION TRIM(WS-DETAIL)
ADD 1 TO WS-COUNT
ADD CUST-AMOUNT TO WS-SUBTOTAL
ADD CUST-AMOUNT TO WS-GRAND.
The first record of the file finds WS-LAST-REGION as spaces, so no subtotal is printed and the saved region becomes N1. Every later change prints the subtotal of the group that just ended before it starts the new one. After the loop, the last group has no following record to break it, and the IF WS-LAST-REGION NOT = SPACES after the END-PERFORM prints it. A report that forgets that line loses the last subtotal, and the grand total still adds up, so the mistake survives a careless check.
EXIT PARAGRAPH in HANDLE-RECORD is the other half: a skipped record must not reach the break logic, or a record with a bad region would close the group it never belonged to. The control-break lesson builds the same pattern from a smaller file; here it has to survive two kinds of bad record as well.
The totals checked by hand
A report is only as good as the arithmetic under it, and the arithmetic is checkable in one line per group:
N1 1234.56 + 2345.67 = 3580.23
S2 456.78 + 5678.90 = 6135.68
E3 789.01 + 1234.56 = 2023.57
grand 3580.23 + 6135.68 + 2023.57 = 11739.48
skipped 2 records, 6 accepted, 8 read
Every number on the page agrees with the printed report. That is the check a reader can do without the program, and it is the check to do before believing a report: if the subtotals add to the grand total and the counts add to the lines, the loop is right; if they do not, no amount of staring at the program will be as fast as the arithmetic.
Capstone A is complete: a layout, a reader that validates, and a report with control breaks. Capstone B takes the same customer file into an indexed file with a balance that transactions change, which is the other half of what a batch system does.
Exercises
- Sort the file by name inside the program and report by the first letter of the name. Which variable becomes the break field?
- Add a page counter: a form feed and a heading every twenty detail lines. Which variable decides when to print the heading?
- Print the count of customers in each subtotal line. Which counter has to be reset where the subtotal is reset?
- Remove the IF WS-LAST-REGION NOT = SPACES after the loop and run the report. Which line is missing, and does the grand total change?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What must be true of the file for a control-break report?
Answer
It is sorted on the field the report breaks on
The data is sorted N1, N1, S2, S2, E3, E3, and the break compares neighbours.
-
Why is the subtotal printed after the loop as well as inside it?
Answer
The last group has no following record to break it
Without that line the last subtotal is lost and the grand total still adds up.
-
What is the grand total of the report?
Answer
11739.48
3580.23 plus 6135.68 plus 2023.57 is 11739.48.
-
Why does the report return 0 while the validation returned 4?
Answer
The report printed its skips and did its job; the validation decides whether the data goes on
The report says over 6 customers, 2 skipped, and exits 0.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(4)V99 VALUE 10.50. 01 B PIC 9(4)V99 VALUE 4.25. 01 C PIC 9(5)V99. PROCEDURE DIVISION. COMPUTE C = A + B DISPLAY C. STOP RUN.Answer
0014.75
Ten fifty and four twenty-five make fourteen seventy-five.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 R PIC X(2) VALUE "N1". 01 L PIC X(2) VALUE SPACES. PROCEDURE DIVISION. IF R NOT = L DISPLAY "break" ELSE DISPLAY "same" END-IF. STOP RUN.Answer
break
The saved region is spaces and the record region is N1.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9(3) VALUE 6. 01 S PIC Z(2)9. PROCEDURE DIVISION. MOVE N TO S DISPLAY "over " FUNCTION TRIM(S) " customers". STOP RUN.Answer
over 6 customers
Z(2)9 suppresses the leading zeros and TRIM removes the spaces left in front.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 T. 05 T-A PIC 9(2) OCCURS 3 TIMES. 01 I PIC 9(2). 01 S PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE 5 TO T-A(1) MOVE 10 TO T-A(2) MOVE 15 TO T-A(3) PERFORM VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN 3 ADD T-A(I) TO S END-PERFORM. DISPLAY S. STOP RUN.Answer
030
Five plus ten plus fifteen is thirty.
-
What does EXIT PARAGRAPH do in HANDLE-RECORD?
Answer
Leaves the paragraph without printing or totalling the record
A skipped record must not reach the break logic.
-
Which check proves the loop dropped nothing?
Answer
The detail lines and the subtotals add up, and accepted plus skipped equals the records read
The page checks six customers and two skipped against eight lines.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev