The DATA DIVISION

The data division does not create anything at the moment it is read: it describes the storage a program will use, and the compiler turns the description into offsets and lengths. Every section of the division is one kind of storage, and the four sections come in one order.

Storage, described

Four sections, in this order: FILE SECTION, WORKING-STORAGE SECTION, LOCAL-STORAGE SECTION and LINKAGE SECTION. A program uses the ones it needs, and every one of them may be left out.

  1. FILE SECTION: the record layout of a file, under an FD that names the file.
  2. WORKING-STORAGE SECTION: the fields the program owns, created when the program starts and kept until it ends.
  3. LOCAL-STORAGE SECTION: fields created each time the program is entered, which is what a recursive program needs.
  4. LINKAGE SECTION: the parameters a caller passes in, described but not allocated by this program.

A level 01 record

Working storage is a list of level 01 entries. An entry may be a single field, or a record: a level 01 name with fields beneath it, each with a higher level number. The record has no storage of its own; it is the fields it contains that take the bytes.

IDENTIFICATION DIVISION. PROGRAM-ID. REC. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER. 05 CUSTOMER-ID PIC 9(4) VALUE 101. 05 CUSTOMER-NAME PIC X(10) VALUE "ADA". 01 TOTAL-FIELD PIC 9(3) VALUE 7. PROCEDURE DIVISION. DISPLAY "ID: " CUSTOMER-ID. DISPLAY "NAME: " CUSTOMER-NAME. DISPLAY "TOTAL:" TOTAL-FIELD. STOP RUN.

cobc -x -free -Wall -o rec rec.cob
./rec

ID: 0101
NAME: ADA
TOTAL:007

The three lines show what the pictures claim. PIC 9(4) holds four digits, so 101 is printed as 0101; PIC X(10) holds ten characters, so the three letters of ADA are followed by seven spaces, which the line above cannot show; PIC 9(3) prints 7 as 007. Nothing was padded at the DISPLAY: the field was already that wide, and the lesson on PICTURE takes the clauses apart.

FILE SECTION

A file is declared twice, in two divisions that must agree. The environment division names it with a SELECT, and the file section describes the record with an FD and a level 01 entry under it. A program that declares a file and never opens it builds and runs, and no file appears on the disk:

IDENTIFICATION DIVISION. PROGRAM-ID. FS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "customers.dat" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(4). 05 CUSTOMER-NAME PIC X(10). WORKING-STORAGE SECTION. 01 TITLE-LINE PIC X(8) VALUE "CUSTOMER". PROCEDURE DIVISION. DISPLAY TITLE-LINE. STOP RUN.

cobc -x -free -Wall -o fs fs.cob
./fs

CUSTOMER

The two halves of the declaration are checked against each other. A SELECT with no FD stops the build with error: missing file description for FILE CUSTOMER-FILE, and an FD with no SELECT is refused because the name is not a file name:

IDENTIFICATION DIVISION. PROGRAM-ID. NOSEL. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(4). WORKING-STORAGE SECTION. 01 TITLE-LINE PIC X(8) VALUE "CUSTOMER". PROCEDURE DIVISION. DISPLAY TITLE-LINE. STOP RUN.

cobc -x -free -Wall -o nosel nosel.cob

nosel.cob:6: error: 'CUSTOMER-FILE' is not defined
4 | DATA DIVISION.
5 | FILE SECTION.
6 > FD CUSTOMER-FILE.
7 | 01 CUSTOMER-RECORD.
8 | 05 CUSTOMER-ID PIC 9(4).
nosel.cob:6: error: 'CUSTOMER-FILE' is not a file name

Working storage below the file section is the usual shape of a program: the file records first, then the fields the program computes with. The lesson on SELECT and FD takes the declaration further, and sequential files opens the first one.

The other three sections

LOCAL-STORAGE looks like working storage and behaves differently: its fields are created when the program is entered and thrown away when it returns, so two calls to the same program do not share them. A program that is not called, and not recursive, cannot tell the two sections apart. The lesson on recursive COBOL needs the difference, and the one on resetting a table uses it to get a fresh table on every call.

LINKAGE describes storage the program does not own: the parameters a caller passes with CALL ... USING. The description must match what the caller passes, byte for byte, because the program reads the caller's memory through it. Both halves of that agreement are in the lesson on CALL and USING.

The order of the sections

The four sections have one order, and it is the order above: a file section written after working storage is not read as a file section, and the SELECT above it is then left without an FD. The compiler reports that as the missing file description, not as the misplaced section, which is why the mistake is worth recognizing from the message.

Inside a section, order does not matter: two level 01 entries may be written in any order, and a field may be used before the field declared below it.

Exercises

  1. Add a second record to the working storage of the first program, with a customer number and an amount, and print both fields with a label. Which field is wider than its value, and by how much?
  2. Move the FILE SECTION of the file program below WORKING-STORAGE and build it. Copy the first error line, then put the section back.
  3. Delete the FD from the file program and build. Which line does the message point at, and what does it call the file?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which section holds fields that exist for the whole run of the program?

    Answer

    WORKING-STORAGE SECTION

    Working storage is created when the program starts and kept until it ends; local storage is created at each entry and linkage is the caller's storage.

  2. Which section describes the parameters a caller passes in?

    Answer

    LINKAGE SECTION

    Linkage describes storage the program does not own: the caller allocates it and the program reads it through the description.

  3. Which section comes first in the DATA DIVISION?

    Answer

    FILE SECTION

    The file records come first, then working storage, then local storage, then linkage; a file section written later is not read as one.

  4. What does an FD describe?

    Answer

    The record layout of a file whose name a SELECT gives

    SELECT names the file and its organization; the FD and the level 01 entry under it describe one record of it.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER. 05 CUSTOMER-ID PIC 9(4) VALUE 101. PROCEDURE DIVISION. DISPLAY "ID=" CUSTOMER-ID. STOP RUN.

    Answer

    ID=0101

    PIC 9(4) always holds four digits, so the value 101 is printed with a leading zero.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "customers.dat" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(4). WORKING-STORAGE SECTION. 01 TITLE-LINE PIC X(8) VALUE "CUSTOMER". PROCEDURE DIVISION. DISPLAY TITLE-LINE. STOP RUN.

    Answer

    CUSTOMER

    Declaring a file allocates no file and opens none: the program runs its one DISPLAY and never touches the disk.

  7. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "customers.dat" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. WORKING-STORAGE SECTION. 01 TITLE-LINE PIC X(8) VALUE "CUSTOMER". PROCEDURE DIVISION. DISPLAY TITLE-LINE. STOP RUN.

    Answer

    error: missing file description for FILE CUSTOMER-FILE

    A SELECT must have an FD in the file section, and the compiler names the file whose description it could not find.

  8. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(4). PROCEDURE DIVISION. STOP RUN.

    Answer

    error: 'CUSTOMER-FILE' is not defined

    An FD names a file that a SELECT in the environment division must declare first; without it the name is undefined.

  9. A program declares a file and never opens it. What happens on the disk?

    Answer

    Nothing: no file is created and no file is read

    The declaration describes a record layout; the file itself appears only when an OPEN creates it or an INPUT reads it.

  10. Where is the record layout of a file written?

    Answer

    Under the FD in the FILE SECTION

    The environment division names the file; the file section describes one record of it, with an FD and a level 01 entry.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev