Data types in COBOL

Introduction

To define the variables in the program, Data Division is used.

Following terms are used to describe the data in COBOL:
  • Level Number
  • Data Name
  • Picture Clause
  • Value clause
data types cobol

Level Number

  • The level number identifies the level of data in a record.
  • It is used to differentiate between elementary items and grouped items.
Level numberDescription
01Record description entry.
02 to 49Grouped and elementary item
66Rename Clause items
77It cannot be sub-divided
88Condition name entry

Elementary items
  • They cannot be further divided.
  • Level number, Data name, Picture clause and Value clause are used to describe an elementary item.
Grouped items
  • They include one or more elementary item.
  • To describe group items Level number, data name and value clauses are used.

Example

DATA DIVISION.
     WORKING-STORAGE SECTION.
01 WS-NAME PIC X(20).                           -------------> ELEMENTARY ITEM
01 WS-ROLL-NUMBER PIC 9(2) VALUE '23'.  ---------> ELEMENTARY ITEM

01 WS-EMP-ADDRESS.                     -------------> GROUPED ITEM
     05 WS-EMP-NAME   PIC X(30).     -------------> ELEMENTARY ITEM
     05 WS-EMP-ID         PIC 9(4).        -------------> ELEMENTARY ITEM
     05 WS-EMP-DEPT    PIC X(30).       -------------> ELEMENTARY ITEM

Data Name

  • Before using the data names in the procedure division it must be defined in the Data Division.
  • Data Names must be user defined names.
  • Precaution should be taken that it should not be a reserved word.
  • Reference is given by the data name to the memory locations where the actual data is stored.
  • Data name is either elementary or group type.
For example,

Valid – EMP-NAME , S200, 10G, etc.
Invalid – MOVE, COMPUTE, (both are reserved word) , 10+c (+ is not allowed) , 500 (No alphabet).

Picture Clause

Following terms are used to define Picture Clause:

1. Data type
  • It can be numeric, alphabetic or alphanumeric.
  • Numeric type includes 0 to 9.
  • Letters A to Z include in alphabetic type.
  • Alphanumeric type includes digits, letters and special characters.
2. Sign
It can be used with the numeric data. (+, -, etc.)

3. Decimal point position
It can be used with numeric data.

4. Length
It defines the number of bytes used by the data item.

Following are the symbols used in picture clause:

9 (Numeric), A (Alphabetic), X (Alphanumeric), V (Implicit Decimal), S (sign), P (Assumed decimal).

Example : Demonstration of PIC clause

IDENTIFICATION DIVISION.
PROGRAM-ID. PI.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC S9(3)V9(2).
   01 WS-NUM2 PIC P9.
   01 WS-NUM3 PIC S9(3)V9(3) VALUE -325.567.
   01 WS-NAME PIC A(12) VALUE 'TUTORIALRIDE'.
   01 WS-ID PIC X(7) VALUE 'AT2705$'.

PROCEDURE DIVISION.
   DISPLAY "WS-NUM1 : "WS-NUM1.
   DISPLAY "WS-NUM2 : "WS-NUM2.
   DISPLAY "WS-NUM3 : "WS-NUM3.
   DISPLAY "WS-NAME : "WS-NAME.
   DISPLAY "WS-ID : "WS-ID.
STOP RUN.


Output:
WS-NUM1 : +000.00
WS-NUM2 : .00
WS-NUM3 : -325.567
WS-NAME : TUTORIALRIDE
WS-ID : AT2705$

Value Clause

  • Value clause is used to initialize the data items. It is an optional clause.
  • The values are alphanumeric literal, numeric literal or figurative constant.
  • It is used with both items i.e grouped and elementary items.

Example : Demonstration of value clause

IDENTIFICATION DIVISION.
PROGRAM-ID. VAL.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 999V99 VALUE IS 2.7.
   01 WS-NAME PIC A(5) VALUE 'XYZ'.
   01 WS-ID PIC 999 VALUE ZERO.
   01 WS-ID1 PIC 999 VALUE QUOTES.

PROCEDURE DIVISION.
   DISPLAY "WS-NUM1 : "WS-NUM1.
   DISPLAY "WS-NAME : "WS-NAME.
   DISPLAY "WS-ID   : "WS-ID.
   DISPLAY "WS-ID1   : "WS-ID1.
STOP RUN.


Output:
WS-NUM1 : 002.70
WS-NAME : XYZ  
WS-ID   : 000
WS-ID1   : " " "