MainframeMaster

COBOL Tutorial

COBOL PAGE-COUNTER - Report Page Numbering

Progress0 of 0 lessons

What is PAGE-COUNTER?

PAGE-COUNTER is a special system variable in COBOL that automatically keeps track of the current page number in a report. Think of it as an automatic page number that COBOL manages for you - every time your report starts a new page, PAGE-COUNTER automatically increases by 1, so you always know which page you're currently working on.

📄 Real-World Analogy

Imagine you're writing a book:

  • PAGE-COUNTER: Like the automatic page numbering in a word processor
  • New Page: Every time you start a new page, the number increases
  • Header/Footer: You can put "Page 1", "Page 2", etc. automatically
  • No Manual Work: You don't have to count pages yourself

PAGE-COUNTER does the same thing for your COBOL reports - it automatically tracks page numbers.

Key Features

  • Automatic Incrementing - Increases by 1 each time a new page starts
  • System Variable - Provided automatically by COBOL, no declaration needed
  • Report Integration - Works seamlessly with COBOL report generation
  • Easy Display - Can be used directly in headers, footers, and output
  • Manual Control - Can be set or reset as needed
  • Formatting Support - Can be formatted for display purposes

How to Use PAGE-COUNTER

Using PAGE-COUNTER is straightforward - it's automatically available when you need it, and you can use it in various ways to display page numbers in your reports.

Basic PAGE-COUNTER Usage

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
* Basic PAGE-COUNTER example IDENTIFICATION DIVISION. PROGRAM-ID. PAGE-COUNTER-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-HEADER PIC X(80). 01 REPORT-FOOTER PIC X(80). 01 FORMATTED-PAGE PIC ZZZ9. PROCEDURE DIVISION. * Initialize page counter to 1 MOVE 1 TO PAGE-COUNTER * Display page header with page number MOVE PAGE-COUNTER TO FORMATTED-PAGE STRING "SALES REPORT" DELIMITED BY SIZE " Page " DELIMITED BY SIZE FORMATTED-PAGE DELIMITED BY SIZE INTO REPORT-HEADER DISPLAY REPORT-HEADER * Generate some report content DISPLAY "Customer data goes here..." DISPLAY "More report content..." * Start a new page DISPLAY " " UPON CONSOLE ADVANCING PAGE * PAGE-COUNTER automatically incremented to 2 MOVE PAGE-COUNTER TO FORMATTED-PAGE STRING "SALES REPORT" DELIMITED BY SIZE " Page " DELIMITED BY SIZE FORMATTED-PAGE DELIMITED BY SIZE INTO REPORT-HEADER DISPLAY REPORT-HEADER STOP RUN.

This example shows how PAGE-COUNTER automatically tracks page numbers in a report.

PAGE-COUNTER Operations

OperationSyntaxExample
Display page numberDISPLAY PAGE-COUNTERDISPLAY "Page: " PAGE-COUNTER
Set page numberMOVE value TO PAGE-COUNTERMOVE 1 TO PAGE-COUNTER
Format for displayMOVE PAGE-COUNTER TO formatted-fieldMOVE PAGE-COUNTER TO FORMATTED-PAGE
Use in stringSTRING ... PAGE-COUNTER ...STRING "Page " PAGE-COUNTER INTO OUTPUT
Compare page numberIF PAGE-COUNTER = valueIF PAGE-COUNTER = 10

Practical Examples

Let's look at some real-world examples of how PAGE-COUNTER is used in COBOL applications.

Complete Report with Page Numbers

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
* Complete report with PAGE-COUNTER IDENTIFICATION DIVISION. PROGRAM-ID. COMPLETE-REPORT. DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-HEADER. 05 REPORT-TITLE PIC X(30) VALUE "CUSTOMER SALES REPORT". 05 FILLER PIC X(20) VALUE SPACES. 05 PAGE-LABEL PIC X(6) VALUE "Page: ". 05 PAGE-NUMBER PIC ZZZ9. 05 FILLER PIC X(20) VALUE SPACES. 05 REPORT-DATE PIC X(10). 01 REPORT-FOOTER. 05 FILLER PIC X(30) VALUE "End of Report". 05 FILLER PIC X(20) VALUE SPACES. 05 TOTAL-PAGES PIC X(10) VALUE "Total Pages: ". 05 FINAL-PAGE PIC ZZZ9. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(6). 05 CUSTOMER-NAME PIC X(30). 05 SALES-AMOUNT PIC 9(8)V99. 01 LINE-COUNT PIC 9(2) VALUE 0. 01 MAX-LINES-PER-PAGE PIC 9(2) VALUE 20. PROCEDURE DIVISION. * Initialize report MOVE 1 TO PAGE-COUNTER MOVE FUNCTION CURRENT-DATE(1:10) TO REPORT-DATE * Display first page header PERFORM DISPLAY-PAGE-HEADER * Process customer records PERFORM UNTIL NO-MORE-RECORDS * Check if we need a new page IF LINE-COUNT >= MAX-LINES-PER-PAGE PERFORM DISPLAY-PAGE-FOOTER DISPLAY " " UPON CONSOLE ADVANCING PAGE PERFORM DISPLAY-PAGE-HEADER END-IF * Display customer record DISPLAY CUSTOMER-ID " " CUSTOMER-NAME " " SALES-AMOUNT ADD 1 TO LINE-COUNT * Read next record (simulated) PERFORM READ-NEXT-RECORD END-PERFORM * Display final page footer MOVE PAGE-COUNTER TO FINAL-PAGE PERFORM DISPLAY-PAGE-FOOTER STOP RUN. DISPLAY-PAGE-HEADER. MOVE PAGE-COUNTER TO PAGE-NUMBER DISPLAY REPORT-HEADER DISPLAY "Customer ID Customer Name Sales Amount" DISPLAY "---------- ---------------------- ------------" MOVE 0 TO LINE-COUNT. DISPLAY-PAGE-FOOTER. DISPLAY " " DISPLAY "----------------------------------------" DISPLAY "Page " PAGE-COUNTER " completed" DISPLAY "----------------------------------------". READ-NEXT-RECORD. * Simulate reading next record * In real program, this would read from a file EXIT.

This example shows a complete report with automatic page numbering and page breaks.

Multi-Section Report

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
* Multi-section report with different page numbering IDENTIFICATION DIVISION. PROGRAM-ID. MULTI-SECTION-REPORT. DATA DIVISION. WORKING-STORAGE SECTION. 01 SECTION-HEADER. 05 SECTION-TITLE PIC X(40). 05 FILLER PIC X(10) VALUE SPACES. 05 PAGE-LABEL PIC X(6) VALUE "Page: ". 05 PAGE-NUMBER PIC ZZZ9. 01 CURRENT-SECTION PIC X(20). 01 SECTION-PAGE-START PIC 9(3). PROCEDURE DIVISION. * Generate Executive Summary Section MOVE "EXECUTIVE SUMMARY" TO CURRENT-SECTION MOVE 1 TO PAGE-COUNTER MOVE PAGE-COUNTER TO SECTION-PAGE-START PERFORM GENERATE-SECTION * Generate Detailed Analysis Section MOVE "DETAILED ANALYSIS" TO CURRENT-SECTION * Don't reset PAGE-COUNTER - continue numbering PERFORM GENERATE-SECTION * Generate Recommendations Section MOVE "RECOMMENDATIONS" TO CURRENT-SECTION PERFORM GENERATE-SECTION * Display final summary DISPLAY " " DISPLAY "Report Summary:" DISPLAY "Total Pages: " PAGE-COUNTER DISPLAY "Executive Summary: Pages " SECTION-PAGE-START " - " FUNCTION MIN(PAGE-COUNTER, 5) STOP RUN. GENERATE-SECTION. * Display section header MOVE CURRENT-SECTION TO SECTION-TITLE MOVE PAGE-COUNTER TO PAGE-NUMBER DISPLAY SECTION-HEADER DISPLAY " " * Generate section content (simulated) DISPLAY "Section content goes here..." DISPLAY "More content..." * Simulate multiple pages for this section PERFORM VARYING PAGE-COUNTER FROM PAGE-COUNTER BY 1 UNTIL PAGE-COUNTER > PAGE-COUNTER + 2 DISPLAY " " UPON CONSOLE ADVANCING PAGE MOVE PAGE-COUNTER TO PAGE-NUMBER DISPLAY SECTION-HEADER DISPLAY "Continued content..." END-PERFORM.

This example shows how to use PAGE-COUNTER across multiple report sections.

Advanced PAGE-COUNTER Features

PAGE-COUNTER supports advanced features for complex reporting scenarios.

Conditional Page Numbering

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
* Conditional page numbering based on content IDENTIFICATION DIVISION. PROGRAM-ID. CONDITIONAL-PAGING. DATA DIVISION. WORKING-STORAGE SECTION. 01 PAGE-HEADER. 05 REPORT-TITLE PIC X(30) VALUE "CONDITIONAL REPORT". 05 FILLER PIC X(20) VALUE SPACES. 05 PAGE-NUMBER PIC ZZZ9. 01 CONTENT-TYPE PIC X(10). 01 LINE-COUNT PIC 9(2) VALUE 0. 01 MAX-LINES PIC 9(2) VALUE 15. PROCEDURE DIVISION. MOVE 1 TO PAGE-COUNTER * Process different content types PERFORM PROCESS-CONTENT UNTIL NO-MORE-CONTENT STOP RUN. PROCESS-CONTENT. * Read content type (simulated) MOVE "SUMMARY" TO CONTENT-TYPE * Different page numbering based on content type EVALUATE CONTENT-TYPE WHEN "SUMMARY" * Summary pages use "S-" prefix PERFORM DISPLAY-SUMMARY-PAGE WHEN "DETAIL" * Detail pages use "D-" prefix PERFORM DISPLAY-DETAIL-PAGE WHEN "APPENDIX" * Appendix pages use "A-" prefix PERFORM DISPLAY-APPENDIX-PAGE END-EVALUATE. DISPLAY-SUMMARY-PAGE. MOVE PAGE-COUNTER TO PAGE-NUMBER DISPLAY "S-" PAGE-NUMBER " - " REPORT-TITLE DISPLAY "Summary content..." DISPLAY " " UPON CONSOLE ADVANCING PAGE. DISPLAY-DETAIL-PAGE. MOVE PAGE-COUNTER TO PAGE-NUMBER DISPLAY "D-" PAGE-NUMBER " - " REPORT-TITLE DISPLAY "Detailed content..." DISPLAY " " UPON CONSOLE ADVANCING PAGE. DISPLAY-APPENDIX-PAGE. MOVE PAGE-COUNTER TO PAGE-NUMBER DISPLAY "A-" PAGE-NUMBER " - " REPORT-TITLE DISPLAY "Appendix content..." DISPLAY " " UPON CONSOLE ADVANCING PAGE.

This example shows how to use different page numbering schemes based on content type.

Page Number Formatting

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
* Advanced page number formatting IDENTIFICATION DIVISION. PROGRAM-ID. FORMATTED-PAGING. DATA DIVISION. WORKING-STORAGE SECTION. 01 FORMATTED-PAGE-INFO. 05 PAGE-PREFIX PIC X(5). 05 PAGE-NUMBER PIC ZZZ9. 05 PAGE-SUFFIX PIC X(5). 05 TOTAL-PAGES PIC X(10) VALUE " of ". 05 FINAL-PAGE PIC ZZZ9. 01 PAGE-STYLE PIC X(10). 01 TOTAL-PAGE-COUNT PIC 9(3) VALUE 25. PROCEDURE DIVISION. MOVE 1 TO PAGE-COUNTER * Different page number styles PERFORM VARYING PAGE-COUNTER FROM 1 BY 1 UNTIL PAGE-COUNTER > 5 * Style 1: Simple numbering MOVE "Page " TO PAGE-PREFIX MOVE PAGE-COUNTER TO PAGE-NUMBER MOVE "" TO PAGE-SUFFIX DISPLAY FORMATTED-PAGE-INFO * Style 2: With total pages MOVE "Page " TO PAGE-PREFIX MOVE PAGE-COUNTER TO PAGE-NUMBER MOVE TOTAL-PAGES TO PAGE-SUFFIX MOVE TOTAL-PAGE-COUNT TO FINAL-PAGE DISPLAY FORMATTED-PAGE-INFO * Style 3: Roman numerals (simulated) MOVE "Page " TO PAGE-PREFIX MOVE PAGE-COUNTER TO PAGE-NUMBER MOVE " (I)" TO PAGE-SUFFIX DISPLAY FORMATTED-PAGE-INFO DISPLAY " " UPON CONSOLE ADVANCING PAGE END-PERFORM STOP RUN.

This example demonstrates different ways to format page numbers for display.

Best Practices and Tips

Following these best practices will help you use PAGE-COUNTER effectively in your COBOL applications.

PAGE-COUNTER Best Practices

  • Initialize at the start - Set PAGE-COUNTER to 1 at the beginning of your report
  • Use consistent formatting - Format page numbers consistently throughout your report
  • Check page limits - Use PAGE-COUNTER to check if you're approaching page limits
  • Include in headers/footers - Always include page numbers in report headers or footers
  • Handle page breaks properly - Use ADVANCING PAGE to ensure proper page breaks
  • Consider total pages - Display both current page and total pages when possible
  • Test with different page counts - Test your reports with various page counts

Common Mistakes to Avoid

MistakeProblemSolution
Not initializing PAGE-COUNTERPage numbers may start from unexpected valuesAlways set PAGE-COUNTER to 1 at report start
Forgetting page breaksContent runs together without proper paginationUse ADVANCING PAGE when starting new pages
Inconsistent formattingReport looks unprofessionalUse consistent page number formatting
Not checking page limitsReports may exceed printer limitsMonitor PAGE-COUNTER for page limits
Missing page numbersUsers can't navigate multi-page reportsAlways include page numbers in headers/footers

PAGE-COUNTER Quick Reference

ActionSyntaxExample
Initialize page counterMOVE 1 TO PAGE-COUNTERMOVE 1 TO PAGE-COUNTER
Display page numberDISPLAY PAGE-COUNTERDISPLAY "Page: " PAGE-COUNTER
Format page numberMOVE PAGE-COUNTER TO formatted-fieldMOVE PAGE-COUNTER TO PAGE-NUMBER
Start new pageDISPLAY " " ADVANCING PAGEDISPLAY " " UPON CONSOLE ADVANCING PAGE
Check page numberIF PAGE-COUNTER = valueIF PAGE-COUNTER = 10
Reset page counterMOVE value TO PAGE-COUNTERMOVE 1 TO PAGE-COUNTER

Test Your Knowledge

1. What is PAGE-COUNTER in COBOL?

  • A data field that counts program executions
  • A system variable that automatically tracks page numbers in reports
  • A counter for database records
  • A variable that counts file records

2. How do you declare PAGE-COUNTER in a COBOL program?

  • 01 PAGE-COUNTER PIC 9(3)
  • You don't declare it - it's automatically available
  • 01 PAGE-COUNTER COMP
  • 01 PAGE-COUNTER USAGE IS INDEX

3. When does PAGE-COUNTER automatically increment?

  • Every time you write a record
  • Every time you start a new page
  • Every time you read a record
  • Every time you perform a calculation

4. How do you display the current page number using PAGE-COUNTER?

  • DISPLAY PAGE-COUNTER
  • DISPLAY "Page: " PAGE-COUNTER
  • MOVE PAGE-COUNTER TO DISPLAY-FIELD
  • All of the above

5. In which COBOL division is PAGE-COUNTER typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

Frequently Asked Questions