Data Item


What is data item?


  • A data item is an identifier used to hold the value for processing in the program.
  • A data item is also called as a data item/data name.
  • Every data item used in the COBOL program should declare in the DATA DIVISION of the same program.
  • data item declaration should always contains the data type in its length (except group data item).
  • A data item 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.

Data item Declaration Syntax -


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

For example - Declaring a data item 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.
  • data-item-name - Specifies the name of the data item. In the above example, it is WS-NUMERIC-VAR.
  • data-type-character - Specifies the type of the data item. In the above example, it is numeric(9).
  • data-item-length - Specifies the data item length to store the data. In the above example, it is 03.
  • literal-value - Specifies the initial value assigned to the data item. In the above example, it is 123.

Data item Naming Rules -


  • Data item 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-$
  • Data item name length can be a minimum of 1 character and a maximum of 30 characters. For example -
    Valid Invalid
    A
    WS-LARGEST-data item-NAME
    1
    WS-LARGEST-data item-NAME-OF-LE31
  • The data item name is a combination of COBOL user-defined words.
  • Data item 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 data item 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 data item name length differs from the data item length. The data item length depends on the declaration in WORKING-STORAGE SECTION.

data item/data item types -


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

  • Individual data item
  • Group data item
  • Elementary data item

Individual data item -


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

Group data item -


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

Elementary data item -


  • The elementary data item is a data item that is declared under the group data item.
  • Syntax -
     group-level-number group-data-item-name.    
         elem-level-number elem-data-item-name    
    				[PIC/PICTURE  data-type-picture-character(data item-length)]
                    [VALUE literal-value].
  • The elementary data item should always have a picture clause.
  • Elementary data items are not unique and can use the same name under different group data items. In this case, the duplicate elementary data items refer to the OF keyword followed by the group data item name.
  • The elementary data item can declare with level numbers from 02 to 49.
  • For Example
    ----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
           01 LEVEL-GROUP.                                                  
          * ELEMENTARY DATA ITEM                                   
              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 data items and declared as shown below -

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

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

data item Diagram

A is assigned with the memory location from 1000 to 1010. So, referring to the data item 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 data item 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 data item. However, B is declared as a group data item for the entire 6 bytes, includes C and FILLER. So, by referring to data item 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 -

data item 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                                  
       01 LEVEL-1       PIC 9(03) VALUE 256.                            
      * GROUP DATA ITEM                                       
       01 LEVEL-GROUP.                                                  
      * ELEMENTARY DATA ITEM                                  
          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 -

data item COBOL program output

Explaining Example -

  • In the above example, the LEVEL-1 individual data item is declared and initialized with the value 256. LEVEL-GROUP data item declared as a group data item with two elementary data items LEVEL-21 and LEVEL-22 of each 03 digits long.
  • Displaying the data items LEVEL-GROUP displays the data of two elementary data items LEVEL-21 and LEVEL-22.