MainframeMaster

COBOL Tutorial

COBOL WRITE Statement

Progress0 of 0 lessons

WRITE Statement Overview

The WRITE statement is used to add new records to a file. It is one of the fundamental file processing verbs in COBOL, allowing programs to create and append data to files. The WRITE statement supports various options depending on the file organization and access mode.

Basic Syntax

cobol
1
2
3
4
5
6
7
WRITE record-name [FROM data-name] [ADVANCING {identifier-1 | integer-1 | mnemonic-name} {LINE | LINES | PAGE}] [BEFORE | AFTER] [AT END-OF-PAGE imperative-statement-1] [NOT AT END-OF-PAGE imperative-statement-2] [END-WRITE]

The WRITE statement writes a record to a file. The record-name must be defined in the FILE SECTION and correspond to a file opened in OUTPUT, EXTEND, or I-O mode.

File Mode Requirements

File ModeWRITE AllowedPurpose
OUTPUTYesCreate new file, write records
EXTENDYesAppend records to existing file
I-OYesRead and write operations
INPUTNoRead-only operations

WRITE Statement Options

The WRITE statement supports several optional clauses that provide additional functionality for different file organizations and output requirements.

FROM Clause

The FROM clause copies data from a working storage field to the record before writing:

cobol
1
2
3
4
5
6
7
8
9
* Basic FROM clause WRITE CUSTOMER-RECORD FROM WS-CUSTOMER-DATA. * FROM clause with literal WRITE HEADER-RECORD FROM "CUSTOMER MASTER FILE". * FROM clause with calculated data COMPUTE WS-TOTAL = WS-AMOUNT + WS-TAX WRITE TOTAL-RECORD FROM WS-TOTAL.

The FROM clause is useful when you want to write data from a different source than the record description, or when you need to perform calculations before writing.

ADVANCING Clause

The ADVANCING clause controls line spacing for LINE SEQUENTIAL files:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
* Single line advance (default) WRITE REPORT-LINE ADVANCING 1 LINE. * Double spacing WRITE REPORT-LINE ADVANCING 2 LINES. * Page break WRITE REPORT-LINE ADVANCING PAGE. * Variable spacing WRITE REPORT-LINE ADVANCING WS-LINE-COUNT LINES. * No advance WRITE REPORT-LINE ADVANCING 0 LINES.

The ADVANCING clause is required for LINE SEQUENTIAL files and ignored for other file organizations. It controls how many lines to advance after writing the record.

BEFORE/AFTER ADVANCING

Controls when the line advance occurs relative to the WRITE operation:

cobol
1
2
3
4
5
6
7
8
* AFTER ADVANCING (default) - advance after writing WRITE REPORT-LINE AFTER ADVANCING 1 LINE. * BEFORE ADVANCING - advance before writing WRITE REPORT-LINE BEFORE ADVANCING 1 LINE. * BEFORE ADVANCING PAGE - page break before writing WRITE REPORT-LINE BEFORE ADVANCING PAGE.

BEFORE ADVANCING is useful when you want to write at a specific line position, while AFTER ADVANCING is the standard behavior for most applications.

AT END-OF-PAGE Clause

Handles page overflow conditions for LINE SEQUENTIAL files:

cobol
1
2
3
4
5
6
7
WRITE REPORT-LINE AT END-OF-PAGE PERFORM PRINT-PAGE-HEADER MOVE 1 TO LINE-COUNTER NOT AT END-OF-PAGE ADD 1 TO LINE-COUNTER END-WRITE.

The AT END-OF-PAGE clause allows you to handle page overflow conditions and perform special processing when a page break occurs.

File Organization Considerations

The behavior of the WRITE statement varies depending on the file organization. Understanding these differences is crucial for proper file handling.

Sequential Files

WRITE operations with sequential files are straightforward:

cobol
1
2
3
4
5
6
* Sequential file WRITE OPEN OUTPUT CUSTOMER-FILE MOVE "JOHN DOE" TO CUSTOMER-NAME MOVE "123 MAIN ST" TO CUSTOMER-ADDRESS WRITE CUSTOMER-RECORD CLOSE CUSTOMER-FILE.

Records are written in the order they are processed. No special considerations are needed for sequential files.

Indexed Files

Indexed files require key field values and support duplicate key handling:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
* Indexed file WRITE with error handling OPEN OUTPUT CUSTOMER-FILE MOVE "C001" TO CUSTOMER-ID MOVE "JOHN DOE" TO CUSTOMER-NAME WRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Duplicate key: " CUSTOMER-ID PERFORM ERROR-HANDLING NOT INVALID KEY DISPLAY "Record written successfully" END-WRITE CLOSE CUSTOMER-FILE.

The INVALID KEY clause is essential for handling duplicate key errors in indexed files. The key field must be properly set before the WRITE operation.

Relative Files

Relative files require a relative record number for positioning:

cobol
1
2
3
4
5
6
7
8
9
10
11
* Relative file WRITE OPEN OUTPUT CUSTOMER-FILE MOVE 1 TO CUSTOMER-ID MOVE "JOHN DOE" TO CUSTOMER-NAME WRITE CUSTOMER-RECORD INVALID KEY DISPLAY "Invalid relative record number: " CUSTOMER-ID NOT INVALID KEY DISPLAY "Record written at position: " CUSTOMER-ID END-WRITE CLOSE CUSTOMER-FILE.

The relative record number (key field) determines where the record is written. Gaps in the file are filled with empty records.

LINE SEQUENTIAL Files

LINE SEQUENTIAL files require the ADVANCING clause:

cobol
1
2
3
4
5
6
7
* LINE SEQUENTIAL file WRITE OPEN OUTPUT REPORT-FILE MOVE "CUSTOMER REPORT" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 2 LINES MOVE "================" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE CLOSE REPORT-FILE.

The ADVANCING clause is required for LINE SEQUENTIAL files to control line spacing. This is commonly used for report generation.

Error Handling and File Status

Proper error handling is essential when using WRITE statements. Understanding file status codes helps diagnose and handle errors effectively.

Common File Status Codes

Status CodeMeaningAction Required
00Successful operationContinue processing
22Duplicate key (indexed files)Handle duplicate key logic
23No record foundCheck record existence
30Permanent errorCheck file permissions, disk space
34Boundary violationCheck record size limits
35File not foundCheck file existence
37File not openEnsure file is opened

Error Handling Example

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
IDENTIFICATION DIVISION. PROGRAM-ID. WRITE-ERROR-HANDLING. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC X(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(45). WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 ERROR-MESSAGE PIC X(50). PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM WRITE-RECORDS PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN OUTPUT CUSTOMER-FILE IF CUSTOMER-STATUS NOT = "00" DISPLAY "Error opening file: " CUSTOMER-STATUS STOP RUN END-IF. WRITE-RECORDS. MOVE "C001" TO CUSTOMER-ID MOVE "JOHN DOE" TO CUSTOMER-NAME MOVE "123 MAIN STREET" TO CUSTOMER-ADDRESS WRITE CUSTOMER-RECORD INVALID KEY PERFORM HANDLE-INVALID-KEY NOT INVALID KEY DISPLAY "Record written successfully" END-WRITE * Check file status after WRITE IF CUSTOMER-STATUS NOT = "00" PERFORM HANDLE-WRITE-ERROR END-IF. HANDLE-INVALID-KEY. EVALUATE CUSTOMER-STATUS WHEN "22" MOVE "Duplicate key detected" TO ERROR-MESSAGE WHEN "30" MOVE "Permanent error occurred" TO ERROR-MESSAGE WHEN "34" MOVE "Boundary violation" TO ERROR-MESSAGE WHEN OTHER MOVE "Unknown error occurred" TO ERROR-MESSAGE END-EVALUATE DISPLAY "Error: " ERROR-MESSAGE. HANDLE-WRITE-ERROR. DISPLAY "Write error - Status: " CUSTOMER-STATUS PERFORM FINALIZATION STOP RUN. FINALIZATION. CLOSE CUSTOMER-FILE.

This example demonstrates comprehensive error handling for WRITE operations, including INVALID KEY processing and file status checking.

Practical Examples

These practical examples demonstrate common WRITE statement usage patterns for different scenarios and file types.

Example 1: Creating a Customer Master File

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
IDENTIFICATION DIVISION. PROGRAM-ID. CREATE-CUSTOMER-FILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUSTOMER-ID FILE STATUS IS CUSTOMER-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC X(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(50). 05 CUSTOMER-PHONE PIC X(15). WORKING-STORAGE SECTION. 01 CUSTOMER-STATUS PIC XX. 01 RECORD-COUNT PIC 9(5) VALUE 0. PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM CREATE-CUSTOMERS PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN OUTPUT CUSTOMER-FILE IF CUSTOMER-STATUS NOT = "00" DISPLAY "Error opening file: " CUSTOMER-STATUS STOP RUN END-IF. CREATE-CUSTOMERS. * Customer 1 MOVE "C001" TO CUSTOMER-ID MOVE "JOHN DOE" TO CUSTOMER-NAME MOVE "123 MAIN STREET, ANYTOWN, USA" TO CUSTOMER-ADDRESS MOVE "(555) 123-4567" TO CUSTOMER-PHONE WRITE CUSTOMER-RECORD ADD 1 TO RECORD-COUNT * Customer 2 MOVE "C002" TO CUSTOMER-ID MOVE "JANE SMITH" TO CUSTOMER-NAME MOVE "456 OAK AVENUE, SOMETOWN, USA" TO CUSTOMER-ADDRESS MOVE "(555) 987-6543" TO CUSTOMER-PHONE WRITE CUSTOMER-RECORD ADD 1 TO RECORD-COUNT * Customer 3 MOVE "C003" TO CUSTOMER-ID MOVE "BOB JOHNSON" TO CUSTOMER-NAME MOVE "789 PINE ROAD, OTHERTOWN, USA" TO CUSTOMER-ADDRESS MOVE "(555) 456-7890" TO CUSTOMER-PHONE WRITE CUSTOMER-RECORD ADD 1 TO RECORD-COUNT. FINALIZATION. CLOSE CUSTOMER-FILE DISPLAY "Created " RECORD-COUNT " customer records".

This example creates an indexed customer master file with three sample records.

Example 2: Report Generation with LINE SEQUENTIAL

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
IDENTIFICATION DIVISION. PROGRAM-ID. GENERATE-REPORT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REPORT-FILE ASSIGN TO "SALES-REPORT.TXT" ORGANIZATION IS LINE SEQUENTIAL FILE STATUS IS REPORT-STATUS. DATA DIVISION. FILE SECTION. FD REPORT-FILE LABEL RECORDS ARE OMITTED RECORD CONTAINS 80 CHARACTERS. 01 REPORT-LINE PIC X(80). WORKING-STORAGE SECTION. 01 REPORT-STATUS PIC XX. 01 LINE-COUNTER PIC 9(3) VALUE 0. 01 PAGE-COUNTER PIC 9(3) VALUE 1. PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM WRITE-REPORT-HEADER PERFORM WRITE-REPORT-BODY PERFORM WRITE-REPORT-FOOTER PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN OUTPUT REPORT-FILE. WRITE-REPORT-HEADER. MOVE "SALES REPORT" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 2 LINES MOVE "=============" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE MOVE "DATE: 12/31/2023" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 2 LINES. WRITE-REPORT-BODY. MOVE "CUSTOMER SALES AMOUNT DATE" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE MOVE "-------- ------------ ----" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE MOVE "JOHN DOE $1,250.00 12/15/2023" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE MOVE "JANE SMITH $2,100.00 12/20/2023" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE MOVE "BOB JOHNSON $950.00 12/25/2023" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE. WRITE-REPORT-FOOTER. MOVE "-------- ------------ ----" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE MOVE "TOTAL $4,300.00" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 2 LINES MOVE "END OF REPORT" TO REPORT-LINE WRITE REPORT-LINE ADVANCING 1 LINE. FINALIZATION. CLOSE REPORT-FILE.

This example demonstrates report generation using LINE SEQUENTIAL files with proper line spacing and formatting.

Example 3: Appending to Sequential File

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
49
50
51
52
53
54
55
56
57
58
59
60
61
IDENTIFICATION DIVISION. PROGRAM-ID. APPEND-TRANSACTIONS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TRANSACTION-FILE ASSIGN TO "TRANSACTIONS.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS TRANSACTION-STATUS. DATA DIVISION. FILE SECTION. FD TRANSACTION-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 50 CHARACTERS. 01 TRANSACTION-RECORD. 05 TRANSACTION-ID PIC X(10). 05 TRANSACTION-DATE PIC 9(8). 05 TRANSACTION-AMT PIC 9(7)V99. 05 TRANSACTION-TYPE PIC X. 05 FILLER PIC X(23). WORKING-STORAGE SECTION. 01 TRANSACTION-STATUS PIC XX. 01 APPEND-COUNT PIC 9(3) VALUE 0. PROCEDURE DIVISION. MAIN-PROCESS. PERFORM INITIALIZATION PERFORM APPEND-TRANSACTIONS PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN EXTEND TRANSACTION-FILE IF TRANSACTION-STATUS NOT = "00" DISPLAY "Error opening file: " TRANSACTION-STATUS STOP RUN END-IF. APPEND-TRANSACTIONS. * Append transaction 1 MOVE "TXN001" TO TRANSACTION-ID MOVE 20231231 TO TRANSACTION-DATE MOVE 1250.50 TO TRANSACTION-AMT MOVE "S" TO TRANSACTION-TYPE WRITE TRANSACTION-RECORD ADD 1 TO APPEND-COUNT * Append transaction 2 MOVE "TXN002" TO TRANSACTION-ID MOVE 20231231 TO TRANSACTION-DATE MOVE 750.25 TO TRANSACTION-AMT MOVE "P" TO TRANSACTION-TYPE WRITE TRANSACTION-RECORD ADD 1 TO APPEND-COUNT. FINALIZATION. CLOSE TRANSACTION-FILE DISPLAY "Appended " APPEND-COUNT " transactions".

This example demonstrates appending records to an existing sequential file using the EXTEND mode.

Best Practices and Tips

Following these best practices ensures reliable and efficient WRITE operations in COBOL programs.

General Best Practices

  • Always check file status after WRITE operations to detect errors
  • Use appropriate file modes - OUTPUT for new files, EXTEND for appending, I-O for read/write
  • Handle INVALID KEY conditions for indexed and relative files
  • Validate data before writing to ensure data integrity
  • Use meaningful record names that clearly identify the data being written
  • Close files properly to ensure all data is written to disk

Performance Considerations

  • Batch WRITE operations when possible to reduce I/O overhead
  • Use appropriate blocking factors for sequential files
  • Minimize file opens/closes by processing multiple records per file session
  • Consider buffering for high-volume WRITE operations
  • Avoid unnecessary FROM clauses when the record description is sufficient

Error Handling Guidelines

  • Always include error handling for production programs
  • Log errors appropriately for debugging and monitoring
  • Provide meaningful error messages that help identify the problem
  • Handle file status codes systematically using EVALUATE statements
  • Consider recovery strategies for different types of errors
  • Test error conditions during development and testing

Common Pitfalls to Avoid

  • Writing to files opened in INPUT mode - this will cause errors
  • Forgetting the ADVANCING clause for LINE SEQUENTIAL files
  • Not setting key fields before writing to indexed files
  • Ignoring file status codes after WRITE operations
  • Writing records larger than defined in the record description
  • Not closing files after writing operations
  • Using wrong file organization for the intended purpose

Test Your Knowledge

1. What is the basic syntax of the WRITE statement in COBOL?

  • WRITE record-name FROM data-name
  • WRITE record-name
  • WRITE file-name
  • WRITE data-name TO record-name

2. When is the ADVANCING clause required in a WRITE statement?

  • Always required for all files
  • Only for LINE SEQUENTIAL files
  • Only for SEQUENTIAL files
  • Never required

3. What does the FROM clause do in a WRITE statement?

  • Specifies the source file
  • Copies data from a working storage field to the record before writing
  • Specifies the destination file
  • Controls file positioning

4. Which file organization requires a prior READ before using WRITE?

  • SEQUENTIAL
  • INDEXED
  • RELATIVE
  • All of the above

5. What is the purpose of the INVALID KEY clause in a WRITE statement?

  • To handle file open errors
  • To handle duplicate key errors in indexed files
  • To handle end-of-file conditions
  • To handle syntax errors

Frequently Asked Questions