MODULE 2 · DATA AND PICTURE · 1/7

WORKING-STORAGE and level numbers

12 min30 XPExercise

Every variable a program uses is declared in the DATA DIVISION. The WORKING-STORAGE SECTION holds items that exist for the whole run of the program: counters, switches, totals, work areas and print lines.

Level numbers build structure

COBOL describes data the way a business form does: a record made of fields, some of which are made of smaller fields. The nesting is shown with level numbers:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. LEVELS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-EMPLOYEE.
           05  WS-EMP-ID        PIC 9(5)  VALUE 123.
           05  WS-EMP-NAME      PIC X(10) VALUE "O'NEILL".
           05  WS-HIRE-DATE.
               10  WS-HIRE-YYYY PIC 9(4)  VALUE 2019.
               10  WS-HIRE-MM   PIC 9(2)  VALUE 3.
               10  WS-HIRE-DD   PIC 9(2)  VALUE 1.
       77  WS-PAGE-NO           PIC 9(3)  VALUE 1.
       PROCEDURE DIVISION.
           DISPLAY "ID:     " WS-EMP-ID.
           DISPLAY "HIRED:  " WS-HIRE-DATE.
           DISPLAY "YEAR:   " WS-HIRE-YYYY.
           DISPLAY "RECORD: [" WS-EMPLOYEE "]".
           DISPLAY "PAGE:   " WS-PAGE-NO.
           STOP RUN.
ID:     00123
HIRED:  20190301
YEAR:   2019
RECORD: [00123O'NEILL   20190301]
PAGE:   001
  • 01 starts a record. It goes in Area A.
  • 02 to 49 are subordinate levels. A higher number means "belongs to the item above". The values only need to increase; shops use steps of 5 (05, 10, 15) so a level can be inserted later without renumbering.
  • 77 declares a single standalone item that can't have children and can't be part of a group. It is common in older code; many shops now use a 01 with a PIC instead, which does the same job.

Group versus elementary items

An elementary item has a PIC clause: WS-EMP-ID, WS-HIRE-YYYY. It's where data actually lives.

A group item has no PIC: WS-EMPLOYEE, WS-HIRE-DATE. It is simply a name for the elementary items beneath it, laid end to end. So:

  • WS-HIRE-DATE is 4 + 2 + 2 = 8 characters: 20190301.
  • WS-EMPLOYEE is 5 + 10 + 8 = 23 characters.
  • Displaying a group shows all its bytes run together, with no separators, padding spaces and all. Look at O'NEILL in the output: the name is padded to its full ten characters.

A group is always treated as alphanumeric, even if every field in it is numeric. That matters for MOVE, as you will see later in this module.

The compiler enforces the structure. Give a group a PIC and GnuCOBOL reports group item 'WS-G' cannot have PICTURE clause; put an 05 under a 77 and it reports level number must begin with 01 or 77.

On the job

Record layouts like this are usually kept in copybooks, shared files that every program reading the same dataset includes with COPY. Reading a copybook and working out each field's position and length is a daily task; add up the PIC sizes as you go.

Your task

Build a customer record.

  1. Turn the three fields into 05-level items of a group called CUST-REC (a 01 with no PIC). Keep their names and pictures.
  2. Add a 77-level item WS-RECORDS PIC 9(3) VALUE 1.
  3. After the three ACCEPTs, display:

    CUSTOMER ID: 10042 CITY: DUBLIN RECORD: [10042MARY KELLY DUBLIN ] RECORDS: 001

for the input 10042, MARY KELLY, DUBLIN. The RECORD: line shows the whole group between square brackets, so the padding inside the record is visible.

Check your understanding

1. Which of these describes a group item?
2. How long is a group containing PIC 9(5), PIC X(10) and PIC 9(8) items?
3. What is special about level 77?
fixed format
Run your program to see its output here. The first visible test's input and datasets are used.
Submit to grade your program against every test.