In old COBOL, variables are called data items.
A COBOL variable is a data definition which defines:
storage unit
length
format
sign-handling
place of decimals
inner representation (USAGE)
logical values (88-level)
These definitions are within the DATA DIVISION.
A COBOL variable has three plus two parts:
level number (01, 05, 10, 88 etc., or 1, 2, 3, 88 etc.)
identifier (name)
picture clause (PIC; the form)
usage clause (USAGE; the inner storage type; optional)
value clause (initial value; optional)
Level numbers define structure:
01 (top-level variable or record)
05, 10, 15, ... (sub-fields, record-structure)
66 (reserved for RENAMES data structures)
77 (simple, individual field, not part of structures, often used in older COBOL, rarely in modern COBOL)
88 (condition-name, logical value)
In theory, the sub-levels can be marked as 02..49. Traditionally, 05, 10, 15, 20, ... are used.
In modern COBOL, next to the classic level numbers, 1, 2, 3, 4, ... numbers are valid as well. The 66, 77, 88 level numbers always are accepted.
EXAMPLE FOR LEVEL NUMBERS AND STRUCTURE:
The PIC defines:
what type of data is stored
in what length
in what format
Special picture symbols:
S (sign)
V (implicit decimal places)
P (assumed decimal scaling)
X (any character)
A (letter)
9 (number)
Z (placeholder mask)
Syntax versions:
[S]SYMBOL[(length)[V9...]]
[S]SYMBOL[(length)[V9(length)]]
[S]SYMBOL[(length)[.9...]]
[S]SYMBOL[(length)[.9(length)]]
In case of A and X variable:
if the stored value does not fill the defined length, the rest of characters to the right will be filled with spaces (equals with blanks)
In case of A, X and 9, defining of the length:
old COBOL (the number of As or Xs or 9s shows the length of the variable)
modern COBOL (in brackets is the length, e.g. A(3), X(2), 9(4))
In case of 9:
without defining the V, the stored value will be a whole number
when the V is defined, the decimal will be implied (COBOL use decimals for calculations but forgets the decimal sign upon displaying the value)
when the . is defined, the decimal will be actual (displaying the value always will show the decimal sign upon displaying the value)
for decimals, in old COBOL the number of 9s are shows the length of the decimal part
for decimals, in modern COBOL the number of 9s are in brackets
in case of DISPLAY, the number is stored as series of characters with numeric checking applied by COBOL
in case of DISPLAY, if the stored value does not fill the defined length, the rest of number places will be filled with zeros to the left
In case of negative 9, the - sign is an extra over the length of the numeric variable:
in case of DISPLAY, extra character
in case of COMP/COMP-3, stored differently
The maskholder Z marks a position which will be SPACE in case the number does not reach the marked position with its numbers, e.g.:
ZZ9 (number which will be shown as "Â n") where n is the number
find details in the FORMATTED NUMBERS TO DISPLAY section
EXAMPLE FOR PIC CLAUSES:
EXAMPLE FOR DIFFERENCES BETWEEN V AND . AND Z:
This defines how the field will be stored in memory:
DISPLAY (default, character-based)
COMP/BINARY (binary integer, computer-based)
COMP-3 (packed decimal or BCD, obligatory for financial data, 4-byte)
COMP-1 (floating-point, 4-byte)
COMP-2 (floating-point, 8-byte)
EXAMPLE FOR USAGE CLAUSES:
This defines the initial value of a variable upon definition. Optional.
If the given value does not fit into the size of the variable, truncating will be executed.
EXAMPLE FOR VALUE CLAUSES:
Variables are static and have active memory allocation upon definition since their sizes are coming with their definitions, due to optimalization of COBOL for rapid and predictable execution.
In case the initial value is not defined upon definition with the VALUE clause, the variable stays uninitialized, and the value is called undefined, until a value is given to the variable, or the INITIALIZE statement is used.
The INITIALIZE is used in the PROCEDURE DIVISION, and typically:
at program start to clean the WORKING-STORAGE
before re-using a record buffet
before populating a structure for output
before calling a sub-program to avoid garbage in LINKAGE
The initialization will render defaults to variables:
A, X (filled with spaces)
9 DISPLAY (filled with "0" characters without floating point)
9 COMP/BINARY (set to binary zero, 00)
9 COMP-1 (+0.0)
9 COMP-2 (+0.0)
9 COMP-3 (set to packed zero with positive sign)
INITIALIZATION works recursively: if executed on a record, will go through its elements and so on.
Do not use it when in a data structure only a few values should be given, use MOVE instead.
EXAMPLE FOR USING INITIALIZE:
The value rendered to a field name is mapped in a definition block:
define the level, the name, the picture symbol
then in 88 lines key-value pairs are given
Within the PROCEDURE DIVISION referencing to the key will point to the corresponding value(s).
EXAMPLE FOR CONDITION NAMES:
Based on the example:
if a field equals OK, means the value is 0
if a field equals ERROR, means the value is 1, 2 or 3
Reading:
if STATUS is 0, OK is true, ERROR is false
if STATUS is 1 or 2 or 3, OK is false and ERROR is true
Instead of numeric literals, strings can be rendered to keys as well. For storing boolean value under COBOL the symbol is X or A, and the values:
"T" for true and
"F" for false
See COBOL LITERALS and COBOL DATA STRUCTURES for more on the topic.
When a variable is given to a DISPLAY statement, it will use all the stored numbers or characters of the variable. In case of numbers, with all the trailing zeroes before the value.
There are solutions to display formatted versions:
type Z (a string in which the numbers will be stored but without the trailing zeroes)
STRING operator with the DELIMITED BY SIZE clause(see the COBOL OPERATORS page)
FUNCTION NUMVAL, NUMVAL-C (see the COBOL FUNCTIONS page)
EDITED field (often used in bank systems, classic COBOL)
EXAMPLE FOR USAGE OF TYPE Z, STRING, FUNCTION NUMVAL, EDITED FIELD:
These are built-in variables:
RETURN-CODE
TALLY
CURRENT-DATE
LENGTH OF identifier
ADDRESS OF identifier