MainframeMaster

COBOL Print Formatting

Print formatting in COBOL is how you build each line that goes to a printer or report file and how you control where that line appears (single space, double space, new page). It involves: (1) defining a record layout with fixed column positions, (2) using edited PICTURE clauses so numbers print with commas, currency, and no leading zeros, (3) placing an ASA control character in the first byte to tell the printer how many lines to advance before printing, and (4) using WRITE with BEFORE or AFTER ADVANCING to control line and page advances. This page covers building the content of each line and the basics of ADVANCING; for full report structure (headings, LINAGE, page breaks) see Report Writing.

Explain Like I'm Five: What Is Print Formatting?

Imagine you are filling in a form: you write the title at the top, then one line per person, and you leave a blank line between sections. Print formatting is telling the computer exactly what to put in each line (names, numbers with commas and dollar signs) and whether to leave one blank line, two, or start a new page. The first character of each line is a secret instruction to the printer: "move down one line" or "move down two" or "go to the next page." The printer follows that and then prints the rest of the line.

Record Layout and Column Positions

A print line is usually a fixed-length record (e.g. 132 bytes for a wide printer). You define it in the FD or in working storage and build it before writing. To align columns you either define group items at specific positions or use reference modification. For example, if the name should start at column 1 and the amount at column 41, you can define a layout with FILLER and named fields at those positions, or use MOVE name TO report-line(1:30) and MOVE edited-amount TO report-line(41:12). Keeping positions in constants or a copybook helps when you change column layout later.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
01 REPORT-LINE. 05 RL-CTL PIC X(1). *> ASA control (space, 0, -, +, 1) 05 RL-NAME PIC X(30). *> cols 2-31 05 FILLER PIC X(9) VALUE SPACES. 05 RL-AMOUNT PIC ZZ,ZZZ,ZZ9.99. *> cols 41-52, edited 05 FILLER PIC X(80) VALUE SPACES. *> Build line: single space, name, then edited amount MOVE SPACE TO RL-CTL MOVE CUST-NAME TO RL-NAME MOVE WS-AMOUNT TO RL-AMOUNT WRITE REPORT-REC FROM REPORT-LINE AFTER ADVANCING 1 LINE

ASA Printer Control Character (First Byte)

On many mainframe printers, the first character of each record is not printed; it is the ASA (American Standards Association) carriage control character. The device uses it to decide how many lines to advance before printing the rest of the record. If you send the record to a dataset that is printed with a driver that interprets ASA, the first byte is consumed and the rest of the line is printed at the new position.

ASA control characters (first byte of record)
CharacterActionTypical use
Space (blank)Advance 1 line, then print.Normal single-spaced lines.
0Advance 2 lines, then print.Double space between lines.
- (minus)Advance 3 lines, then print.Triple space; leave two blank lines.
+ (plus)No advance; print over current line.Overstrike (e.g. bold by printing twice).
1Advance to top of next page, then print.New page; first line on next page.

Space (blank) is the usual choice for normal single-spaced lines. Use 0 for double space and - for triple space when you want gaps between sections. Plus (+) means no advance: the line is printed on top of the current line (overstrike). One (1) ejects to the next page and then prints, so the record becomes the first line on the new page. Some environments use different conventions or omit ASA; check your runtime and printer setup. When browsing the dataset in an editor, the first column will show these characters; they may not appear on the physical print if the device strips them.

Edited PICTUREs for Printed Numbers

Internal numeric fields use PICTUREs like 9(7)V99 or S9(5). For printed output you want readable format: no leading zeros (use Z), optional dollar sign ($), commas (,) and a decimal point (.). You define a separate receiving field with an edited PICTURE and MOVE the numeric value to it; the MOVE does the conversion and editing. The result is then placed into the report line (or is the report line).

cobol
1
2
3
4
5
6
01 WS-AMOUNT PIC S9(7)V99. 01 WS-EDITED PIC +++,+++,++9.99. MOVE 1234567.89 TO WS-AMOUNT MOVE WS-AMOUNT TO WS-EDITED *> Result: +1,234,567.89 MOVE WS-EDITED TO RL-AMOUNT *> into report line

WRITE and ADVANCING

For sequential report files you use WRITE with BEFORE or AFTER ADVANCING to control vertical position. AFTER ADVANCING 1 LINE advances one line and then writes the record; that is the normal way to print one line per WRITE. AFTER ADVANCING n LINES (or identifier LINES) skips n lines before writing. AFTER ADVANCING PAGE ejects to the next page and then writes (good for the first line on a new page, e.g. a heading). BEFORE ADVANCING PAGE writes the record and then ejects (good for the last line on the current page). ADVANCING is only valid for sequential (and line-sequential) files, not VSAM.

WRITE ADVANCING options
FormEffectTypical use
AFTER ADVANCING 1 LINEAdvance one line, then write record.Each WRITE on next line.
AFTER ADVANCING n LINESAdvance n lines, then write.Skip lines or leave gaps.
AFTER ADVANCING PAGEEject to next page, then write.First line on new page (e.g. heading).
BEFORE ADVANCING PAGEWrite record, then eject.Last line on page, then new page.

Step-by-Step: Building a Single Detail Line

  1. Define the report record with a one-byte control area and fields (or reference modification) for each column. Set the first byte to space for single space, 0 for double, or 1 for new page as needed.
  2. Move or clear the line (e.g. MOVE SPACES TO REPORT-LINE). Move character data into the correct positions. For numbers, move the numeric value to an edited field (PICTURE with Z, $, comma, etc.), then move that edited value into the report line at the right column.
  3. Write the record with WRITE report-record AFTER ADVANCING 1 LINE (or the appropriate ADVANCING option). If your printer uses ASA, the first byte will be used for spacing; otherwise the whole record is printed and ADVANCING still controls line position in the logical file.

Step-by-Step: When to Use Each ASA Character

  1. Normal detail lines: set the first byte to space. Each WRITE with AFTER ADVANCING 1 LINE then prints single-spaced.
  2. Section spacing: use 0 for one blank line between lines (double space) or - for two blank lines (triple space) before the next printed line.
  3. Page heading: set the first byte to 1 and write the heading record so it appears at the top of the next page. Alternatively use WRITE ... AFTER ADVANCING PAGE and keep the first byte as space.
  4. Overstrike: use + only when you intend to print on the same line again (e.g. bold by writing twice). Otherwise avoid + so lines do not overwrite each other.

Line Length and Fixed-Width Layout

Traditional mainframe printers use 132-column lines. Your report record is often PIC X(132) or a structure that totals 132 bytes. The first byte is ASA; the remaining 131 are visible. Define the layout so that each field starts and ends at known positions. For example, columns 1–30 for name, 31–39 blank, 40–52 for amount. Use FILLER PIC X(n) for gaps. When you MOVE a value into a field that is shorter than the source, data is truncated; when the source is shorter, the rest of the field is often space-filled (depending on the type of move). For edited numeric fields, the PICTURE length determines how many characters are produced (e.g. ZZ,ZZZ,ZZ9.99 is 12 characters including comma and period). Ensure the report line is long enough so that no data overflows.

Difference Between ADVANCING and ASA

ADVANCING is a COBOL WRITE option: it tells the runtime how to advance the logical position (line or page) when the record is written. ASA is a convention for the first byte of the record: the device (or print driver) interprets that byte and may advance the paper or form accordingly. In some setups both are used: you use WRITE AFTER ADVANCING 1 LINE for normal flow, and still set the first byte to space so that if the file is printed elsewhere, the control character is correct. In other setups only one of the two is relevant (e.g. only ADVANCING, or only ASA). Check your environment.

Best Practices

Test Your Knowledge

Test Your Knowledge

1. The first byte of a print record is often used for:

  • Page number
  • ASA carriage control (space, 0, -, +, 1)
  • Record length
  • File name

2. To get a number to print with commas and no leading zeros you use:

  • PIC 9(7)
  • An edited PICTURE like Z,ZZZ,ZZ9
  • PIC X(10)
  • WRITE only

3. WRITE ... AFTER ADVANCING 1 LINE means:

  • Print then move down
  • Move down one line then print
  • Print on same line
  • Skip a page