BudiBadu Logo
Samplebadu

COBOL by Example: Level Numbers

COBOL 2002

Understanding COBOL level numbers for defining data hierarchy from 01 for record-level structures to 49 for nested field definitions, implementing group items without PIC clauses that contain elementary items with PIC clauses, using level 77 for independent standalone variables outside any record structure, and working with level 88 condition names for boolean logic and readable conditional statements.

Code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. LEVELS-DEMO.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  STUDENT-RECORD.
           05  STUDENT-ID      PIC 9(5).
           05  STUDENT-NAME.
               10 FIRST-NAME   PIC X(10).
               10 LAST-NAME    PIC X(10).
       77  INDEPENDENT-VAR     PIC 99 VALUE 0.
       
       PROCEDURE DIVISION.
           MOVE 12345 TO STUDENT-ID.
           MOVE "John" TO FIRST-NAME.
           MOVE "Doe" TO LAST-NAME.
           DISPLAY "Student: " FIRST-NAME " " LAST-NAME.
           STOP RUN.

Explanation

COBOL uses a system of level numbers to define data hierarchy, similar to a struct or class in other languages. The 01 level always represents a record—the top-level grouping. Numbers from 02 to 49 (conventionally 05, 10, 15) represent fields within that record. Higher numbers indicate deeper nesting.

This hierarchical structure allows you to manipulate data at different levels of granularity. In this example, you can MOVE data to the individual FIRST-NAME field, or you can MOVE data to the entire STUDENT-NAME group item, which affects both first and last names simultaneously. This is powerful for reading and writing complex file records.

There are also special level numbers. 77 is used for independent data items that are not part of any record structure (like loop counters). 88 is used for condition names, which act as boolean switches associated with a variable, allowing for readable code like IF STUDENT-PASSED instead of IF GRADE > 50.

Code Breakdown

6
01 STUDENT-RECORD. The root of our data structure. It has no PIC clause because it doesn't hold data directly; it holds other fields.
7
05 STUDENT-ID. A child of STUDENT-RECORD. It is an "elementary item" because it has a PIC clause (PIC 9(5)).
8
05 STUDENT-NAME. Another child of STUDENT-RECORD, but this is a "group item" because it is further subdivided into level 10 items.
9-10
10 FIRST-NAME / LAST-NAME. These are children of STUDENT-NAME. They are elementary items where the actual text data is stored.
11
77 INDEPENDENT-VAR. A standalone variable. It cannot be subdivided and is not part of the STUDENT-RECORD hierarchy.