MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Report Generation

Report generation in COBOL is the process of creating formatted, professional output documents from data. Reports are essential for business applications, providing readable summaries, transaction listings, financial statements, and analytical data. Effective report generation involves organizing data logically, applying proper formatting, calculating totals and subtotals, handling page breaks, and creating clear, professional layouts. Mastering report generation is crucial for creating business-ready output in mainframe COBOL applications.

What is Report Generation?

Report generation transforms raw data into formatted, readable documents. A well-designed report includes:

  • Headers: Report title, date, page numbers, column headings
  • Detail lines: Individual data records formatted for readability
  • Control breaks: Grouping data and showing subtotals
  • Totals and subtotals: Aggregated data at group and report levels
  • Footers: Summary information, totals, page information
  • Formatting: Proper alignment, spacing, and number formatting

Reports serve various purposes: financial statements, inventory listings, transaction summaries, customer statements, management reports, and analytical summaries. Effective report generation makes data accessible and actionable.

Basic Report Structure

A typical COBOL report follows this structure:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
IDENTIFICATION DIVISION. PROGRAM-ID. SALES-REPORT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO 'SALES.DAT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. SELECT REPORT-FILE ASSIGN TO 'SALES.RPT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD INPUT-FILE LABEL RECORDS ARE STANDARD. 01 INPUT-RECORD. 05 SALES-DATE PIC 9(8). 05 SALES-REGION PIC X(10). 05 SALES-AMOUNT PIC 9(7)V99. 05 SALES-REP PIC X(20). FD REPORT-FILE LABEL RECORDS ARE STANDARD. 01 REPORT-LINE PIC X(132). WORKING-STORAGE SECTION. 01 WS-CONTROL-FIELDS. 05 WS-PAGE-NUMBER PIC 9(4) VALUE 0. 05 WS-LINE-COUNT PIC 9(2) VALUE 0. 05 WS-LINES-PER-PAGE PIC 9(2) VALUE 55. 05 WS-PREV-REGION PIC X(10) VALUE SPACES. 01 WS-TOTALS. 05 WS-REGION-TOTAL PIC 9(9)V99 VALUE 0. 05 WS-GRAND-TOTAL PIC 9(9)V99 VALUE 0. 01 WS-HEADER-LINE. 05 FILLER PIC X(20) VALUE SPACES. 05 FILLER PIC X(30) VALUE 'SALES REPORT BY REGION'. 05 FILLER PIC X(50) VALUE SPACES. 05 FILLER PIC X(6) VALUE 'PAGE: '. 05 WS-PAGE-NUM-OUT PIC ZZZ9. 01 WS-COLUMN-HEADER. 05 FILLER PIC X(5) VALUE SPACES. 05 FILLER PIC X(10) VALUE 'DATE'. 05 FILLER PIC X(15) VALUE SPACES. 05 FILLER PIC X(10) VALUE 'REGION'. 05 FILLER PIC X(15) VALUE SPACES. 05 FILLER PIC X(12) VALUE 'AMOUNT'. 05 FILLER PIC X(15) VALUE SPACES. 05 FILLER PIC X(20) VALUE 'SALES REP'. 01 WS-DETAIL-LINE. 05 FILLER PIC X(5) VALUE SPACES. 05 WS-DATE-OUT PIC 99/99/9999. 05 FILLER PIC X(10) VALUE SPACES. 05 WS-REGION-OUT PIC X(10). 05 FILLER PIC X(10) VALUE SPACES. 05 WS-AMOUNT-OUT PIC $,$$$,$$$.99. 05 FILLER PIC X(10) VALUE SPACES. 05 WS-REP-OUT PIC X(20). 01 WS-SUBTOTAL-LINE. 05 FILLER PIC X(5) VALUE SPACES. 05 FILLER PIC X(25) VALUE 'REGION SUBTOTAL: '. 05 WS-REGION-OUT-SUB PIC X(10). 05 FILLER PIC X(20) VALUE SPACES. 05 WS-SUBTOTAL-OUT PIC $,$$$,$$$,$$$.99. 01 WS-GRAND-TOTAL-LINE. 05 FILLER PIC X(5) VALUE SPACES. 05 FILLER PIC X(25) VALUE 'GRAND TOTAL: '. 05 FILLER PIC X(45) VALUE SPACES. 05 WS-GRAND-TOTAL-OUT PIC $,$$$,$$$,$$$.99.

This structure defines the input file, output report file, control fields for page management, totals for accumulation, and formatted output lines for headers, detail, subtotals, and grand totals.

Report Headers

Headers appear at the top of each page and provide context about the report. A good header includes:

  • Report title: Clear identification of the report purpose
  • Date: Report generation date or data date
  • Page number: Current page for multi-page reports
  • Column headings: Labels for each data column

Header Printing Logic

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT INPUT-FILE OUTPUT REPORT-FILE PERFORM PRINT-REPORT-HEADER PERFORM PROCESS-INPUT-FILE PERFORM PRINT-GRAND-TOTAL CLOSE INPUT-FILE REPORT-FILE STOP RUN. PRINT-REPORT-HEADER. ADD 1 TO WS-PAGE-NUMBER MOVE WS-PAGE-NUMBER TO WS-PAGE-NUM-OUT MOVE WS-HEADER-LINE TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING PAGE MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE MOVE WS-COLUMN-HEADER TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE MOVE ALL '-' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE MOVE 4 TO WS-LINE-COUNT.

This header paragraph:

  • Increments page number: Tracks which page we're on
  • Prints title and page number: Provides report identification
  • Adds blank line: Creates visual separation
  • Prints column headings: Labels each column
  • Prints separator line: Visual divider using dashes
  • Sets line count: Tracks lines printed on current page

Detail Lines

Detail lines display individual data records. Each input record typically produces one detail line, formatted for readability.

Detail Line Printing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
PROCESS-INPUT-FILE. PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END SET END-OF-FILE TO TRUE NOT AT END PERFORM CHECK-CONTROL-BREAK PERFORM PRINT-DETAIL-LINE PERFORM ACCUMULATE-TOTALS END-READ END-PERFORM. PRINT-DETAIL-LINE. PERFORM CHECK-PAGE-BREAK MOVE SALES-DATE TO WS-DATE-OUT MOVE SALES-REGION TO WS-REGION-OUT MOVE SALES-AMOUNT TO WS-AMOUNT-OUT MOVE SALES-REP TO WS-REP-OUT MOVE WS-DETAIL-LINE TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT.

The detail line printing process:

  • Checks page break: Ensures we don't exceed page limits
  • Moves data to output fields: Transfers input data to formatted output fields
  • Formats numbers: Uses editing characters for proper display
  • Writes the line: Outputs the formatted detail line
  • Updates line count: Tracks lines printed for page break logic

Control Breaks

Control breaks occur when a control field (grouping field) changes value. They signal the start of a new group and trigger printing of subtotals for the previous group.

Control Break Logic

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
CHECK-CONTROL-BREAK. IF WS-PREV-REGION NOT = SPACES IF SALES-REGION NOT = WS-PREV-REGION PERFORM PRINT-REGION-SUBTOTAL PERFORM INITIALIZE-REGION-TOTAL END-IF END-IF MOVE SALES-REGION TO WS-PREV-REGION. PRINT-REGION-SUBTOTAL. PERFORM CHECK-PAGE-BREAK MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT MOVE WS-PREV-REGION TO WS-REGION-OUT-SUB MOVE WS-REGION-TOTAL TO WS-SUBTOTAL-OUT MOVE WS-SUBTOTAL-LINE TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT MOVE ALL '-' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT. INITIALIZE-REGION-TOTAL. MOVE 0 TO WS-REGION-TOTAL.

Control break processing:

  • Compares current to previous: Detects when control field changes
  • Prints subtotal: Shows totals for the completed group
  • Resets accumulator: Prepares for the new group
  • Updates previous value: Tracks the current group for next comparison

Multiple Level Control Breaks

Reports can have multiple control break levels (major, intermediate, minor):

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
WORKING-STORAGE SECTION. 01 WS-CONTROL-FIELDS. 05 WS-PREV-DEPARTMENT PIC X(10) VALUE SPACES. 05 WS-PREV-DIVISION PIC X(10) VALUE SPACES. 05 WS-PREV-REGION PIC X(10) VALUE SPACES. 01 WS-TOTALS. 05 WS-DEPARTMENT-TOTAL PIC 9(9)V99 VALUE 0. 05 WS-DIVISION-TOTAL PIC 9(9)V99 VALUE 0. 05 WS-REGION-TOTAL PIC 9(9)V99 VALUE 0. 05 WS-GRAND-TOTAL PIC 9(9)V99 VALUE 0. PROCEDURE DIVISION. CHECK-CONTROL-BREAKS. *> Check major break (Region) IF WS-PREV-REGION NOT = SPACES IF SALES-REGION NOT = WS-PREV-REGION PERFORM PRINT-DIVISION-SUBTOTAL PERFORM PRINT-REGION-SUBTOTAL PERFORM INITIALIZE-REGION-TOTALS ELSE *> Check intermediate break (Division) IF SALES-DIVISION NOT = WS-PREV-DIVISION PERFORM PRINT-DIVISION-SUBTOTAL PERFORM INITIALIZE-DIVISION-TOTALS ELSE *> Check minor break (Department) IF SALES-DEPARTMENT NOT = WS-PREV-DEPARTMENT PERFORM PRINT-DEPARTMENT-SUBTOTAL PERFORM INITIALIZE-DEPARTMENT-TOTALS END-IF END-IF END-IF END-IF MOVE SALES-REGION TO WS-PREV-REGION MOVE SALES-DIVISION TO WS-PREV-DIVISION MOVE SALES-DEPARTMENT TO WS-PREV-DEPARTMENT.

Multiple level breaks allow hierarchical grouping: regions contain divisions, divisions contain departments. Subtotals are printed at each level when that level's control field changes.

Page Break Handling

Page breaks ensure reports don't exceed page limits and maintain professional formatting. You track the line count and trigger a new page when the limit is reached.

Page Break Logic

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CHECK-PAGE-BREAK. IF WS-LINE-COUNT >= WS-LINES-PER-PAGE PERFORM PRINT-PAGE-FOOTER PERFORM PRINT-REPORT-HEADER END-IF. PRINT-PAGE-FOOTER. *> Print footer information if needed MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE MOVE ALL '-' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE.

Page break handling:

  • Line count check: Compare current line count to maximum lines per page
  • Print footer: Optional footer information before page break
  • Advance page: Use AFTER ADVANCING PAGE to start new page
  • Print header: Print header on new page
  • Reset line count: Start counting lines on new page

Totals and Subtotals

Totals provide summary information at various levels. Subtotals appear at control breaks, grand totals at the end of the report.

Accumulating Totals

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ACCUMULATE-TOTALS. ADD SALES-AMOUNT TO WS-REGION-TOTAL ADD SALES-AMOUNT TO WS-GRAND-TOTAL. PRINT-REGION-SUBTOTAL. PERFORM CHECK-PAGE-BREAK MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT MOVE WS-PREV-REGION TO WS-REGION-OUT-SUB MOVE WS-REGION-TOTAL TO WS-SUBTOTAL-OUT MOVE WS-SUBTOTAL-LINE TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT. PRINT-GRAND-TOTAL. PERFORM CHECK-PAGE-BREAK MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT MOVE ALL '=' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT MOVE WS-GRAND-TOTAL TO WS-GRAND-TOTAL-OUT MOVE WS-GRAND-TOTAL-LINE TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 LINE ADD 1 TO WS-LINE-COUNT.

Total accumulation and printing:

  • Accumulate during processing: Add each record's value to totals
  • Print subtotals at breaks: Display group totals when control breaks occur
  • Print grand total at end: Display final totals after all records processed
  • Format totals: Use editing characters for proper display

Number Formatting

Proper number formatting makes reports readable and professional. COBOL provides editing characters for formatting.

Editing Characters

COBOL Editing Characters for Number Formatting
CharacterPurposeExample
$Dollar sign (floating or fixed position)PIC $,$$$,$$$.99 → $1,234.56
,Comma separator for thousandsPIC 9,999,999 → 1,234,567
.Decimal pointPIC 999.99 → 123.45
ZZero suppression (leading zeros become spaces)PIC ZZZ9 → " 123" (not "0123")
-Negative sign (floating)PIC ---,---.99 → -1,234.56
+Plus or minus signPIC +999.99 → +123.45
CRCredit indicator (appears if negative)PIC 999.99CR → 123.45CR if negative
DBDebit indicator (appears if negative)PIC 999.99DB → 123.45DB if negative

Formatting Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
WORKING-STORAGE SECTION. 01 WS-RAW-AMOUNT PIC 9(7)V99 VALUE 1234567.89. 01 WS-FORMATTED-AMOUNTS. 05 WS-CURRENCY-FMT PIC $,$$$,$$$.99. 05 WS-NO-DECIMAL PIC Z,ZZZ,ZZ9. 05 WS-PERCENT-FMT PIC ZZ9.99. 05 WS-SIGNED-FMT PIC ---,---.99. PROCEDURE DIVISION. FORMAT-EXAMPLES. *> Currency format MOVE WS-RAW-AMOUNT TO WS-CURRENCY-FMT *> Result: $1,234,567.89 *> No decimal, zero suppression MOVE WS-RAW-AMOUNT TO WS-NO-DECIMAL *> Result: 1,234,568 (rounded, no leading zeros) *> Percentage format MOVE 123.45 TO WS-PERCENT-FMT *> Result: "123.45" (leading zeros suppressed) *> Signed format MOVE -1234.56 TO WS-SIGNED-FMT *> Result: "-1,234.56"

Editing characters transform raw numbers into readable, professional formats suitable for business reports.

Complete Report Example

Here's a complete report generation program combining all concepts:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
IDENTIFICATION DIVISION. PROGRAM-ID. COMPLETE-REPORT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SALES-FILE ASSIGN TO 'SALES.DAT'. SELECT REPORT-FILE ASSIGN TO 'REPORT.OUT'. DATA DIVISION. FILE SECTION. FD SALES-FILE. 01 SALES-RECORD. 05 SALE-DATE PIC 9(8). 05 SALE-REGION PIC X(10). 05 SALE-AMOUNT PIC 9(7)V99. 05 SALE-REP PIC X(20). FD REPORT-FILE. 01 REPORT-LINE PIC X(132). WORKING-STORAGE SECTION. 01 WS-CONTROL. 05 WS-PAGE-NUM PIC 9(4) VALUE 0. 05 WS-LINE-CTR PIC 9(2) VALUE 0. 05 WS-MAX-LINES PIC 9(2) VALUE 55. 05 WS-PREV-REGION PIC X(10) VALUE SPACES. 05 WS-EOF-FLAG PIC X VALUE 'N'. 88 END-OF-FILE VALUE 'Y'. 01 WS-TOTALS. 05 WS-REGION-TOT PIC 9(9)V99 VALUE 0. 05 WS-GRAND-TOT PIC 9(9)V99 VALUE 0. 01 WS-HEADER. 05 FILLER PIC X(40) VALUE SPACES. 05 FILLER PIC X(20) VALUE 'SALES REPORT'. 05 FILLER PIC X(40) VALUE SPACES. 05 FILLER PIC X(6) VALUE 'PAGE: '. 05 WS-PAGE-OUT PIC ZZZ9. 01 WS-COL-HEADER. 05 FILLER PIC X(5) VALUE SPACES. 05 FILLER PIC X(10) VALUE 'DATE'. 05 FILLER PIC X(10) VALUE SPACES. 05 FILLER PIC X(10) VALUE 'REGION'. 05 FILLER PIC X(10) VALUE SPACES. 05 FILLER PIC X(12) VALUE 'AMOUNT'. 05 FILLER PIC X(10) VALUE SPACES. 05 FILLER PIC X(20) VALUE 'SALES REP'. 01 WS-DETAIL. 05 FILLER PIC X(5) VALUE SPACES. 05 WS-DATE-OUT PIC 99/99/9999. 05 FILLER PIC X(10) VALUE SPACES. 05 WS-REG-OUT PIC X(10). 05 FILLER PIC X(10) VALUE SPACES. 05 WS-AMT-OUT PIC $,$$$,$$$.99. 05 FILLER PIC X(10) VALUE SPACES. 05 WS-REP-OUT PIC X(20). 01 WS-SUBTOTAL. 05 FILLER PIC X(5) VALUE SPACES. 05 FILLER PIC X(20) VALUE 'REGION TOTAL: '. 05 WS-REG-SUB PIC X(10). 05 FILLER PIC X(20) VALUE SPACES. 05 WS-SUB-AMT PIC $,$$$,$$$,$$$.99. 01 WS-GRAND-TOTAL-LINE. 05 FILLER PIC X(5) VALUE SPACES. 05 FILLER PIC X(20) VALUE 'GRAND TOTAL: '. 05 FILLER PIC X(45) VALUE SPACES. 05 WS-GRAND-AMT PIC $,$$$,$$$,$$$.99. PROCEDURE DIVISION. MAIN-PARA. OPEN INPUT SALES-FILE OUTPUT REPORT-FILE PERFORM PRINT-HEADER PERFORM PROCESS-RECORDS PERFORM PRINT-REGION-SUBTOTAL PERFORM PRINT-GRAND-TOTAL CLOSE SALES-FILE REPORT-FILE STOP RUN. PRINT-HEADER. ADD 1 TO WS-PAGE-NUM MOVE WS-PAGE-NUM TO WS-PAGE-OUT MOVE WS-HEADER TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING PAGE MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 MOVE WS-COL-HEADER TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 MOVE ALL '-' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 MOVE 4 TO WS-LINE-CTR. CHECK-PAGE-BREAK. IF WS-LINE-CTR >= WS-MAX-LINES PERFORM PRINT-HEADER END-IF. PROCESS-RECORDS. PERFORM UNTIL END-OF-FILE READ SALES-FILE AT END SET END-OF-FILE TO TRUE NOT AT END PERFORM CHECK-CONTROL-BREAK PERFORM PRINT-DETAIL PERFORM ACCUMULATE MOVE SALE-REGION TO WS-PREV-REGION END-READ END-PERFORM. CHECK-CONTROL-BREAK. IF WS-PREV-REGION NOT = SPACES IF SALE-REGION NOT = WS-PREV-REGION PERFORM PRINT-REGION-SUBTOTAL MOVE 0 TO WS-REGION-TOT END-IF END-IF. PRINT-DETAIL. PERFORM CHECK-PAGE-BREAK MOVE SALE-DATE TO WS-DATE-OUT MOVE SALE-REGION TO WS-REG-OUT MOVE SALE-AMOUNT TO WS-AMT-OUT MOVE SALE-REP TO WS-REP-OUT MOVE WS-DETAIL TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR. ACCUMULATE. ADD SALE-AMOUNT TO WS-REGION-TOT ADD SALE-AMOUNT TO WS-GRAND-TOT. PRINT-REGION-SUBTOTAL. PERFORM CHECK-PAGE-BREAK MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR MOVE WS-PREV-REGION TO WS-REG-SUB MOVE WS-REGION-TOT TO WS-SUB-AMT MOVE WS-SUBTOTAL TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR MOVE ALL '-' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR. PRINT-GRAND-TOTAL. PERFORM CHECK-PAGE-BREAK MOVE SPACES TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR MOVE ALL '=' TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR MOVE WS-GRAND-TOT TO WS-GRAND-AMT MOVE WS-GRAND-TOTAL-LINE TO REPORT-LINE WRITE REPORT-LINE AFTER ADVANCING 1 ADD 1 TO WS-LINE-CTR.

Best Practices for Report Generation

Follow these best practices for professional, maintainable reports:

1. Design First

Design your report layout on paper or in a document before coding. Define:

  • Column positions and widths
  • Header and footer content
  • Control break levels
  • Total and subtotal locations
  • Page layout and margins

2. Use Consistent Formatting

Maintain consistent formatting throughout the report:

  • Align columns consistently
  • Use the same number formatting for similar fields
  • Maintain consistent spacing
  • Use standard separators (dashes, equals signs)

3. Handle Edge Cases

Consider and handle edge cases:

  • Empty input files (no data to report)
  • Very long data values (truncate or wrap)
  • Negative numbers (proper sign display)
  • Zero values (display appropriately)
  • Page breaks in the middle of groups

4. Validate Before Printing

cobol
1
2
3
4
5
6
7
8
9
10
11
VALIDATE-AND-PRINT. IF SALE-AMOUNT < 0 DISPLAY 'WARNING: Negative amount for ' SALE-REGION MOVE 0 TO SALE-AMOUNT END-IF IF SALE-REGION = SPACES MOVE 'UNKNOWN' TO SALE-REGION END-IF PERFORM PRINT-DETAIL.

5. Document Report Layout

Document your report structure:

  • Report purpose and usage
  • Input file structure
  • Output format and layout
  • Control break logic
  • Calculation formulas
  • Special formatting rules

Explain Like I'm 5: Report Generation

Think of report generation like creating a school report card:

  • Header is like the report card header with school name, student name, and date
  • Detail lines are like individual grades for each subject
  • Control breaks are like grouping subjects by category (math, science, English) and showing category averages
  • Subtotals are like the average for each subject category
  • Grand total is like the overall GPA at the end
  • Page breaks are like starting a new page when you run out of room
  • Formatting is like making sure everything is neat, aligned, and easy to read

Just like a report card organizes grades into a readable format, COBOL report generation organizes data into a professional, readable document!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Basic Report

Create a simple report that reads customer records (ID, name, balance) and prints a formatted report with headers, detail lines, and a grand total of all balances.

Exercise 2: Control Breaks

Create a sales report grouped by region. Include detail lines for each sale, subtotals for each region, and a grand total. Handle control breaks properly.

Exercise 3: Page Breaks

Enhance your report to handle page breaks. Print headers on each page, track page numbers, and ensure subtotals and grand totals don't get split across pages.

Exercise 4: Number Formatting

Create a financial report with properly formatted currency amounts, percentages, and signed numbers. Use various editing characters to format different types of numeric data.

Exercise 5: Multi-Level Control Breaks

Create a report with multiple control break levels: region, then department, then product. Print subtotals at each level and a grand total at the end.

Test Your Knowledge

1. What is a control break in COBOL report generation?

  • A page break
  • When a control field value changes, signaling a new group
  • An error in the report
  • A line break in the output

2. When should you print report headers?

  • Only at the beginning of the report
  • At the beginning and at each page break
  • After every detail line
  • Only in the footer

3. How do you format a number as currency in COBOL?

  • Use the CURRENCY keyword
  • Use editing characters like $ and , in the PICTURE clause
  • Use a special FORMAT statement
  • COBOL cannot format currency

4. What is the difference between detail lines and summary lines?

  • There is no difference
  • Detail lines show individual records, summary lines show totals
  • Detail lines are longer
  • Summary lines are printed first

5. How do you handle page breaks in a COBOL report?

  • COBOL handles it automatically
  • Track line count and print footer/header when maximum lines reached
  • Use a special PAGE BREAK statement
  • Page breaks are not possible in COBOL

6. When should you print grand totals in a report?

  • At the beginning
  • After each detail line
  • At the end, after all records are processed
  • In the header

Related Concepts

Related Pages