TExcelTAD
Home › COBOL › COBOL Level Numbers & Groups

COBOL Level Numbers & Groups

COBOL describes data as a hierarchy, like an outline. The level number at the start of each entry says where the item sits in that hierarchy.

Level Meaning
01 A record: the top of a hierarchy (starts in Area A)
02–49 Subordinate items inside a record. Higher number = deeper
66 RENAMES: an alternative name for a range of items (rare)
77 An independent elementary item that is not part of any record
88 A condition name attached to the item above it (own lesson)

Any increasing numbers work for nesting, but most shops step by 5 (01, 05, 10, 15) so new levels can be inserted later without renumbering.

Records and fields

A customer record broken down into fields:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CUSTOMER-REC.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CUSTOMER-RECORD.
           05  CUST-ID            PIC 9(5)  VALUE 10234.
           05  CUST-NAME.
               10  CUST-FIRST     PIC X(8)  VALUE "Linus".
               10  CUST-LAST      PIC X(10) VALUE "Okafor".
           05  CUST-CITY          PIC X(8)  VALUE "Lagos".
       PROCEDURE DIVISION.
           DISPLAY "Record:    [" CUSTOMER-RECORD "]"
           DISPLAY "Name:      [" CUST-NAME "]"
           DISPLAY "Last name: " CUST-LAST
           DISPLAY "Length:    " FUNCTION LENGTH(CUSTOMER-RECORD)
           STOP RUN.
Record:    [10234Linus   Okafor    Lagos   ]
Name:      [Linus   Okafor    ]
Last name: Okafor
Length:    31

CUSTOMER-RECORD and CUST-NAME are group items. A group is always treated as alphanumeric text: its value is just the bytes of its children laid side by side, 5 + 8 + 10 + 8 = 31 bytes.

Moving whole groups

Because a group is one block of bytes, a single MOVE copies every field at once. This is how records are copied from an input buffer to an output buffer:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. GROUP-MOVE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-ADDRESS-IN.
           05  IN-STREET          PIC X(12) VALUE "12 Elm Road".
           05  IN-ZIP             PIC 9(5)  VALUE 90210.
       01  WS-ADDRESS-OUT.
           05  OUT-STREET         PIC X(12).
           05  OUT-ZIP            PIC 9(5).
       PROCEDURE DIVISION.
           MOVE WS-ADDRESS-IN TO WS-ADDRESS-OUT
           DISPLAY "Street: " OUT-STREET
           DISPLAY "Zip:    " OUT-ZIP
           STOP RUN.
Street: 12 Elm Road
Zip:    90210

Note: A group move copies bytes without looking at the fields. It works perfectly when both groups have the same layout. If the layouts differ, the bytes land in the wrong fields. To copy field by field between different layouts, use MOVE CORRESPONDING (below) or individual moves.

MOVE CORRESPONDING

MOVE CORRESPONDING (or MOVE CORR) moves each child of one group to the child with the same name in another group, converting as needed:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MOVE-CORR.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  INPUT-ORDER.
           05  ORDER-NO           PIC 9(4)  VALUE 77.
           05  CUSTOMER           PIC X(6)  VALUE "ACME".
           05  AMOUNT             PIC 9(4)V99 VALUE 250.5.
       01  PRINT-LINE.
           05  CUSTOMER           PIC X(6).
           05  FILLER             PIC X(2)  VALUE "| ".
           05  ORDER-NO           PIC 9(4).
           05  FILLER             PIC X(2)  VALUE " |".
           05  AMOUNT             PIC ZZZ9.99.
       PROCEDURE DIVISION.
           MOVE CORRESPONDING INPUT-ORDER TO PRINT-LINE
           DISPLAY PRINT-LINE
           DISPLAY "Order " ORDER-NO OF INPUT-ORDER
           STOP RUN.
ACME  | 0077 | 250.50
Order 0077

When the same name exists in two groups, you qualify it with OF (or IN): ORDER-NO OF INPUT-ORDER.

Level 77 items

A 77 item is a standalone variable. It behaves exactly like an 01 elementary item; many style guides prefer 01 because it can later grow into a group.

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LEVEL-77.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       77  WS-PAGE-COUNT          PIC 9(3) VALUE 1.
       01  WS-LINE-COUNT          PIC 9(3) VALUE 55.
       PROCEDURE DIVISION.
           ADD 1 TO WS-PAGE-COUNT
           DISPLAY "Page " WS-PAGE-COUNT ", line " WS-LINE-COUNT
           STOP RUN.
Page 002, line 055

Test yourself

Which level number starts a record?

  1. 00
  2. 01
  3. 05
  4. 77

A group has children PIC X(4), PIC 9(3) and PIC X(2). How long is the group?

  1. 3 bytes
  2. 4 bytes
  3. 9 bytes
  4. It has no length

What does MOVE CORRESPONDING A TO B do?

  1. Copies A byte by byte into B
  2. Moves each child of A to the child of B with the same name
  3. Swaps A and B
  4. Compares A and B