Open In App

Data Item Declaration in COBOL

Last Updated : 19 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Data Item declaration is nothing but declaring the variables used in a COBOL program. To declare the variables in a program, we should start with the level number and name of the variable. There are some optional clauses that we can declare after the name of the variable such as PICTURE, VALUE, DISPLAY, SIGN, OCCURS, USAGE, REDEFINES, SYNCHRONIZED, BLANK WITH ZERO., The most frequently used clauses are PICTURE and VALUE. Let us look at the syntax of the data item declaration.

Syntax:

LEVEL_NUMBER VARIABLE_NAME PICTURE-DATA_TYPE(LENGTH) VALUE

Example:

01 EMPLOYEES-COUNT PIC 9(3) VALUE 100

LEVEL_NUMBER 

The level number is used to specify the hierarchy level of the data items in COBOL. The level number starts from 01 to 49 and there are some special level numbers they are 66, 77, and 88. The level number can be of two types. They are Group items and Elementary items.

  • Elementary items: Level number, Variable name, Picture clause, and Value clause (this is optional) are used to declare an elementary item. It can be grouped together to form the group item,
  • Group items: Level number, Variable name, and Value clause (this is optional) are used to declare a group item. The group level number is always 01. It consists of one or more elementary items.
    • Level number 01 specifies record description entry and it always begins in Margin A and continues to Margin B in the coding sheet.
    • Level number 02-49 is used to describe group and elementary elements. It can either begin with Margin A or Margin B.
    • Level number 66 is used to rename clause items. It can either begin with Margin A or Margin B.
    • Level number 77 is used when items can’t be subdivided. It can either begin with Margin A or Margin B.
    • Level number 88 is used for condition entry. It can either begin with Margin A or Margin B.

Example:

01 EMPLOYEES is an example of group item.
02 COUNT-EMPLOYEES  PIC 9(2)  
VALUE  '10' is an example of elementary item. 

VARIABLE_NAME 

  • It is also called an identifier or filler.
  • It is defined by the user and the name cannot be a reserved keyword.
  • A variable name can have a minimum of 1 character and a maximum of 30 characters. It can consist of 0 -9, A-Z, or a hyphen.
  • Variable names should not begin or end with hyphens. Other special characters except hyphen are not allowed.
  • There should not be any space involved in the variable name.
  • FILLER names cannot be initialized or used in any of the operations in the procedure.
  • The variable name for the group item must be unique.
  • Variable names for elementary items in different or various groupings can have the same name but we have to use OF qualifier followed by group item to describe it uniquely.

Example: 

TWO-EMPLOYEES, TOTAL-01 are valid variable names. 
ADD, MOVE, FOR, FILLER, FINAL are 
reserved keywords and these are invalid variable names. 
#123 is not a valid variable name 
because it contains # special character.
-EMPLOYEE1 is also not a valid variable 
name because it starts with hyphen.

PICTURE CLAUSE

  • It is used to describe the characteristics of the variable/data item.
  • It is used only with the elementary data item and it cannot be used with the group data item.
  • It consists of 4 parts. They are data type, sign, decimal point, and length. These are the characteristics of the PICTURE CLAUSE.
  • The data type is nothing but declaring what type of data is going to be stored in the memory. In COBOL, the data types can be numeric(only digits), alphabetic(only alphabetic), or alphanumeric(contains digits, alphabetic and special characters).
  • The sign is used with numeric type and it can be + or -.
  • The decimal point is used with numeric type and it is used to specify the decimal position.
  • Length is nothing but specifying the number of bytes needed by the data item.
Declaration Description
PIC 9(LENGTH) Declaration for Numeric Type
PIC A(LENGTH) Declaration for Alphabetic Type
PIC X(LENGTH) Declaration for Alphanumeric Type
PIC S(LENGTH) Declaration for Sign
PIC V(LENGTH) Declaration for Implicit Decimal Type
PIC P(LENGTH) Declaration for Assumed Decimal Type

Example:

01 A1 PIC A(5) VALUE 'GEEKS'
01 A2 PIC X(5) VALUE 'A1B2C'
01 A3 PIC 9(2) VALUE 12
01 A4 PIC S9(2)V9(2) VALUE -22.33

VALUE CLAUSE

  • The value clause is an optional clause.
  • It can be used with both group data items and elementary data items.
  • For initializing the variable, we have to specify the term VALUE in the declaration statement and specify the values.
  • It consists of numeric, alphabetic, and alphanumeric values.
  • For alphabetic and alphanumeric types, declare the values inside the single quote ‘ ‘.
  • For numeric type, don’t include a single quote and specify it without using a single quote.

Example:

01 GFG PIC 9(3) VALUE 101
01 GFG PIC A(3) VALUE 'GFG'

Example with Code :

In the below coding, we are declaring and initializing the variable in the header section. To declare and initialize the variables include the headers Data, File, and Working-Storage division. In the Procedure Division, simply print the values using the DISPLAY keyword. ‘.’ in each statement must be included in all the lines to indicate the end of the statement. STOP RUN statement should be given at the end of the PROCEDURE DIVISION. 

Example 1:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. HELLOWORD.
    DATA DIVISION.
    FILE SECTION.
    WORKING-STORAGE SECTION
       01 A1 PIC 9(3) VALUE 100.
       01 A2 PIC A(3) VALUE 'GFG'.
       01 A3 PIC S9(2)V9(2) VALUE -22.33
     PROCEDURE DIVISION.  
     DISPLAY A1.
     DISPLAY A2.
     DISPLAY A3.
STOP RUN.      


Output :

100
GFG
-22.33


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads