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.
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.
12345678910111213141516171819202122232425262728293031323334353637*> 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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748*> 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.
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.
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.