Updated March 23, 2023
Introduction to COBOL Data Types
Like any other programming language Cobol also has its own set of data types. All these data types are declared in the data division section of the program. Every variable declaration in the data division section must be of the below format,
Syntax:
Level-number data-name picture-clause value-clause
Example:
01 Initial-value PIC 9(2) VALUE '01'.
01 WS-Employee-id PIC 9(002) VALUE ZERO.
01 WS-Employee-name PIC X(009) VALUE SPACE.
Level Numbers
The level in which the data record is situated is determined using level numbers in COBOL. They play an important role in Cobol variable declaration. The classifications of level numbers are as below,
- Individual Data Items: These are Level numbers from 01 to 49. They do not have any sub-items.
- Group Data Items: Group data items consist of more than one individual data item within it.
- Elementary Data Items: The elementary data items cannot be divided further.
66 – Rename Clause items
77 – Non divided elementary items
88 – Conditional entries
Picture Clause
The major element in a picture clause is the data type being used.
Example:
01 WS-ONE PIC 9(2) VALUE '01'.
Then the sign is used to depict the sign of the data being used.
Example:
01 WS-ONE PIC S9(2) VALUE '01'.
The precession point in numeric data is represented by means if these decimal points. Here the represented position is the position of the decimal point which is being used.
Example:
01 WS-ONE PIC 9(2)V9(2) VALUE '01'.
The length specifies the overall byte values which is getting stored in this variable
Example:
01 WS-ONE PIC 9(2) VALUE '01'.
Data Types in COBOL
The data types in Cobol are classified into three types which are as follows:
1. Numeric
The numeric data types are used to represent numeric values (0-9) in Cobol programming.
Cobol:
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AA.
02 VARIABLE-A PIC 9(2) VALUE 10.
02 VARIABLE-B PIC 9(2) VALUE 20.
02 OUT-VARIABLE PIC 9(4).
PROCEDURE DIVISION.
P1.
DISPLAY 'PROGRAM TO COMPUTE SUM OF TWO NUMBERS'
DISPLAY ' VALUE A : ' , VARIABLE-A
DISPLAY ' VALUE B : ' , VARIABLE-B
COMPUTE OUT-VARIABLE = VARIABLE-A + VARIABLE-B.
DISPLAY "SUM VALUE : ", OUT-VARIABLE.
STOP RUN.
JCL:
//TESTJOB JOB(TESTJCL,XXXXXX),CLASS = W , MSGCLASS = E//STEP1 EXEC PGM = ADDITION
Output:
Explanation:
Here all the variables declared are placed in the data division area specifically at the working storage section. The job control language used for the program execution, The JCL for executing this program is also shared. Submitting the job control statement to the JES system executes the job and displays the output in the console. The below Cobol program receives input from two numeric variable VARIABLE-A and VARIABLE-B, the received input is added using compute function. The output of the compute function is again stored into a Cobol numeric variable namely OUT-VARIABLE. Here all the variables being created are of numeric type. The display statement is used for displaying the generated output in the terminal.
2. Alphabetic
The alphabetic type variable can hold only alphabetic variables within A-Z.
Cobol:
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AA.
02 VARIABLE-A PIC A(5) VALUE 'HELLO'.
02 VARIABLE-B PIC A(5) VALUE 'WORLD'.
02 OUT-VARIABLE PIC A(10).
PROCEDURE DIVISION.
P1.
DISPLAY 'PROGRAM TO CONCATENATE TWO STRINGS'
STRING VARIABLE-A DELIMITED BY SPACE
VARIABLE-B DELIMITED BY SPACE
INTO OUT-VARIABLE
DISPLAY "CONCATENATED VALUE : ", OUT-VARIABLE.
STOP RUN.
JCL:
//TESTJOB JOB(TESTJCL,XXXXXX),CLASS = W , MSGCLASS = E//STEP1 EXEC PGM = HELLO
Output:
Explanation:
Here all the variables declared are placed in the data division area specifically at the working storage section. The job control language used for the program execution, The JCL for executing this program is also shared. Submitting the job control statement to the JES system executes the job and displays the output in the console. The below Cobol program receives string input from two numeric variable VARIABLE-A and VARIABLE-B, the received input is concatenated using string function. The output of the string function is again stored into a Cobol alphabetic variable namely OUT-VARIABLE. Here all the variables being created are of an alphabetic type. The display statement is used for displaying the generated output in the terminal.
3. Alphanumeric
An alphanumeric variable can hold both alphabetic and numeric values together within it. It can hold below possible characters ( 0 – 9 ), (A – Z).
Cobol:
IDENTIFICATION DIVISION.
PROGRAM-ID. AAA.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AA.
02 VARIABLE-A PIC X(5) VALUE 'HELLO'.
02 VARIABLE-B PIC X(6) VALUE 'WORLD,'.
02 VARIABLE-C PIC X(8) VALUE '2000-ERA'.
02 OUT-VARIABLE PIC X(20).
PROCEDURE DIVISION.
P1.
DISPLAY 'PROGRAM TO DISPLAY ALPHANUMERIC USAGE'
STRING VARIABLE-A DELIMITED BY SPACE
VARIABLE-B DELIMITED BY SPACE
VARIABLE-C DELIMITED BY SPACE
INTO OUT-VARIABLE
DISPLAY "APLPHANUMERIC OUTPUT : ", OUT-VARIABLE.
STOP RUN.
JCL:
//TESTJOB JOB(TESTJCL,XXXXXX),CLASS = W , MSGCLASS = E//STEP1 EXEC PGM = HELLO
Output:
Explanation:
Here all the variables declared are placed in the data division area specifically at the working storage section. The job control language used for the program execution, The JCL for executing this program is also shared. submitting the job control statement to the JES system executes the job and displays the output in the console.
The Cobol program receives string input from three alphanumeric variable VARIABLE-A, VARIABLE-B and VARIABLE-A the received input is concatenated using string function. The output of the string function is again stored into a Cobol alphanumeric variable namely OUT-VARIABLE. Here all the variables being created are of an alphabetic type. The display statement is used for displaying the generated output in the terminal.
Recommended Articles
This is a guide to COBOL Data Types. Here we discuss the introduction and different data types in Cobol along with implementation. You may also look at the following articles to learn more –