COBOL Tutorial

Progress0 of 0 lessons

COBOL line positioning

Line positioning in COBOL controls where report output appears: which line and, in some contexts, which column. You use the LINAGE clause to define the logical page (lines per page, footing area), and the WRITE statement with ADVANCING to move the print position (e.g. one line, or to the next page). Together they give you predictable report layout and page breaks.

LINAGE: defining the page

For report files (e.g. printer or spooled output), LINAGE tells the run time how big a “page” is. You put it on the FD (file description). Typical form:

cobol
1
2
3
4
5
6
FD REPORT-FILE LINAGE IS 50 LINES WITH FOOTING AT 45 LINES AT TOP 5 LINES AT BOTTOM 3. 01 REPORT-LINE PIC X(132).

LINAGE IS 50 LINES – the logical page has 50 lines. WITH FOOTING AT 45 – the footing area starts at line 45 (so the body is lines 1–44). LINES AT TOP and LINES AT BOTTOM reserve margin lines. The run time tracks the current line and can trigger AT END-OF-PAGE when the next write would hit the footing. See AT END-OF-PAGE for using that in WRITE.

WRITE with ADVANCING

WRITE ... ADVANCING controls how the file position moves after (or before) writing a record. This gives you line-by-line spacing and page breaks.

Common ADVANCING options
OptionMeaning
AFTER ADVANCING 1 LINEWrite the record, then advance one line
AFTER ADVANCING n LINESWrite, then advance n lines
AFTER ADVANCING PAGEWrite, then advance to the next page
BEFORE ADVANCING 1 LINEAdvance one line, then write
cobol
1
2
3
4
5
6
7
8
*> Write a line and then advance one line (next write on next line) WRITE REPORT-LINE AFTER ADVANCING 1 LINE *> Write a line and then advance to the next page WRITE REPORT-LINE AFTER ADVANCING PAGE *> Advance two lines, then write (blank line between) WRITE REPORT-LINE AFTER ADVANCING 2 LINES

Column positioning

For sequential report files, there is no COLUMN clause on WRITE. You build each line in working storage: move data into a long field (e.g. PIC X(132)) at the right positions, then WRITE that field. For example, move a title to positions 1–40, a page number to 70–72, and spaces everywhere else. Report Writer and screen I/O (e.g. SCREEN SECTION) may support LINE and COLUMN for direct positioning; check your compiler manual.

Step-by-step: simple report with page breaks

  • Define the report file with LINAGE (and optionally WITH FOOTING, LINES AT TOP/BOTTOM).
  • Define a record (e.g. 01 REPORT-LINE PIC X(132)).
  • For each line, build the content in REPORT-LINE, then WRITE REPORT-LINE AFTER ADVANCING 1 LINE. Use WRITE ... AFTER ADVANCING PAGE when you want a new page.
  • To run code at page full, use WRITE ... AT END-OF-PAGE PERFORM paragraph-name and in that paragraph advance to the next page and optionally reprint headers.

Explain like I'm five

Imagine a notepad with a fixed number of lines per page. LINAGE says “this many lines per page.” Each time you write a line, you can say “then move to the next line” or “then turn to a new page.” Line positioning is those rules so your report doesn’t run off the page and new pages start where you expect.

Test Your Knowledge

1. What does AFTER ADVANCING PAGE do?

  • Advances one line
  • Starts a new page and then writes the record
  • Writes the record then advances to the next page
  • Prints a page number

2. What is the LINAGE clause used for?

  • Defining record length
  • Defining the logical page size and footing for report files
  • Defining column width
  • Defining file organization

Related concepts

Related Pages