Level Numbers


Level number plays the most important role in declaring the variables in the application programs. The level number specifies the hierarchy or level of data items or variables. They provide a way to create simple and complex structures, allowing for nested and repeated data items.

Syntax -

level-number {variable|FILLER} ...
  • level-number -

    • Level number is a one or two-digit numeric value. (For example - 01, 02, ...49, 66, 77 and 88).
    • Level numbers 01 or 77 should begin in Area A.
    • A level-numbers 02 through 49, 66 and 88 can begin in either Area A or Area B.
    • Single-digit level-numbers 1 through 9 can be substituted for level-numbers 01 through 09.
  • variable - Identifies the variable used in the program.
  • FILLER - FILLER is a variable that is not explicitly referred in a program. The keyword FILLER is optional. In a MOVE CORRESPONDING, ADD CORRESPONDING or SUBTRACT CORRESPONDING, FILLER items are ignored.

Level Number Types -


Level numbers are two types based on their usage purpose -

  • General purpose level numbers (01 to 49)
  • Special purpose level numbers (66, 77 and 88)

General purpose level numbers (01 to 49) -


General purpose level numbers are used for regular variables declaration. The level number hierarchy starts from 01 through 49. The level numbers hierarchy should be in ascending order from lower number to higher number. i.e., 01 is the highest level number, and 49 is the lowest level number.

The variables declared with general-purpose level numbers can be divided into two types and we will discuss more about them in variable topic.

  • Group variable - A Group variable is a variable that is declared without the picture clause and has elementary variables declared under it.
  • Elementary variable - The elementary variable is a variable that is declared under the group variable.

The picture clauses specify the length of the elementary variable. The length of the group variables is calculated by summing the sizes of the elementary variables declared under them.

Example -

Scenario - Declaring employee record.

----+----1----+----2----+----3----+----4----+----5----+
       01 EMPLOYEE-RECORD.
          05 EMP-ID              PIC 9(5).
          05 EMP-NAME.
             10 FIRST-NAME       PIC X(15).
             10 MIDDLE-NAME      PIC X(10).
             10 LAST-NAME        PIC X(20).
          05 DATE-OF-BIRTH.
             10 DOB-YEAR         PIC 9(4).
             10 DOB-MONTH        PIC 9(2).
             10 DOB-DAY          PIC 9(2).

Explaining Example -

In the above example, EMPLOYEE-RECORD, EMP-NAME, and DATE-OF-BIRTH are group variables, and FIRST-NAME, MIDDLE-NAME, LAST-NAME, DOB-YEAR, DOB-MONTH, DOB-DAY are the elementary variables.

EMP-NAME is a group variable with three elementary variables: FIRST-NAME (15 bytes), MIDDLE-NAME(10 bytes), and LAST-NAME(20 bytes). So, the total length of the EMP-NAME is 45 bytes.

Similarly, DATE-OF-BIRTH is a group variable with three elementary variables: DOB-YEAR(4 bytes), DOB-MONTH(2 bytes), and DOB-DAY(2 bytes). So, the total length of the EMP-NAME is 8 bytes.

Lastly, EMPLOYEE-RECORD has three variables: EMP-ID(5 characters), EMP-NAME(45 bytes), DATE-OF-BIRTH(8 bytes). So, the total employee record size is 58 bytes.

Special purpose level numbers (66, 77 and 88) -


These level numbers are used for special purposes like renaming a variable and declaring individual variables and conditional names. Those are -

  • 66 level number
  • 77 level number
  • 88 level number

66 level number

Level number 66 is used to create another logical group by regrouping the elementary variables of a group. The RENAMES keyword is used along with the 66-level number to rename the group.

Example -

Scenario - Create a employee record with EMP-ID and EMP-NAME alone.

----+----1----+----2----+----3----+----4----+----5----+
       01 EMPLOYEE-RECORD.
          05 EMP-ID              PIC 9(5).
          05 EMP-NAME.
             10 FIRST-NAME       PIC X(15).
             10 MIDDLE-NAME      PIC X(10).
             10 LAST-NAME        PIC X(20).
          05 DATE-OF-BIRTH.
             10 DOB-YEAR         PIC 9(4).
             10 DOB-MONTH        PIC 9(2).
             10 DOB-DAY          PIC 9(2).
			 
	   66 EMPLOYEE-REC01 RENAMES EMP-ID THRU EMP-NAME.

In the above example, EMPLOYEE-REC01 contains only EMP-ID and EMPLOYEE-NAME.

77 level number

Info! Level number 77 has been set for deletion from the COBOL language. It has been explained here to understand better if you encounter it in an existing program. Level number 77 is not supposed to code in new programs.

In some scenarios, the variables are neither allowed to convert as group variables nor as elementary variables, for example - constants. They have no immediate relationship to any other variables. Those variables are called Individual variables.

The special purpose level number 77 is used to declare the individual variables. It's not part of any hierarchical structure and doesn't subordinate other variables.

Example -

Scenario - Create a employee record with EMP-ID and EMP-NAME alone.

----+----1----+----2----+----3----+----4----+----5----+
       77 WS-PI     PIC 9V9(2) VALUE 3.14.

In the above example, WS-PI contains the value 3.14 and it is not allowed to change its declaration.

88 level number

The 88-level number defines a name for a value or a set of values under the variable. The 88-level number can't declare a variable, but it is used to provide a descriptive name for a condition. The name associated with 88 level numbers is called as Condition Name, and the variable with 88 level numbers attached to it is called as Conditional Variable.

Example -

Scenario - condition name with all its formats.

----+----1----+----2----+----3----+----4----+----5----+
       01 WS-ALPHABET       PIC X(01).
	  * Condition names with single values
          88 ALPHABET-A     VALUE "A". 
          88 ALPHABET-S     VALUE "S".
		  
      * Condition names with multiple values 
          88 VOWELS         VALUE "A" "E" "I" "O" "U".
		  
      * Condition names with range of values	
          88 VALID-ALPHABET VALUE "A" THROUGH "Z".	  
          88 CONSONANTS     VALUE "B" THRU "D" 
                                  "F" THRU "H"
                                  "J" THRU "N"
                                  "P" THRU "T"
                                  "V" THRU "Z". 

In the above example:

  • WS-ALPHABET is a condition variable with condition names under it.
  • ALPHABET-A and ALPHABET-S are the condition names with single values.
  • VOWELS is a condition name with multiple values.
  • VALID-ALPHABET and CONSONANTS are the condition names with range of values.