by | Nov 24, 2020 | COBOL | 0 comments
COBOL Data Items And Declaration
Data types are an attribute of data variables which tell the processer or interpreter how to and where to use these data items. Most commonly used data types are integer, float, char, Boolean etc.
Similarly, COBOL also contains these data types, but COBOL data types are divided amongst 5 sections, which are given below:


EXAMPLE =>
As stated earlier, COBOL is a programming language similar to English. We don’t have to learn a complicated syntax to perform COBOL commands.
Level numbers are mainly used to distinguish between whether the created item is an elementary item or grouped item, i.e., it is used to identify a data record level.
So, basically level numbers are mainly used in declaration of variables in COBOL programming. COBOL provides us with level numbers varying from 01 to 49 and some special case level numbers are 66 , 77 and 88.
These level numbers are also used to define variable hierarchy.
They are majorly divided amongst 2 type of level numbers
GENERAL PURPOSE LEVEL NUMBERS =>
These are the level numbers that are used to define variables in COBOL programs during the execution, they consist of hierarchy starting from lower term 01 and further increases till maximum number to 49.
01 – 49 they are divided in 3 categories
Individual data items: As the name suggests, are stand-alone data items. These are used to declare only one variable. These are defined with a picture clause, but do not have any sub –items. They must be uniquely declared amongst the called and the calling program. These items lie between 01 to 49.
EXAMPLE =>
LEVEL – 1
Group items: As stated above, group elements are nothing but a collection of elementary elements, these data items do not contain a picture clause and the name should always be unique.
Elementary items: These data items are defined under the group data item, i.e., they are the sub data item to the main group data item. These can be similar to other elementary items defined under different group data item. To define an elementary element, we have to add the following details, and these elementary items cannot be divided further. The data name and Picture clause are used to define an elementary item. It lies between 02 to 49.
SAMPLE PROGRAM
This is a demo program to learn and understand various types of data items through examples.


THE OUTPUT WILL BE:
In this program, we have used and explained all DATA ITEMS supported by COBOL with example.
SPECIAL PURPOSE LEVEL NUMBERS =>
These level numbers are used in specific location and specific time like rename, condition and individual data item names.
66 level number for RENAME clause – The rename is basically used to regroup elementary data items in group data items.
SYNTAX =>
Here,
data – name – 1 denotes name of group
data – name – 2 denotes starting of elementary data items in group
data – name – 3 denotes ending of elementary data items in group.
NOTES:- 66 level must not include a picture clause and RENAME should be added at the end of the group data item
EXAMPLE =>
Here, in the above example, we have declared a group data item with name MAIN_GROUP and declared 10 elementary data items from ITEM-A1 to ITEM-A10 that will be going to store in the continuous memory allocated in the database. Later using 66 level, we have defined a secondary group naming SECOND_GROUP with the elementary items FROM ITEM-A1 to ITEM-A5
Here SECOND_GROUP is just for the renaming variable that will store the data in the same location allocated during the MAIN_GROUP
It is only used for individual’s data items or elementary items. It cannot be subdivided; therefore, we should avoid using it.
SYNTAX =>
DEFINEMENT OF LEVELS IN COBOL
item or group item, and there cannot be more than one 01 level
EXAMPLE:- 01 EMP-ID PIC 9(5)
This is the individual or stand-alone variable, and it does not contain any sub-item below. The same can be used for Group data items, but this data item does not contain a picture clause. This item contains elementary items.
EXAMPLE:-
EXAMPLE
EXAMPLE-
Before using the program’s data name, we have to define it in the Data division and later by using the same in the procedure division.
While defining the data name in the procedural division, it allows a name to the data cell’s memory location where the actual data will be stored. The data name must be user-defined (given by the user itself), and reserved keywords cannot be used.
FEW EXAMPLES of valid and invalid data names
VALID DATA NAMES
INVALID DATA NAMES
1)   Variables
2)   Literals / constants
3)   Figurative / predefined constants
1) Variables
Variables are set of data consisting of information, which tells the program what to do and what data it will be using in the program.
These values are not fixed and can be changed upon certain conditions.
In COBOL, variables are encrypted in the Data division section, and these are declared in terms of their type and size.
In simple words, variables are identifiers that hold value and identify a particular memory location and can be of a maximum length of 30 characters. Variable must contain numbers (0-9)and digits(A-Z), and hyphens, It must not include a space or any of the reserved keywords. (REPLACE, INSPECT, TALLYING, ETC).
Essential tips for writing variables:-
2 ) Literals / Constants :-
As the name suggest literals are mainly data items that consists of fixed value of data in it, and is declared at the time of declaration.
Types of LITERALS in COBOL


3 ) Figurative / predefined constants :-
predefined or built-in constants in COBOL is called figurative constants.
1) ZERO or ZEROS or ZEROES
2) SPACE or SPACES
3) HIGH-VALUE or HIGHEST-VALUES-highest ordinal position
4) LOW-VALUE or LOW-VALUES-lowest ordinal position
5) QUOTE or QUOTES – quotation(“) or apostrophe(‘)
6) ALL
7) NULL or NULLS
SAMPLE PROGRAM
THIS IS A DEMO PROGRAM TO LEARN AND UNDERSTAND VARIOUS TYPES OF DATA NAMES THROUGH EXAMPLES.
THE OUT PUT WILL BE:
In this program, we have used and explained all DATA NAMES supported by COBOL with example. Here LITERAL defines literal constant data item. FIG – CONST defines figurative constant and VARIABLE denotes variable.
3) PICTURE CLAUSE
Sign: signs like “+” and “-” can only be used with numeric data.
Decimal point position: Assumed a decimal position that is not located in the data and can only be used numerically.
The length defines the Number of bytes used by the data item.
For example:
“9” Numeric this holds actual memory space [ PIC 9(2) ]
“A” Alphabetic 
“X” Alphanumeric
“V” Implicit Decimal It does not hold any memory space. It is used for computation rather than used for display.
[PIC 9(4)V99], if the value of this variable is defined as 123456, then it will be stored like 1234.56 but only stored, not displayed. It will only be used in computation; this part remains hidden.
“.” Actual decimal point only used for display, not for any calculation or Mathematical operations.
[ PIC 9(4).99 ] if the value is 123456, it will be displayed as 1234.56, but it will not be used anywhere for compilation.
“S” Sign data type- it provides a sign to a number. It represent the number is signed and if not, the number is unsigned.
“+” plus sign used to print (+) as a sign.
“-“ minus sign used to print (-) as a sign.
“P” Assumed Decimal
“Z” – only used to hide leading zeroes in the decimal and has nothing to do with non-zeroes
[PIC ZZ99.99] if the value of this variable is 0012.34, then it will be bb12.34
“COMMA” – to insert a comma at a particular position in a data item
“$”-Dollar symbol- used to denote currency. It is used at the beginning of the command.
SAMPLE PROGRAM
THIS IS A DEMO PROGRAM TO LEARN AND UNDERSTAND VARIOUS TYPES OF DATA TYPES THROUGH EXAMPLES.
THE OUTPUT WILL BE:
In this program, we have used and explained all data types supported by COBOL with example.
4)VALUE CLAUSE
Value clause can be used both with an elementary item as well as a group item.
These clauses are used to activate the data item, and its value can be numeric literal, alphanumeric literal, and figurative constant and most importantly, these clauses are not mandatory to use. Program can be initialized even without a value clause.
SAMPLE PROGRAM 
This is a demo program to learn and understand the concept of variables, literals, and figurative constants
THE OUTPUT WILL BE:
Here in this program, we have defined various data names and assigned them with numeric, non-numeric and alphanumeric texts.
Sample PROGRAM 
This is a demo program to learn and understand the concept of variables, literals, and figurative constants
THE OUT PUT WILL BE:
Here in this program, we have defined various data names and assigned them with numeric, non-numeric, and alphanumeric texts.
DATA TYPES
Data types are mainly used to describe the characteristics of a data item defined in a program.
WORDS / ALPHABETIC :- These words consist of user-defined keywords and reserved words known as predefined keywords.
They consist of 31 characters and also include letters, digits, hyphens, and underscores.
NUMERIC LITERAL-Maximum length of 18 characters includes digits(0-9) or only one sign (either +or -)and only one decimal.
example – 123.
NON-NUMERIC(alphanumeric)LITERAL-Maximum length of 160 characters, and must start and end with a quote symbol.
example-‘I is an example of non-numeric literal.’
‘123’
SIGN :- sign data type is nothing but declaring sign value to numeric literal , we use S to declare sign data type
STRING :- String is a literal type representing a string or group of characters as a single source code within the program.
Example – ” HELLO ! “
SEPARATORS :– Separators consist of commas, semicolon, colon, and space, followed by space.
By using all the above literals together, we can only make a computer program to compile.
CODE FORMAT in COBOL
Before the 2002 COBOL update, COBOL programming was restricted under specific guidelines. It consisted of fixed/default format, in which codes should fit in a particular limited area and later updates COBOL introduced a free format in which code can be placed in any line or column, and comments can be placed in any line anywhere using”*>” irrespective of the coding area.
Fixed coding format
Sequence number area
1 – 6 columns
The compiler ignores this area, but it is mainly used for line/card numbers.
This area assigns sorting to manually punched cards, and it ensures program code to run after manual editing.
Indicator area 
7- columns
This area is kept for commenting to make program codes easily understandable by a different user.
Some of the characters generally used here
Comment line character to start a comment “* – “.
Comment line character that enables us to print on a new source listing page “/ – “.
Continuation line, which enables words or literals from the previous line to continue “- –”,
this line enables only in debugging “D –”.
Area A 
8 – 11 columns
This is the main coding area and consists of all the defined COBOL structures, i.e., [DIVISION, SECTION, STATEMENT, CHARACTERS ETC].
In this area, the only main program compiles and executes.
Area B 
12 – 72 columns
All those codes that are not allowed in area A are command and executed in this section.
data types
To define variables in COBOL, one has to use data division and must understand the following Jargon
Designed by Elegant Themes | Powered by WordPress

source