Skip to content
systemcoding.dev
Mainframe

COBOL Level Numbers 01, 05, 77 and 88 Explained

COBOL level numbers build records from groups and elementary items. Learn what 01, 02 to 49, 66, 77 and 88 mean, with a record layout and its output.

6 min read
Summarize in AI
Flat illustration of a 3270 terminal showing JCL beside a mainframe.

The two digits at the start of a COBOL data description, such as 01 or 05, are level numbers, and they are the language's way of building a structure out of smaller pieces. A level number says where an item sits in the hierarchy of a record: the lower the number, the more important the item is. Learning the handful of special numbers is the fastest way to read an unfamiliar COBOL data division. This post explains them with one record layout in Enterprise COBOL for z/OS 6.4, and shows what happens when you move or display a whole record. It follows the posts on divisions and PIC clauses and assumes nothing else.

Groups and elementary items

An elementary item is a field that is not divided further and has a PIC clause. A group item is made of other items and has no PIC clause. Its size is the total of its parts, and it is treated as alphanumeric when you move or display it.

The level number says which is which. Here is a customer record:

level.cblcobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LEVELS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CUSTOMER-RECORD.
           05  CUST-ID           PIC 9(6)  VALUE 104233.
           05  CUST-NAME.
               10  CUST-FIRST    PIC X(8)  VALUE 'GRACE'.
               10  CUST-LAST     PIC X(10) VALUE 'HOPPER'.
           05  CUST-BALANCE      PIC S9(5)V99 VALUE 1200.50.
       PROCEDURE DIVISION.
           DISPLAY 'NAME=[' CUST-NAME ']'
           DISPLAY 'FIRST=[' CUST-FIRST ']'
           DISPLAY 'RECORD=[' CUSTOMER-RECORD ']'
           GOBACK.
Text
NAME=[GRACE     HOPPER    ]
FIRST=[GRACE   ]
RECORD=[104233GRACE     HOPPER    012005{]

CUSTOMER-RECORD is the top level and a group. CUST-NAME is a group inside it, with two elementary items. The rest are elementary. The displayed record is the bytes of all items joined together: six digits, eight characters, ten characters and the seven-digit balance. The balance is signed, so its last digit shows as an overpunch. The value 1200.50 stored as 0120050 with a positive sign on the last digit 0 displays as 012005{, where { is how a positive zero appears in EBCDIC. Display of signed fields varies, which is why reports use edited fields.

The numbers you can use

LevelMeaning
01The top of a record or a standalone item. A group's name, or a field on its own
02 to 49Subordinate items inside a group. Nesting depth is shown by increasing numbers
66RENAMES: a new name for a range of existing items
77An independent elementary item, with no group above it
88A condition name, which gives a value of the item above it a name

You are free to pick numbers between 02 and 49, as long as a subordinate item has a higher number than its group. Programmers use 05, 10, 15 and so on, leaving gaps so a new level can be added without renumbering. Using 02, 03, 04 is legal and equally valid, and the jumps carry no meaning beyond order.

Rules of the hierarchy

An item belongs to the nearest group above it that has a lower number. In the example, CUST-FIRST at level 10 belongs to CUST-NAME at level 05, which belongs to CUSTOMER-RECORD at level 01. If the next item were also at level 05, the CUST-NAME group would end there.

A group item cannot have a PIC clause, and an elementary item must have one. A VALUE on a group sets the whole group as text and is rarely wise. Set values on the elementary items.

FILLER is a name for an item you do not use, such as spaces between fields in a report line. You can have as many as you like, and you cannot refer to one by name:

cobol
       01  REPORT-LINE.
           05  FILLER        PIC X(2)  VALUE SPACES.
           05  RL-NAME       PIC X(18).
           05  FILLER        PIC X(4)  VALUE SPACES.
           05  RL-AMOUNT     PIC ZZ,ZZ9.99.

In current compilers, the word FILLER can be left out entirely and the item written with just a level number and a PIC, and the compiler treats it as filler.

Level 77

A 77 is a standalone item, such as a counter, that is not part of any record. It cannot be subdivided and has no items below it:

cobol
       77  WS-COUNTER        PIC 9(4) VALUE 0.

Most modern code uses 01 for the same purpose, since the two behave the same in nearly every way. The compiler accepts both. The only real difference is that a 77 must stay in the working storage or linkage section and cannot be a group, whereas an 01 could later grow into a group by adding subordinate items.

Level 88

An 88 is a condition name. It attaches to the elementary item directly above it and gives meaning to a value or a set of values:

cobol
       01  WS-STATUS         PIC X.
           88  STATUS-ACTIVE     VALUE 'A'.
           88  STATUS-CLOSED     VALUE 'C' 'X'.

A test such as IF STATUS-ACTIVE is true when WS-STATUS holds A. The post on 88-level names and SET TO TRUE covers it in depth. The key point here is that an 88 has no PIC and uses no storage of its own.

Level 66

66 renames a range of items under a second name. It appears after all the items of the record and has the clause RENAMES first THRU last:

cobol
       66  CUST-IDENT RENAMES CUST-ID THRU CUST-LAST.

It is rare in new programs, and the REDEFINES clause, which gives a second view of the same storage, is the usual tool when you need one. The post on REDEFINES and RENAMES covers both.

Moving a group

A move from one group to another is a move of text, byte by byte, with no regard to the items inside:

cobol
           MOVE CUSTOMER-RECORD TO CUSTOMER-COPY

If the two groups have the same layout, the copy is exact. If they differ, such as when a numeric field in the source lines up with a text field in the target, the bytes copy and the meaning is lost. To move field by field by matching names, use MOVE CORRESPONDING, which copies each elementary item whose name exists in both groups, converting as needed.

Reading someone else's data division

When you open an unfamiliar program, read the level numbers before the pictures. The indentation in the source is only a convention, so trust the numbers: an item belongs to the nearest group above it with a lower level. Draw the tree on paper for a large record and write the byte offset of each item beside it. The offset of an item is the sum of the sizes of everything before it in the record, and knowing it is what lets you match a COBOL record against a hex dump of a file.

For the example, CUST-ID starts at offset 0 and takes 6 bytes. CUST-NAME starts at 6 and takes 18, which is the sum of CUST-FIRST at 8 and CUST-LAST at 10. CUST-BALANCE starts at 24 and takes 7, so the record is 31 bytes long. A file that holds these records must have a record length of 31, and a mismatch between that and the LRECL in the JCL is a classic cause of garbled output.

Why levels matter in files

The records of a file are described under an FD with an 01 level. A file can have several 01 records under one FD, and they share the same buffer, which is how a file with different record types is read: one buffer, several layouts laid on top of it.

The rules are in the Data Division chapter of the Enterprise COBOL for z/OS Language Reference.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.