MainframeMaster

COBOL Tutorial

COBOL BEFORE Clause

The BEFORE clause in COBOL is a powerful formatting feature primarily used in report generation and output control to specify positioning and spacing requirements before displaying or printing data. This clause provides precise control over page formatting, line spacing, and positional relationships in printed reports and screen displays, enabling developers to create professional, well-formatted output that meets business documentation standards. Understanding the BEFORE clause is essential for creating sophisticated reporting systems that produce clear, readable, and professionally formatted documents that comply with organizational standards and regulatory requirements.

The BEFORE clause operates in conjunction with COBOL's report writer facilities and DISPLAY statements to provide comprehensive formatting control. This functionality is particularly important in business applications where report formatting standards are critical for compliance, readability, and professional presentation of business data.

Understanding BEFORE Clause Functionality

The BEFORE clause controls spacing and positioning that occurs before the output of data lines, report groups, or display elements. It can specify line advancement, page breaks, and positioning requirements that ensure proper formatting and layout. The clause is commonly used with the WRITE statement for file output, DISPLAY statements for screen output, and within report description entries for report generation.

Key BEFORE Clause Applications:

  • Page Control: Manage page breaks and ensure proper page formatting in reports
  • Line Spacing: Control vertical spacing between lines and report sections
  • Report Formatting: Ensure consistent spacing and layout in business reports
  • Display Positioning: Control screen positioning for terminal applications
  • Document Structure: Create well-organized documents with proper section breaks
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
*> Basic BEFORE clause usage in report writing ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REPORT-FILE ASSIGN TO "REPORT.TXT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD REPORT-FILE. 01 REPORT-RECORD PIC X(132). WORKING-STORAGE SECTION. 01 WS-PAGE-COUNTER PIC 9(3) VALUE 1. 01 WS-LINE-COUNTER PIC 9(2) VALUE 0. 01 WS-CUSTOMER-DATA. 05 WS-CUSTOMER-ID PIC 9(8). 05 WS-CUSTOMER-NAME PIC X(30). 05 WS-BALANCE PIC S9(7)V99. PROCEDURE DIVISION. WRITE-CUSTOMER-REPORT. OPEN OUTPUT REPORT-FILE. *> Write report header with page break before WRITE REPORT-RECORD FROM WS-HEADER-LINE BEFORE ADVANCING PAGE. *> Write customer data with controlled line spacing WRITE REPORT-RECORD FROM WS-CUSTOMER-LINE BEFORE ADVANCING 2 LINES. *> Write total line with extra spacing before WRITE REPORT-RECORD FROM WS-TOTAL-LINE BEFORE ADVANCING 3 LINES. CLOSE REPORT-FILE.

Advanced BEFORE Techniques

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
*> Comprehensive report formatting with BEFORE clause DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-CONTROLS. 05 LINES-PER-PAGE PIC 9(2) VALUE 60. 05 CURRENT-LINE PIC 9(2) VALUE 0. 05 PAGE-NUMBER PIC 9(3) VALUE 1. 01 REPORT-HEADERS. 05 MAIN-HEADER PIC X(80) VALUE " CUSTOMER BALANCE REPORT". 05 COLUMN-HEADERS PIC X(80) VALUE "CUSTOMER ID CUSTOMER NAME BALANCE". 05 PAGE-HEADER PIC X(80). PROCEDURE DIVISION. PRINT-REPORT-SECTION. *> Check if new page needed before printing IF CURRENT-LINE + 5 > LINES-PER-PAGE PERFORM PRINT-PAGE-BREAK END-IF. *> Print section header with controlled spacing WRITE REPORT-RECORD FROM SECTION-HEADER BEFORE ADVANCING 2 LINES. ADD 2 TO CURRENT-LINE. *> Print detail lines with standard spacing PERFORM PRINT-CUSTOMER-DETAILS. PRINT-PAGE-BREAK. *> Force new page with header WRITE REPORT-RECORD FROM SPACES BEFORE ADVANCING PAGE. *> Print page header STRING "PAGE " PAGE-NUMBER INTO PAGE-HEADER. WRITE REPORT-RECORD FROM PAGE-HEADER BEFORE ADVANCING 1 LINE. WRITE REPORT-RECORD FROM MAIN-HEADER BEFORE ADVANCING 2 LINES. WRITE REPORT-RECORD FROM COLUMN-HEADERS BEFORE ADVANCING 2 LINES. MOVE 6 TO CURRENT-LINE. ADD 1 TO PAGE-NUMBER.

Best Practices for BEFORE Clause

Formatting Guidelines

  • Consistent Spacing: Establish standard spacing patterns for different report elements
  • Page Management: Implement proper page break logic to avoid orphaned lines
  • Readability: Use appropriate spacing to enhance document readability
  • Standards Compliance: Follow organizational formatting standards and requirements
  • Testing: Verify formatting across different output devices and configurations

Frequently Asked Questions

Q: What's the difference between BEFORE and AFTER advancing?

BEFORE ADVANCING positions the paper or cursor before writing the data, while AFTER ADVANCING moves the position after writing. BEFORE is typically used when you want to control spacing before output, while AFTER controls spacing after output.

Q: Can I use BEFORE ADVANCING with screen displays?

While BEFORE ADVANCING is primarily designed for printed output, some COBOL implementations support it with DISPLAY statements for screen positioning. However, screen handling is often better accomplished using specific screen control features or positioning clauses.