COBOL DATA DIVISION
In COBOL every piece of data must be declared before the PROCEDURE DIVISION can use it, and the declaration says
exactly how big it is. There are no let x = … declarations in the middle of the code.
The sections of the DATA DIVISION
| Section | Holds | Lifetime |
|---|---|---|
FILE SECTION |
Record layouts of the files declared in FILE-CONTROL (FD entries) |
Buffer for the current record |
WORKING-STORAGE SECTION |
The program's own variables, counters, tables, messages | Whole run (kept between calls of a subprogram) |
LOCAL-STORAGE SECTION |
Variables re-initialised on each call of the program | One call |
LINKAGE SECTION |
Data passed in by a calling program | Owned by the caller |
The sections must appear in that order (FILE, WORKING-STORAGE, LOCAL-STORAGE, LINKAGE). Most programs use
WORKING-STORAGE for nearly everything.
Declaring data items
A data description entry has a level number, a name and clauses, and ends with a period:
01 WS-CUSTOMER-NAME PIC X(20) VALUE SPACES.
*^ ^ ^ ^
*| name picture initial value
*level numberPIC X(20)(orPICTURE X(20)) says the item holds 20 characters.PIC 9(5)holds a five-digit unsigned number.VALUEgives the starting value. Without it, the content is undefined in standard COBOL (GnuCOBOL fills alphanumeric items with spaces and numeric items with zeros by default, but don't rely on that).
IDENTIFICATION DIVISION.
PROGRAM-ID. WS-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CUSTOMER-NAME PIC X(20) VALUE "Maria Lopez".
01 WS-CUSTOMER-ID PIC 9(6) VALUE 42.
01 WS-BALANCE PIC S9(7)V99 VALUE -150.75.
PROCEDURE DIVISION.
DISPLAY "Name: [" WS-CUSTOMER-NAME "]"
DISPLAY "Id: " WS-CUSTOMER-ID
DISPLAY "Balance: " WS-BALANCE
STOP RUN.Name: [Maria Lopez ]
Id: 000042
Balance: -0000150.75
Look at the output closely:
- The name is padded with spaces to its full 20 characters (the brackets show the padding).
- The id
42is shown as000042: a numeric item always has exactly the digits its picture declares. - The signed balance shows its sign and the implied decimal point.
Fixed size is the point
Every item has a fixed size in bytes. A PIC X(20) item always occupies 20 bytes, and a PIC 9(6) item 6 bytes
(in the default DISPLAY usage, one byte per digit). This makes records predictable: a program can describe a
100-byte record from another system field by field and read it directly.
Elementary and group items
An item with a PIC clause is elementary. An item without PIC that has subordinate items is a group.
The group is simply the concatenation of its parts, and can be used as one alphanumeric field:
IDENTIFICATION DIVISION.
PROGRAM-ID. GROUP-INTRO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DATE.
05 WS-YEAR PIC 9(4) VALUE 2026.
05 FILLER PIC X VALUE "-".
05 WS-MONTH PIC 99 VALUE 3.
05 FILLER PIC X VALUE "-".
05 WS-DAY PIC 99 VALUE 9.
PROCEDURE DIVISION.
DISPLAY "Whole group: " WS-DATE
DISPLAY "Month only: " WS-MONTH
STOP RUN.Whole group: 2026-03-09
Month only: 03
FILLER is a nameless item: it reserves space (here a hyphen) that you never refer to directly.
LOCAL-STORAGE vs WORKING-STORAGE
In a main program they behave the same. The difference matters in subprograms: WORKING-STORAGE keeps its values
from one CALL to the next, while LOCAL-STORAGE starts fresh each time. You will see this in the subprograms
lesson.
IDENTIFICATION DIVISION.
PROGRAM-ID. LOCAL-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNTER PIC 99 VALUE 10.
LOCAL-STORAGE SECTION.
01 LS-COUNTER PIC 99 VALUE 20.
PROCEDURE DIVISION.
ADD 1 TO WS-COUNTER
ADD 1 TO LS-COUNTER
DISPLAY "Working-storage: " WS-COUNTER
DISPLAY "Local-storage: " LS-COUNTER
STOP RUN.Working-storage: 11
Local-storage: 21
Test yourself
How is PIC 9(6) VALUE 42 displayed?
- 42
- 42
- 000042
- +42
Which section holds the data a calling program passes to a subprogram?
- WORKING-STORAGE SECTION
- LOCAL-STORAGE SECTION
- LINKAGE SECTION
- FILE SECTION
What is a group item?
- An item with a PIC clause
- An item without PIC that contains subordinate items
- A table with OCCURS
- A constant