Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 38 ยท Divisions and Data Description

Data Division, Sections and Level Numbers

Learn the COBOL data division: file, working-storage, local-storage and linkage sections, level numbers 01-49, 66, 77 and 88, group and elementary items, and FILLER.

COBOL has no int x;. Instead, the data division describes memory as a hierarchy of named items with exact sizes and formats. You decide how many bytes a customer name takes and how a balance is stored before a single statement runs. This looks heavy at first but it is the reason COBOL records map directly onto files.

The sections of the data division

COBOL
       DATA DIVISION.
       FILE SECTION.
       FD  CUSTOMER-FILE.
       01  CUSTOMER-RECORD.
           ...
       WORKING-STORAGE SECTION.
       01  WS-COUNTER      PIC 9(4) VALUE 0.
       LOCAL-STORAGE SECTION.
       01  LS-TEMP         PIC X(20).
       LINKAGE SECTION.
       01  LK-PARAMETER    PIC X(10).
Section Holds Lifetime
FILE SECTION Record layouts for each file (FD entries). While the file is open.
WORKING-STORAGE SECTION Your variables and constants. Whole run, initialised once.
LOCAL-STORAGE SECTION Variables created fresh on every call. One call of the program.
LINKAGE SECTION Parameters passed by a caller (see subprograms). Owned by the caller.

They must appear in that order, and you use only the ones you need. Most beginner programs need just WORKING-STORAGE.

Level numbers and hierarchy

Every entry starts with a level number. The lower the number, the higher in the hierarchy.

Level Meaning
01 A top-level record or independent item. Starts in area A.
02-49 Subordinate items (children). They are grouped under the nearest preceding lower-numbered item.
66 RENAMES entry (regrouping; see redefines and renames).
77 Independent elementary item (no hierarchy). Largely replaced by 01.
88 Condition name: a named test on the item above it.
COBOL
       01  CUSTOMER.
           05  CUST-ID        PIC 9(6).
           05  CUST-NAME.
               10  FIRST-NAME PIC X(15).
               10  LAST-NAME  PIC X(20).
           05  CUST-BALANCE   PIC S9(7)V99.

An item with children is a group item; one without is an elementary item, which is the only kind that has a PIC. Group items are always treated as alphanumeric text when you MOVE them (see MOVE and edited pictures). Here CUSTOMER is a group of CUST-ID, CUST-NAME (itself a group) and CUST-BALANCE, and the record is 6 + 15 + 20 + 9 = 50 bytes.

Level numbers do not have to be consecutive or fixed; 05, 10, 15 (steps of five) is a convention that leaves room for inserting levels later.

Using the names

Any data name can be used on its own when it is unique. When two groups share a child name, qualify it with OF or IN:

COBOL
           MOVE 100 TO CUST-ID OF CUSTOMER
           DISPLAY FIRST-NAME OF CUST-NAME OF CUSTOMER

FILLER

Parts of a record that you never refer to by name are called FILLER. Since COBOL-2002 you may even omit the name:

COBOL
       01  REPORT-LINE.
           05  FILLER         PIC X(5) VALUE SPACES.
           05  RL-NAME        PIC X(20).
           05  FILLER         PIC X(3) VALUE " | ".
           05  RL-AMOUNT      PIC ZZZ,ZZ9.99.

Initial values

VALUE sets the starting contents in working storage. Items without VALUE hold whatever the compiler leaves (GnuCOBOL clears them; mainframes may leave junk), so initialise explicitly. The INITIALIZE statement resets a group to zeros for numeric items and spaces for alphanumeric ones (see picture clauses and usage and value).

Computing a record layout (Python stand-in)

The browser cannot compile COBOL, but a record layout is just arithmetic. The runnable blocks below are Python: they walk a list of (level, name, size) entries and compute each item's offset and size, the way the compiler lays the record out.

Python Record offsets from a level-number hierarchy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading a record (Python stand-in)

Python Slicing a fixed-width record

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Giving a PIC to a group item, or forgetting it on an elementary item.
  • Starting an 01 or FD outside area A.
  • Using 77 levels in new code; prefer 01.
  • Using a reserved word such as DATE as a data name.
  • Forgetting the period after every entry (each entry ends with a period).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which level number starts a top-level record?

    01 is the top-level record. 77 is a stand-alone elementary item, and 05 is a subordinate level.
  2. 2. Which kind of item can carry a PIC clause?

    A PIC describes one elementary value. Groups are built from their children.
  3. 3. Which section holds the variables you declare for your own use, initialised once at program start?

    WORKING-STORAGE holds your variables and constants for the whole run.
  4. 4. A record has fields of 6, 15, 20 and 9 bytes. How long is the record?

    6 + 15 + 20 + 9 = 50 bytes. The record size is the sum of its elementary items (no alignment padding for DISPLAY data).

Summary

  • The data division has FILE, WORKING-STORAGE, LOCAL-STORAGE and LINKAGE sections.
  • Level numbers form a hierarchy: 01 records, 02-49 children, 66 renames, 77 stand-alone items, 88 condition names.
  • Group items have no PIC; elementary items do. A group's size is the sum of its parts.
  • Use FILLER for unnamed parts and VALUE or INITIALIZE to set starting contents.
  • Qualify duplicate names with OF.

Next: PICTURE Clauses