Data Item (Variable) Declaration
COBOL variable declaration provides a structured, descriptive way to ensure clarity and accuracy. It is essential to declare the variables with adequate lengths, especially when dealing with data from external sources.
The variable declaration is as follows -
level-number data-item|variable-name
PIC data-type-character(length)
[VALUE literal-value] [additional-clauses].
For example -
Level Number
The level number specifies the hierarchy of data items or variables. Level number is a one or two-digit numeric value. The valid level numbers are - 01 to 49, 66, 77 and 88.
Level numbers are two types based on their usage -
- General purpose level numbers (01 to 49) - General purpose level numbers are used for regular variables declaration. The level numbers hierarchy should be in ascending order from lower number to higher number. i.e., 01 is the highest level number, and 49 is the lowest level number.
- Special purpose level numbers (66, 77 and 88) - These level numbers are used for special purposes like renaming a variable and declaring individual variables and conditional names.
- 66 level number - It is used to create another logical group by regrouping the elementary variables of a group. The RENAMES clause is used along with it to rename the group.
- 77 level number - It is used to declare the individual variables. It's not part of any hierarchical structure and doesn't subordinate other variables.
- 88 level number - It defines a condition name for a specific value or a set of values under the variable. It can't declare a variable, but it is used to provide a descriptive name for a condition.
Example -
Scenario - Create a employee record with EMP-ID and EMP-NAME alone.
01 EMPLOYEE-RECORD.
05 EMP-ID PIC 9(5).
88 INVALID-EMP-ID VALUE LOW-VALUES
SPACES.
05 EMP-NAME.
10 FIRST-NAME PIC X(15).
10 MIDDLE-NAME PIC X(10).
10 LAST-NAME PIC X(20).
05 DATE-OF-BIRTH.
10 DOB-YEAR PIC 9(4).
10 DOB-MONTH PIC 9(2).
10 DOB-DAY PIC 9(2).
66 EMPLOYEE-REC01 RENAMES
EMP-ID THRU EMP-NAME.
In the above example, the level numbers are in ascending order. INVALID-EMP-ID is the condition name that is declared with 88 level number. INVALID-EMP-ID is equal to IF EMP-ID EQUAL LOW-VALUES OR SPACES.
Data item | Variable
A variable is a name used to hold the value for processing in the program. It is also called as a data item. It should be declared in the DATA DIVISION of the program. The variables are divided into three types based on their declaration, and those are -
- Individual variable - An individual variable is declared individually but neither under a group variable nor as a group variable. It always has the picture clause during its declaration.
- Group variable - A Group variable is declared without the picture clause and has elementary variables declared under it. It does not have a picture clause.
- Elementary variable - - The elementary variable is a variable that is declared under the group variable. It should always have a PIC clause.
Examples -
Scenario - Different variable types declaration, initialization and display in COBOL program.
----+----1----+----2----+----3----+----4----+----5----+
...
DATA DIVISION.
WORKING-STORAGE SECTION.
* Individual variable
01 LEVEL-1 PIC 9(03) VALUE 256.
* Group variable
01 LEVEL-GROUP.
* Elementary variables
05 LEVEL-21 PIC 9(03) VALUE 256.
05 LEVEL-22 PIC 9(03) VALUE 128.
PICTURE Clause
The PICTURE clause is used to specify the characteristics of the variable while declaring it. i.e., variable type, length, etc. PICTURE clause always codes with the symbol which is a letter used to specify the type of the variable. For example - A, B, E, G, N, P, S, V, X, Z, CR, DB.
Data Types
A data type defines the kind of data a variable can hold, such as numeric values, alphabetic characters, or alphanumeric strings. There are five data types in COBOL and those are -
Data Type | Description |
---|---|
Numeric | Numeric data type allows to declare the variables to store the numeric decimal values.
Numeric values are the combination of 0 to 9 numbers. Declaration Symbol - 9 Allowed Characters - 0 to 9. For Example -
|
Alphabet | Alphabetic data type allows to declare the variables to store the alphabetic strings.
Alphabetic strings are the combination of A to Z or a to z characters. Declaration Symbol - A Allowed Characters - Space + - * / = $ Comma(,) ; Decimal point(.) " ' ( ) > < : _ A-Z, a-z. For Example -
|
Alpha-numeric | Alphanumeric data type allows to declare the variables to store the strings that are combination of alphabets and numbers.
Alphanumeric strings are the combination of A to Z or a to z characters or 0 to 9 numbers. Declaration Symbol - X Allowed Characters - Space + - * / = $ Comma(,) ; Decimal point(.) " ' ( ) > < : _ A-Z, a-z 0-9. For Example -
|
Sign | Sign data type allows to declare the numeric variable with sign to capture the negative values.
Sign can specify for numeric values. i.e. sign data types can come up with numeric data type. Declaration Symbol - S Allowed Characters - - (minus) or + (plus) For Example -
|
Decimal point | When a data comes as a decimal to the program, a variable should declare with decimal point to handle the decimal value. Declaration Symbol - P/V Allowed Characters - . (Dot) For Example -
|
We will discuss more about this topic in the next chapter.
VALUE Clause
The VALUE clause is used to assign an initial value to a variable at the time of declaration. This initialization happens when the program starts executing and before any other operation occurs.
Examples -
Scenario1 - Alphanumeric Initialization.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(15) VALUE 'Mainframes'.
Here, the alphanumeric variable WS-NAME is initialized with the value 'Mainframes'.
Scenario2 - Numeric Initialization.
WORKING-STORAGE SECTION.
01 WS-AGE PIC 9(02) VALUE 25.
Additional Clauses -
Additional clauses may use on a need basis -
- BLANK WHEN ZERO Clause
It is used to define a variable filled with spaces when its value is zero.
It is coded with a numeric variable, or its picture clause is defined as numeric-edited.
Output -WORKING-STORAGE SECTION. 01 WS-VAR. * Declaring a variable with BLANK WHEN ZERO 05 WS-BWZ-VAR PIC ZZ,ZZ9.9(2) BLANK WHEN ZEROES. PROCEDURE DIVISION. MOVE ZEROES TO WS-BWZ-VAR. DISPLAY "WS-BWZ-VAR: " WS-BWZ-VAR. DISPLAY " ". MOVE 20000 TO WS-BWZ-VAR. DISPLAY "WS-BWZ-VAR: " WS-BWZ-VAR.
WS-BWZ-VAR: WS-BWZ-VAR: 20,000.00
- JUSTIFIED | JUST Clause
By default, alphabetic and alphanumeric data is left aligned, and numeric data is right aligned in the COBOL variables.
It is used to override the default alignment of data and aligns it to the right while displaying them.
It applies only to alphabetic and alphanumeric variables but not numeric ones.
Output -WORKING-STORAGE SECTION. 01 WS-VAR PIC X(15). 01 WS-RJE-VAR PIC X(15) JUSTIFIED RIGHT. PROCEDURE DIVISION. MOVE "MAINFRAMES" TO WS-VAR WS-RJE-VAR. DISPLAY "WS-VAR :" WS-VAR ":". DISPLAY "WS-RJE-VAR :" WS-RJE-VAR ":".
WS-VAR :MAINFRAMES : WS-RJE-VAR : MAINFRAMES:
- SYNCHRONIZED Clause
It is used to allocate the variables at their respective natural memory boundaries.
It specifies the alignment of an elementary item on a natural boundary in storage and reduces the slack bytes.
Slack bytes (unused character positions) are added before each synchronized item in the records. For Example -
The compiler inserts the slack bytes after STUDENT-NO to start the next item STUDENT-NAME from the boundary. Similary, for the STUDENT-GRADER to start the STUDENT-CLASS from the boundary. The declaration becomes after slack bytes is -01 STUDENT. 05 STUDENT-NO PIC 9(02). 05 STUDENT-NAME PIC X(12). 05 STUDENT-GRADE PIC 9(02). 05 STUDENT-CLASS PIC X(03).
Total 4 slack bytes inserted if we are not used SYNC clause. To avoid the slack bytes, the layout should be declared with SYNC clause like below -01 STUDENT. 05 STUDENT-NO PIC 9(02). [05 SLACK-BYTES PIC XX. INSERTED BY COMPILER] 05 STUDENT-NAME PIC X(12). 05 STUDENT-GRADE PIC 9(02). [05 SLACK-BYTES PIC XX. INSERTED BY COMPILER] 05 STUDENT-CLASS PIC X(03).
01 STUDENT. 05 STUDENT-NO PIC 9(02). 05 STUDENT-NAME PIC X(12) SYNC. 05 STUDENT-GRADE PIC 9(02). 05 STUDENT-CLASS PIC X(03) SYNC.