COBOL – Data Types
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: GeekForBeginner |
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 'GeekForBeginner' . 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 'GeekForBeginner'. /*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 ‘GeekForBeginner’. /*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 ‘GeekForBeginner’. /* ST_NAME is holding default string value “GeekForBeginner” defined with keyword VALUE*/
Contact Us