Open In App

COBOL – Data Types

Last Updated : 05 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A Datatype is a classification by the programmer to tell the compiler/interpreter how data will be used inside a program. For example, the roll number of the student defined as the number will take input as a number only if other values are supplied instead of the number it will raise an abend inside the program.

Symbol Short Description

Description

Value example  
9 Numeric Include Digits 0 to 9 Phone_number: 9898989898
A Alphabetic Include only Letter A to A/a-z Name:   GeekForGeeks
X Alphanumeric Include both digits and letters Gift_voucher:  ABZ445
S Signed Include integers value  Balance:  -458 
P Assumed Decimal Used to find value on the left or right side of the decimal Assumed_dec:  

Example Program: We will be using this program to explain the concepts of this article.

Cobol




IDENTIFICATION DIVISION.
       PROGRAM-ID. YOUR-PROGRAM-NAME.
       DATA DIVISION.
       FILE SECTION.
       WORKING-STORAGE SECTION.
        
           01 GROUP01.
            02 PHONE_NUMBER            PIC 9(10) VALUE 7845955477.
            02 ST_NAME                 PIC A(20) VALUE 'GeekForGeeks'.
             
           01 GIFTVOUCHER              PIC X(6) VALUE 'ABZ445'.
           01 BALANCE                  PIC S9(3) VALUE -458.
           01 ASSUMED_DEC              PIC P9(2) VALUE 23.458.
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
            DISPLAY GROUP01
            DISPLAY GIFTVOUCHER
            DISPLAY BALANCE
            DISPLAY ASSUMED_DEC
             
            STOP RUN.


Explanation: To understand the concept of Datatype, we need to know about the basic term used.

  •  Data Name
  •  Level Number
  •  Picture Clause
  •  Value Clause

1. Data Name

A Data name is like a User-defined variable used in the program which will be used to hold different values in it and must contain only digits(0-9), letters(A-Z), minus signs, and Hyphens(-), a data name cannot use reserved words such as MOVE, COMPUTE.

Some Valid data names:

 PHONE_NUMBER
 ST_NAME
 WS-POS1
 BOOK
 
Invalid data names:
 
MOVE          : it is reserved keyword
COMPUTE          : it is reserved keyword    
$VAR          : $ char not allowed
100              : only number not allowed

2. Level Number

A level number is a one-digit or two-digit integer between 01 and 49, or one of three special level numbers: 66, 77, or 88. The following level numbers are used to structure records:

  • Group Item: A group item consists of one or more elementary items, in the below example GROUP01 is a group item.
  • Elementary item: It is an individual defined item, in the above example PHONE_NUMBER is an elementary item.
Level Number Description Type
01 Record Description or Title for Group General Level Number
02 to 49 For Group/Elementary items
66 Rename Clause Items Special Level Number
77 Fixed cannot be subdivided to declare an elementary item
88 Condition name entry (mainly used for flag purposes)

Example:

DATA DIVISION.
WORKING-STORAGE SECTION.


   01 GROUP01.                                                        /*GROUP ELEMENT*/ 
   02 PHONE_NUMBER            PIC 9(10) VALUE 7845955477.            /*ELEMENTARY ELEMENT*/
   02 ST_NAME                 PIC A(20) VALUE 'GeekForGeeks'.        /*ELEMENTARY ELEMENT*/
   02 GIFTVOUCHER              PIC X(6) VALUE 'ABZ445'.
   02 BALANCE                  PIC S9(3) VALUE 458.
   
  66 WS-VAR2 RENAMES PHONE_NUMBER THROUGH ST_NAMES                   /*RENAME ELEMENT*/   
  
  77  ASSUMED_DEC              PIC P9(2) VALUE 23.458.                      /*INDEPENDENT ELEMENT*/
  
   01 WS-GENDER                 PIC X(01).                             /*CONDITIONAL ELEMENT*/
   88 WS-MALE                   VALUE "M". 
   88 WS-FEMALE                   VALUE "F".

3. Picture Clause

In the above code different datatype variables such as PHONE_NUMBER, ST_NAME, GIFT VOUCHER  are defined with the help of the Picture clause also known as PIC there are 5 symbols(9, A, X, S, P) which can be used with the help of picture clause which is already explained.

Example:

02 PHONE_NUMBER            PIC 9(10) VALUE 7845955477.        /*the PHONE_NUMBER is initialised as a numberic value with the help of picture clause using symbol 9 which can hold 10 digits*/

02 ST_NAME                 PIC A(20) VALUE ‘GeekForGeeks’.  /*the ST_NAME is initialised as a Alphabetic value with the help of picture clause using symbol A which can hold 20 characters*/

4. Value Clause 

It is used to initialize the data item example in the above code PHONE_NUMBER is having a default value of 785955477, defined with the help of the value clause.it is optional to use the value clause.

When we compile and execute the above code, it will display the values defined using the Values clause

Example:

02 PHONE_NUMBER            PIC 9(10) VALUE 7845955477. /* PHONE_NUMBER is holding default value 7845955477 which will be displayed if not other values is assigned */

02 ST_NAME                 PIC A(20) VALUE ‘GeekForGeeks’. /* ST_NAME is holding default string value “GeekForGeeks” defined with keyword VALUE*/



Similar Reads

COBOL - Installing COBOL
There are three things that interested software developers always need when learning a new programming language: a desire to learn, a few good books on said programming language as well as a decent IDE to master the basics. If you’re a Computer Science student who has had to program in both Java and Python, you’ll know that obtaining good resources
5 min read
Data Item Declaration in COBOL
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,
5 min read
Data Type Justification in COBOL
Data Classification/Types helps the system identify how the programmer wants to use the data in any particular program. In COBOL we have three different Classifications of DATA TYPES, which are Numeric, Alphabetic, and Alpha-Numeric data types. In COBOL DATA TYPE justification applies to Numeric data type, Alpha-numeric data type, and Alphabetic da
4 min read
Data Layout in COBOL
COBOL is a programming language that was developed in the 1950s for business and financial applications. In COBOL, the data layout is the arrangement of data items in a program. It specifies how the data is organized and how it is accessed. COBOL programs are organized into four divisions: the identification division, the environment division, the
8 min read
Display Computation in COBOL
DISPLAY is the most common form of internal data representation. DISPLAY stores in decimal form. Each character of the data will represent one byte of storage. If there is no usage clause for data items, then by default it will come under DISPLAY. DISPLAY can be used for all types namely Numeric data types, Alphabetic data types, and Alpha-Numeric
2 min read
COMP-2 in COBOL
In COMP Usage data will get stored in the complete binary form. Based on the usage the data will get stored either in HALF-WORD or FULL-WORD. HALF-WORD which is nothing but 2-bytes data and FULL-WORD is nothing but 4 bytes of data. When comes COMP-2, is similar to COMP-1. In COMP-1 data will get stored in one word in the floating point form whereas
2 min read
Program Structure of COBOL
COBOL is a programming language that was developed to solve business problems. COBOL stands for Common Business Oriented Language. Being a High-Level Structured Language, COBOL is very similar to English-like language, which is used to develop major business applications. Due to its easier maintenance, ability to work with any database (DB2, VSAM),
2 min read
Database Interface in COBOL
Programs in COBOL communicate with the DB2 Database. Data Base2 is DB2, and it was created by IBM. The database is a relational one. The relational information is kept in the TABLE format, which consists of multiples of rows and attributes (Columns). DB2 is generally used for storing sizable amounts of Mainframe application data. It is comparable t
4 min read
Difference Between Search and Search All in COBOL
There are two ways in which we can perform the searching operations in COBOL, first using the traditional method i.e. by applying the loop with the help of PERFORM statement, or by using the predefined verbs: SEARCH and SEARCH ALL. SEARCH and SEARCH ALL verbs can be used only in the INDEXED FILES because they use the indexed variable associated wit
3 min read
Difference Between Sequential, Indexed, and Relative Files in COBOL
Files are the collection of records related to a particular entity. Through file handling, we can store these records in an organized order. These records are stored either on magnetic tape or on a hard disk. The files are further classified into 3 types: Sequential file organization.Relative file organization.Indexed file organization.Advantages o
5 min read
Article Tags :