WRITE statement

The WRITE statement releases a logical record to an output or input/output file.

When the WRITE statement is executed:

  • The associated sequential file must be open in OUTPUT or EXTEND mode.
  • The associated indexed or relative file must be open in OUTPUT, I-O, or EXTEND mode.

Format 1: WRITE statement for sequential files

Read syntax diagramSkip visual syntax diagramWRITErecord-name-1FROMidentifier-1BEFOREAFTERADVANCINGidentifier-2integer-1LINELINESmnemonic-name-1PAGEphrase 1invalid_keynot_invalid_keyEND-WRITE
phrase 1
Read syntax diagramSkip visual syntax diagramATEND-OF-PAGEEOPimperative-statement-3NOTATEND-OF-PAGEEOPimperative-statement-4
invalid_key
Read syntax diagramSkip visual syntax diagramINVALIDKEYimperative-statement-1
not_invalid_key
Read syntax diagramSkip visual syntax diagramNOT INVALIDKEYimperative-statement-2

Format 2: WRITE statement for indexed and relative files

Read syntax diagramSkip visual syntax diagramWRITErecord-name-1FROMidentifier-1INVALIDKEYimperative-statement-1NOT INVALIDKEYimperative-statement-2END-WRITE

Format 3: WRITE statement for line-sequential files

Read syntax diagramSkip visual syntax diagramWRITErecord-name-1FROMidentifier-1AFTERADVANCINGidentifier-2integer-1LINELINESPAGEEND-WRITE

record-name-1
Must be defined in a DATA DIVISION FD entry. record-name-1 can be qualified. It must not be associated with a sort or merge file.

For relative files, the number of character positions in the record being written can be different from the number of character positions in the record being replaced.

FROM phrase
The result of the execution of the WRITE statement with the FROM identifier-1 phrase is equivalent to the execution of the following statements in the order specified:

MOVE identifier-1 TO record-name-1.
WRITE record-name-1. 

The MOVE is performed according to the rules for a MOVE statement without the CORRESPONDING phrase.

identifier-1
identifier-1 can reference any of the following items:
  • A data item defined in the WORKING-STORAGE SECTION, the LOCAL-STORAGE SECTION, or the LINKAGE SECTION
  • A record description for another previously opened file
  • An alphanumeric function
  • A national function

identifier-1 must be a valid sending item for a MOVE statement with record-name-1 as the receiving item.

identifier-1 and record-name-1 must not refer to the same storage area.

After the WRITE statement is executed, the information is still available in identifier-1. (See INTO and FROM phrases under "Common processing facilities".)

identifier-2
Must be an integer data item.

ADVANCING phrase

The ADVANCING phrase controls positioning of the output record on the page.

The BEFORE and AFTER phrases are not supported for VSAM files. QSAM files are sequentially organized. The ADVANCING and END-OF-PAGE phrases control the vertical positioning of each line on a printed page.

You can specify the ADVANCING PAGE and END-OF-PAGE phrases in a single WRITE statement.

If the printed page is held on an intermediate device (a disk, for example), the format can appear different from the expected format when the output is edited or browsed.

ADVANCING phrase rules

When the ADVANCING phrase is specified, the following rules apply:

  1. When BEFORE ADVANCING is specified, the line is printed before the page is advanced.
  2. When AFTER ADVANCING is specified, the page is advanced before the line is printed.
  3. When identifier-2 is specified, the page is advanced the number of lines equal to the current value in identifier-2. identifier-2 must name an elementary integer data item.
  4. When integer is specified, the page is advanced the number of lines equal to the value of integer.
  5. Integer or the value in identifier-2 can be zero.
  6. When PAGE is specified, the record is printed on the logical page BEFORE or AFTER (depending on the phrase used) the device is positioned to the next logical page. If PAGE has no meaning for the device used, then BEFORE or AFTER (depending on the phrase specified) ADVANCING 1 LINE is provided.

    If the FD entry contains a LINAGE clause, the repositioning is to the first printable line of the next page, as specified in that clause. If the LINAGE clause is omitted, the repositioning is to line 1 of the next succeeding page.

  7. When mnemonic-name is specified, a skip to channels 1 through 12, or space suppression, takes place. mnemonic-name must be equated with environment-name-1 in the SPECIAL-NAMES paragraph.

    The mnemonic-name phrase can also be specified for stacker selection with a card punch file. When using stacker selection, WRITE AFTER ADVANCING must be used.

The ADVANCING phrase of the WRITE statement, or the presence of a LINAGE clause on the file, causes a carriage control character to be generated in the record that is written. If the corresponding file is described with the EXTERNAL clause, all file connectors within the run unit must be defined such that carriage control characters will be generated for records that are written. That is, if all the files have a LINAGE clause, some of the programs can use the WRITE statement with the ADVANCING phrase and other programs can use the WRITE statement without the ADVANCING phrase. However, if none of the files has a LINAGE clause, then if any of the programs use the WRITE statement with the ADVANCING phrase, all of the programs in the run unit that have a WRITE statement must use the WRITE statement with the ADVANCING phrase.

When the ADVANCING phrase is omitted, automatic line advancing is provided, as if AFTER ADVANCING 1 LINE had been specified.

LINAGE-COUNTER rules

If the LINAGE clause is specified for this file, the associated LINAGE-COUNTER special register is modified during the execution of the WRITE statement, according to the following rules:

  1. If ADVANCING PAGE is specified, LINAGE-COUNTER is reset to 1.
  2. If ADVANCING identifier-2 or integer is specified, LINAGE-COUNTER is increased by the value in identifier-2 or integer.
  3. If the ADVANCING phrase is omitted, LINAGE-COUNTER is increased by 1.
  4. When the device is repositioned to the first available line of a new page, LINAGE-COUNTER is reset to 1.

Usage note: If you use the ADV compiler option, the compiler adds 1 byte to the record length in order to allow for the control character. If in your record definition you already reserve the first byte for the control character, you should use the NOADV option. For files defined with the LINAGE clause, the NOADV option has no effect. The compiler processes these files as if the ADV option were specified.

END-OF-PAGE phrases

The AT END-OF-PAGE phrase is not supported for VSAM files.

When END-OF-PAGE is specified, and the logical end of the printed page is reached during execution of the WRITE statement, the END-OF-PAGE imperative-statement is executed. When the END-OF-PAGE phrase is specified, the FD entry for this file must contain a LINAGE clause.

The logical end of the printed page is specified in the associated LINAGE clause.

An END-OF-PAGE condition is reached when execution of a WRITE END-OF-PAGE statement causes printing or spacing within the footing area of a page body. This occurs when execution of such a WRITE statement causes the value in the LINAGE-COUNTER special register to equal or exceed the value specified in the WITH FOOTING phrase of the LINAGE clause. The WRITE statement is executed, and then the END-OF-PAGE imperative-statement is executed.

An automatic page overflow condition is reached whenever the execution of any given WRITE statement (with or without the END-OF-PAGE phrase) cannot be completely executed within the current page body. This occurs when a WRITE statement, if executed, would cause the value in the LINAGE-COUNTER to exceed the number of lines for the page body specified in the LINAGE clause. In this case, the line is printed BEFORE or AFTER (depending on the option specified) the device is repositioned to the first printable line on the next logical page, as specified in the LINAGE clause. If the END-OF-PAGE phrase is specified, the END-OF-PAGE imperative-statement is then executed.

If the WITH FOOTING phrase of the LINAGE clause is not specified, the automatic page overflow condition exists because no end-of-page condition (as distinct from the page overflow condition) can be detected.

If the WITH FOOTING phrase is specified, but the execution of a given WRITE statement would cause the LINAGE-COUNTER to exceed both the footing value and the page body value specified in the LINAGE clause, then both the end-of-page condition and the automatic page overflow condition occur simultaneously.

The keywords END-OF-PAGE and EOP are equivalent.

You can specify both the ADVANCING PAGE phrase and the END-OF-PAGE phrase in a single WRITE statement.

INVALID KEY phrases

The INVALID KEY phrase is not supported for VSAM sequential files.

An invalid key condition is caused by the following cases:

  • For sequential files, an attempt is made to write beyond the externally defined boundary of the file.
  • For indexed files:
    • An attempt is made to write beyond the externally defined boundary of the file.
    • ACCESS SEQUENTIAL is specified and the file is opened OUTPUT, and the value of the prime record key is not greater than that of the previous record.
    • The file is opened OUTPUT or I-O and the value of the prime record key equals that of an already existing record.
  • For relative files:
    • An attempt is made to write beyond the externally defined boundary of the file.
    • When the access mode is random or dynamic and the RELATIVE KEY data item specifies a record that already exists in the file.
    • The number of significant digits in the relative record number is larger than the size of the relative key data item for the file.

When an invalid key condition occurs:

  • If the INVALID KEY phrase is specified, imperative-statement-1 is executed. For details of invalid key processing, see Invalid key condition.
  • Otherwise, the WRITE statement is unsuccessful and the contents of record-name are unaffected (except for QSAM files) and the following case occurs:
    • For sequential files, the file status key, if specified, is updated and an EXCEPTION/ERROR condition exists.

      If an explicit or implicit EXCEPTION/ERROR procedure is specified for the file, the procedure is executed. If no such procedure is specified, the results are unpredictable.

    • For relative and indexed files, program execution proceeds according to the rules described by Invalid key condition under "Common processing facilities".

      The INVALID KEY conditions that apply to a relative file in OPEN OUTPUT mode also apply to one in OPEN EXTEND mode.

  • If the NOT INVALID KEY phrase is specified and a valid key condition exists at the end of the execution of the WRITE statement, control is passed to imperative-statement-4.

Both the INVALID KEY phrase and an applicable EXCEPTION/ERROR procedure can be omitted.

END-WRITE phrase

This explicit scope terminator serves to delimit the scope of the WRITE statement. END-WRITE permits a conditional WRITE statement to be nested in another conditional statement. END-WRITE can also be used with an imperative WRITE statement.

For more information, see Delimited scope statements.