Data Item


A data item is a name used to hold the value for processing in the program. It is also called as a Variable. The variable declaration should always contain the data type with its length (except the group variable). It is a pointer to the memory location from the starting to the ending byte based on the size. It should be declared in the DATA DIVISION of the program.

Syntax -

 level-number variable-name    
		[PIC/PICTURE  data-type-character(variable-length)]
        [VALUE literal-value].
Note! All statements coded in [ ] are optional.

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

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

Variable Naming Rules -

  • Variable name is a combination of COBOL user-defined words that are formed by alphabets(A-Z), digits(0-9) and hyphens (-).
  • Variable name length can be a minimum of 1 character and a maximum of 30 characters.
  • Variable name should not be a COBOL reserved word(i.e., ACCEPT, ADD, MOVE, etc..).
  • The variable name does not allow spaces between the words and should combine them with '-'. Starting or ending character should be an alphabet(A-Z) or a digit(0-9) but not a hyphen(-) or space.

For example -

Valid Invalid
WS-A
VAR
A12
1A2
WS-2
WS
 WS
WS_A
WS*
MOVE
WS-$
WS A
-WS
WS-
Note! The variable name length differs from the variable length. The variable length depends on the declaration in WORKING-STORAGE SECTION.

Variable types -


The variables are divided into three types based on their declaration, and those are -

  • Individual variable
  • Group variable
  • Elementary variable

Individual variable -


  • An individual variable is declared individually but neither under a group variable nor as a group variable.
  • It always has the picture clause during its declaration.
  • It should be unique in the program or its calling programs.
  • It can be declared using level numbers 01 to 49 and 77.

For example -

----+----1----+----2----+----3----+----4----+----5----+
      * INDIVIDUAL VARIABLE
       01 LEVEL-1       PIC 9(03) VALUE 256.

Group variable -


  • A Group variable is declared without the picture clause and has elementary variables declared under it.
  • It does not have a picture clause.
  • Its name should be unique in a program or its calling programs.
  • These can be declared using level numbers from 01 to 48.

For example -

----+----1----+----2----+----3----+----4----+----5----+
      * GROUP VARIABLE
       01 LEVEL-GROUP.
      * ELEMENTARY VARIABLE 
          05 LEVEL-21       PIC 9(03) VALUE 256.
          05 LEVEL-22       PIC 9(03) VALUE 128.

Elementary variable -


  • The elementary variable is a variable that is declared under the group variable.
  • It should always have a PIC clause.
  • Elementary variables are not unique and can use the same name under different group variables. In this case, the elementary variables use the OF keyword followed by the group variable name.
  • It can be declared using level numbers from 02 to 49.

For example -

----+----1----+----2----+----3----+----4----+----5----+
       01 LEVEL-GROUP.
      * ELEMENTARY 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 are declared as shown below -

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

Let us assume variable A allocated 10 bytes of memory from 1000 to 1010 and variable B with 6 bytes from 1100 to 1106. The below diagram represents memory allocation for the declaration -

data item Diagram

Variable A is allocated with the memory location from 1000 to 1010. So, referring to the variable A, it accesses the 10-byte data from memory location 1000 to 1010. Variable C is allocated with the memory location from 1100 to 1103. So, the variable C accesses the 3-byte data from memory location 1100 to 1103.

We can't access the FILLER directly because it is not a variable. However, variable B is declared as a group variable for 6 bytes, including variable C and FILLER. So, by referring to variable B, we can access the 6 bytes of data from memory.

Practical Example -


Scenario - Different variable types declaration, initialization and display in COBOL program.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. VARTYPES. 
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.
      * Individual variable
       01 LEVEL-1       PIC 9(03) VALUE 256.
      * Group variable
       01 LEVEL-GROUP.
      * Elementary variables
          05 LEVEL-21       PIC 9(03) VALUE 256.
          05 LEVEL-22       PIC 9(03) VALUE 128.

       PROCEDURE DIVISION.

           DISPLAY 'INDIVIDUAL VARIABLE   : ' LEVEL-1. 
           DISPLAY 'GROUP VARIABLE        : ' LEVEL-GROUP.
           DISPLAY 'ELEMENTARY VARIABLE-1 : ' LEVEL-21. 
           DISPLAY 'ELEMENTARY VARIABLE-2 : ' LEVEL-22. 
           STOP RUN. 

Output -

INDIVIDUAL VARIABLE   : 256
GROUP VARIABLE        : 256128
ELEMENTARY VARIABLE-1 : 256
ELEMENTARY VARIABLE-2 : 128

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.
  • LEVEL-GROUP displays the data of two elementary variables LEVEL-21 and LEVEL-22.