• A Variable is an identifier or data name used to hold the value for processing in the program.
  • A Variable is also called as a data item/data name.
  • Every variable used in the COBOL program should declare in the DATA DIVISION of the same program.
  • Variable declaration should always contains the data type in its length (except group variable).
  • A Variable is a pointer to the memory location from starting byte to the ending byte based on the specified length in the declaration. This memory gets allocated during the program execution.

Variable Declaration Syntax -

 level-number variable-name    
				[PIC/PICTURE  data-type-character(variable-length)]
                [VALUE literal-value].

For example - Declaring a variable of numeric type to store a value 123.

 01 WS-NUMERIC-VAR      PIC 9(03) VALUE 123.
  • level-number - Specifies the level number of the declaration from 01 to 49. In the above example, it is 01.
  • variable-name - Specifies the name of the variable. In the above example, it is WS-NUMERIC-VAR.
  • data-type-character - Specifies the type of the variable. In the above example, it is numeric(9).
  • variable-length - Specifies the variable length to store the data. In the above example, it is 03.
  • literal-value - Specifies the initial value assigned to the variable. In the above example, it is 123.

Variable Naming Rules -

  • Variable name is a combination of alphabets(A-Z), digits(0-9) and hyphens (-).
    For example -
    Valid Invalid
    WS-A
    VAR
    A12
    1A2
    WS-2
    1ABC
    WRITE1
    MOVE1
    WS_A
    WS*
    W()
    W{
    ABC&
    WS-$
  • Variable name length can be a minimum of 1 character and a maximum of 30 characters.
    For example -
    Valid Invalid
    A
    WS-LARGEST-VARIABLE-NAME
    1
    WS-LARGEST-VARIABLE-NAME-OF-LE31
  • The variable name is a combination of COBOL user-defined words.
  • Variable name should not be a COBOL reserved word(i.e., ACCEPT, ADD, MOVE, etc..).
    For example -
    Valid Invalid
    MOVE1
    AMOVE
    MOVEA
    ADD1
    WS-ADD
    MOVE
    ADD
  • The variable name does not allow spaces.
    For example (Invalid) -
    Valid Invalid
    WS
     WS
    WS A
    WS VAR1
  • Starting character should be an alphabet(A-Z) or a digit(0-9) but not a hyphen(-) or space.
    Valid Invalid
    -WS
     WS
    1WS
    WS-1
Note! The variable name length differs from the variable length. The variable length depends on the declaration in WORKING-STORAGE SECTION.

Variable/data item types -

The data items/variables are divided into three categories based on the level numbers and those are -

  • Individual variable / Independent data item
  • Group variable / Group data item
  • Elementary Variable / Elementary data item

Individual variable / Independent data item -

  • The individual variable is a variable that will not be subdivided into multiple variables in the future.
  • Syntax -
     level-number variable-name    
    				[PIC/PICTURE  data-type-picture-character(variable-length)]
                    [VALUE literal-value].
  • An individual variable always has the picture clause and no sub-items.
  • Individual variable names should be unique in the program or its calling programs.
  • The individual variable can declare with level numbers from 01 to 49.
  • For Example
    ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
          * INDIVIDUAL DATA ITEM/VARIABLE                                   
           01 LEVEL-1       PIC 9(03) VALUE 256.            

Group variable / Group data item -

  • A Group variable is a variable that is declared without the picture clause.
  • Syntax -
     group-level-number group-variable-name.    
         elem-level-number elem-variable-name    
    				[PIC/PICTURE  data-type-picture-character(variable-length)]
                    [VALUE literal-value].
  • The group variable does not have a picture clause.
  • Group variable names should be unique in a program or its calling programs.
  • Group variable can declare with level numbers from 01 to 48.
  • For Example
    ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
          * GROUP DATA ITEM/VARIABLE                                        
           01 LEVEL-GROUP.                                                  
          * ELEMENTARY DATA ITEM/VARIABLE                                   
              05 LEVEL-21       PIC 9(03) VALUE 256.                        
              05 LEVEL-22       PIC 9(03) VALUE 128.            

Elementary Variable / Elementary data item -

  • The elementary variable is a variable that is declared under the group variable.
  • Syntax -
     group-level-number group-variable-name.    
         elem-level-number elem-variable-name    
    				[PIC/PICTURE  data-type-picture-character(variable-length)]
                    [VALUE literal-value].
  • The elementary variable should always have a picture clause.
  • Elementary variables are not unique and can use the same name under different group variables. In this case, the duplicate elementary variables refer to the OF keyword followed by the group variable name.
  • The elementary variable can declare with level numbers from 02 to 49.
  • For Example
    ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
           01 LEVEL-GROUP.                                                  
          * ELEMENTARY DATA ITEM/VARIABLE                                   
              05 LEVEL-21       PIC 9(03) VALUE 256.                        
              05 LEVEL-22       PIC 9(03) VALUE 128.            

Memory allocation understanding example -

Let's take the below example to understand better. A, B & C are variables and declared as shown below -

01 A		PIC X(10).
01 B.
   02 C  	PIC X(03).
   02 FILLER 	PIC X(03).

Assume variable A allocated with 10 bytes of memory from 1000 to 1010, and variable B with 6 bytes of memory from 1100 to 1106 (that include variable C and FILLER). The below diagram representing memory bytes for the above declaration.

Variable Diagram

A is assigned with the memory location from 1000 to 1010. So, referring to the variable A accesses the 10 bytes data from memory location 1000 to 1010. C is assigned with the memory location from 1100 to 1103. So, referring to the variable C accesses the 3 bytes data from memory location 1100 to 1103.

The memory allocated for FILLER can't be accessed because FILLER is not a variable. However, B is declared as a group variable for the entire 6 bytes, includes C and FILLER. So, by referring to variable B, we can access the 6 bytes of data from memory.

Practical Example -

Scenario - Explain the different data item types declaration, initialization and display in COBOL program.

Code -

Variable COBOL program code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. VARTYPES.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
      * INDIVIDUAL DATA ITEM/VARIABLE                                   
       01 LEVEL-1       PIC 9(03) VALUE 256.                            
      * GROUP DATA ITEM/VARIABLE                                        
       01 LEVEL-GROUP.                                                  
      * ELEMENTARY DATA ITEM/VARIABLE                                   
          05 LEVEL-21       PIC 9(03) VALUE 256.                        
          05 LEVEL-22       PIC 9(03) VALUE 128.                        
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           DISPLAY 'INDIVIDUAL DATA ITEMS  : ' LEVEL-1.                 
           DISPLAY 'GROUP DATA ITEM        : ' LEVEL-GROUP.             
           DISPLAY 'ELEMENTARY DATA ITEM-1 : ' LEVEL-21.                
           DISPLAY 'ELEMENTARY DATA ITEM-2 : ' LEVEL-22.                
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

Variable COBOL program output

Explaining Example -

In the above example, the LEVEL-1 individual variable is declared and initialized with the value 256. LEVEL-GROUP variable declared as a group variable with two elementary variables LEVEL-21 and LEVEL-22 of each 03 digits long.

Displaying the variables LEVEL-GROUP displays the data of two elementary variables LEVEL-21 and LEVEL-22.