COBOL Data Items And Declaration

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:

  • LEVEL NAME / NUMBER
  • VARIABLE / DATA NAME
  • PICTURE CLAUSE
  • DATA TYPE
  • VALUE CLAUSE

EXAMPLE =>

01NEW-EMPLOYEPIC 9(10)VALUE “ 123 “  
               |               |               |               |
               |               |               |               |
LEVEL NAMEDATA NAMEPICTURE CLAUSEVALUE CLAUSE

As stated earlier, COBOL is a programming language similar to English. We don't have to learn a complicated syntax to perform COBOL commands.

1.   LEVEL NUMBER:-

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

  1. General purpose level number
  2. Special purpose 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

  1. Individual data item
  2. Group data item
  3. Elementary data item

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.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
ENVIRONMENT DIVISION.
DATA DIVISION.    
WORKING-STORAGE SECTION.
* * * * INDIVIDUAL / ELEMENTARY DATA ITEM * * * *
01 LEVEL-1 PIC 9(03) VALUE 256.
* * * * GROUP DATA ITEM * * * *
01 LEVEL-GROUP.
* * * * ELEMENTARY DATA ITEM * * * *
    05 LEVEL-21 PIC 9(03) VALUE 256.
    05 LEVEL-22 PIC 9(03) VALUE 128.
PROCEDURE DIVISION.
DISPLAY ' PROGRAM TO DISPLAY THE WORKING OF GENERAL PURPOSE LEVEL NUMBER'.
DISPLAY ' INDIVIDUAL DATA ITEM     : ' LEVEL-1.
DISPLAY ' GROUP DATA ITEM          : ' LEVEL-GROUP.
DISPLAY ' ELEMENTARY DATA ITEM - 1 : ' LEVEL-21.
DISPLAY ' ELEMENTARY DATA ITEM - 2 : ' LEVEL-22.
STOP RUN.

THE OUTPUT WILL BE:

PROGRAM TO DISPLAY THE WORKING OF GENERAL PURPOSE LEVEL NUMBER
INDIVIDUAL DATA ITEM     : 256
GROUP DATA ITEM          : 256128
ELEMENTARY DATA ITEM - 1 : 256
ELEMENTARY DATA ITEM - 2 : 128
COBOL Data Items And Declaration

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
  • 77 level number for INDIVIDUAL ELEMENTRY VARIABLES / DATA ITEMS
  • 88 level number for CONDITIONAL names

66 level number for RENAME clause – The rename is basically used to regroup elementary data items in group data items.

SYNTAX =>

66 data – name – 1 RENAMES data – name -2
                 [ THROUGH / THRU data –name -3 ]

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 =>

01 MAIN_GROUP
     05 ITEM - A1 PIC X(05)
     05 ITEM - A 2 PIC X(05)
     05 ITEM - A 3 PIC X(05)
     05 ITEM - A 4 PIC X(05)
     05 ITEM - A 5 PIC X(05)
     05 ITEM - A 6 PIC X(05)
     05 ITEM - A 7 PIC X(05)
     05 ITEM - A 8 PIC X(05)
     05 ITEM - A 9 PIC X(05)
     05 ITEM - A 10 PIC X(05)
66 SECOND_GROUP ITEM1 THROUGH ITEM5

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

ELEMENTARY ITEMS 12345678910
            
MAIN_GROUP ITEM -A1ITEM - A2ITEM - A3ITEM      - A4ITEM - A5ITEM - A6ITEM - A7ITEM - A8ITEM - A9ITEM - A10
SECOND_GROUP ITEM - A1ITEM – A2ITEM - A3ITEM - A4ITEM - A5     
  • 77 level number for INDIVIDUAL ELEMENTRY VARIABLES / DATA ITEMS – These are reserved for individual data items that do not have any hierarchical relationship with other data items mentioned or individual constants that are not sub – divided. These data items must be defined only in working storage section.

           It is only used for individual’s data items or elementary items. It cannot be subdivided; therefore, we should avoid using it.

  • 88 level number for CONDITIONAL names -  It is used under group item and works on the principle of true or false and is only used for conditional processing. We have to use VALUE clause in order to associate conditional name. 88 level number should not be defined with a picture clause.

SYNTAX =>

88 Condition – name { VALUE / VALUES } { literal low – value { THROUGH / THRU } high – value }
FORMAT – 01 )
88 Condition – name VALUE  literal / figurative constants
FORMAT – 02 )
88 Condition – name  VALUE  literal – 1 literal – 2 literal – 3 . . . literal – n
FORMAT – 03 )
88 Condition – name  VALUE  literal -1 THROUGH / THRU literal – n

DEFINEMENT OF LEVELS IN COBOL

  1. 01 LEVEL-top most level: this level contains individual/elementary data

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:-

01 EMP-RECORD. --GROUP ITEM
05 EMP-ID PIC9(5). --ELEMENTARY ITEM
05 EMP-NAME. --GROUP ITEM
10 FIRST-NAME PIC A(15). --ELEMENTARY ITEM
10 LAST-NAME PIC A(15). --ELEMENTARY ITEM
  • Level number are considered to be on the same level if they have same numeric value.
  • Level numbers must increase as we move down
  • levels from 01 -49 are used for general purposes
  • special purpose levels are 66,77,88
  • 66- used for rename clause and it should not have a picture clause.

EXAMPLE-

01 EMP-REC.
05 EMP-ID PIC 9(5).
05 EMP-NAME PIC A(10).
05 EMP-BIRTH-DATE PIC X(10).
66 EMP-DETAILS RENAME EMP-ID THRU EMP-NAME.

EXAMPLE-

01 CHECK-DAY.
05 DAY PIC X(3)
88 MONDAY VALUE 'MON'
88 TUESDAY VALUE 'TUE'
88 WEDNESDAY VALUE 'WED'
88 THURSDAY VALUE 'THU'
88 FRIDAY VALUE 'FRI'
88 SATURDAY VALUE 'SAT'
UNDAY VALUE 'SUN'

2)  VARIABLE / DATA NAME In COBOL

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

MS-NAME
TOTAL_EMPLOYES
T1000
100Z

INVALID DATA NAMES

MOVE ( reserved keyword )
ADD ( reserved keyword )
100 ( must be alphanumeric )
100+B ( +is not allowed )

 

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:-

  • Variables can be array, integer, file, or anything, but they must be declared in the working storage section.
  • The first character of the variable should be an alphabet, and the complete variable can be alphanumeric.
  • example-"Name21"
  • You can even use special characters

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

  • NUMERIC ( example- 255 13467 etc )
  • NON NUMERIC  ( example – ‘ COBOL ‘ ‘ DATA DIVISION ‘ etc)

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.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LITERAL PIC X(20) VALUE 'FIRST COBOL PROGRAM'.
01 FIG-CONST PIC X(03) VALUE '007'.
01 VARIABLE PIC X(05) VALUE 'NEW'.
PROCEDURE DIVISION.
DISPLAY 'VALUE OF LITERAL : ' LITERAL.
IF FIG-CONST IS NUMERIC
   DISPLAY ' FIGURATIVE CONSTANT IS NUMERIC '
ELSE
   DISPLAY ' FIGURATIVE CONSTANT IS NON NUMERIC '
END-IF.
DISPLAY 'VALUE OF VARIABLE : ' VARIABLE.
STOP RUN.

 

THE OUT PUT WILL BE:

VALUE OF LITERAL : FIRST COBOL PROGRAM 
 FIGURATIVE CONSTANT IS NUMERIC
VALUE OF VARIABLE : NEW
COBOL Data Items And Declaration
 

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

  • A string of characters representing a part of the information and what an item holds is called the PICTURE (or PIC) clause.
  • The picture clause generally consists of some picture characters that define how many characters or digits or what type of item is stored in memory.
  • For example, a 9 indicates a decimal digit, and an A represents alphanumeric.
  • We can use the Picture clause to define the data types in COBOL.
  • These consist of numeric 0 to 9, which represent numeric data. Their maximum length is 18 and is denoted by the symbol "9", alphabetic consist of A to Z and spaces their maximum length is 255. and is denoted by the symbol "A". Alphanumeric consist of unique characters, alphabets, and digits altogether they are denoted by symbol "X".

 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.

IDENTIFICATION DIVISION.
PROGRAM-ID. PICCLAUS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUMERIC PIC 9(03) VALUE 123.
01 WS-ALPHABET PIC A(10).
01 WS-ALPHANUMERIC PIC X(05).
01 WS-SIGNED-P PIC S9(03) VALUE +123.
01 WS-SIGNED-N PIC S9(03) VALUE -123.
01 WS-POSSIGN PIC +9(03).
01 WS-NEGSIGN PIC -9(03).
01 WS-IMPLIED-DEC PIC 9(03)V99 VALUE 123.45.
01 WS-ACTUAL-DEC PIC 9(03).99.
01 WS-ZERO-SUP PIC ZZ99.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY 'DISPLAYING PICTURE CLAUSE'.
DISPLAY 'NUMERIC ' WS-NUMERIC.
MOVE 123 TO WS-POSSIGN.
MOVE -123 TO WS-NEGSIGN.
MOVE 1234 TO WS-ACTUAL-DEC.
MOVE ‘TUTORIALANDEXAMPLE' TO WS-ALPHABET.
MOVE 'STW007' TO WS-ALPHANUMERIC.
MOVE 0099 TO WS-ZERO-SUP.
DISPLAY "ALPHABET " WS-ALPHABET.
DISPLAY "ALPHANUMERIC VALUE " WS-ALPHANUMERIC.
DISPLAY "SIGNED POSITIVE " WS-SIGNED-P.
DISPLAY "SIGNED NEGATIVE " WS-SIGNED-N.
DISPLAY "POSITIVE SIGN " WS-POSSIGN.
DISPLAY "NEGATIVE SIGN " WS-NEGSIGN.
DISPLAY "IMPLIED DECIMAL " WS-IMPLIED-DEC.
DISPLAY "ACTUAL DECIMAL " WS-ACTUAL-DEC.
DISPLAY "ZERO SUPPRESSION " WS-ZERO-SUP.
STOP RUN.

THE OUTPUT WILL BE:

DISPLAYING PICTURE CLAUSE
NUMERIC 123
ALPHABET TUTORIALANDEXAMPLE
ALPHANUMERIC VALUE STW00
SIGNED POSITIVE +123
SIGNED NEGATIVE -123
POSITIVE SIGN +123
NEGATIVE SIGN -123
IMPLIED DECIMAL 123.45
ACTUAL DECIMAL 234.00
ZERO SUPPRESSION 99

In this program, we have used and explained all data types supported by COBOL with example.

COBOL Data Items And Declaration

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

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VARIABLE PIC X(30).
01 WS-LITERAL-1 PIC 9(03) VALUE 123.
01 WS-LITERAL-2 PIC A(15) VALUE "I AM A LITERAL".
01 WS-LITERAL-3 PIC X(03) VALUE 'Z99'.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY ' Hello ,  world ' .
MOVE SPACES TO WS-VARIABLE.
MOVE " VARIABLES , LITERALS AND MORE " TO WS-VARIABLE.
DISPLAY WS-VARIABLE.
DISPLAY WS-LITERAL-1.
DISPLAY WS-LITERAL-2.
DISPLAY WS-LITERAL-3.
STOP RUN.

THE OUTPUT WILL BE:

Hello, world
VARIABLES, LITERALS, AND MORE 
123
I AM A LITERAL 
Z99

Here in this program, we have defined various data names and assigned them with numeric, non-numeric and alphanumeric texts.

COBOL Data Items And Declaration

Sample PROGRAM 

This is a demo program to learn and understand the concept of variables, literals, and figurative constants

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STD-DTL.
   02 STD-NUM PIC 9(03).
   02 STD-NAME.
      05 STD-NAME-INIT PIC X(01).
         88 STD-NAME-VALID VALUE 'A' THRU 'Z'.
         88 STD-NAME-INVALID VALUE '0' THRU '9'.
      05 STD-NAME-REST PIC X(14).
    02 STD-GENDER PIC X(01).
         88 VALID-GENDER VALUE 'M' 'F'.
         88 MALE VALUE 'M'.
         88 FEMALE VALUE 'F'.
    02 STD-MARKS PIC 9(03).
         88 FIRST-CLASS  VALUE 060 THRU 100.
         88 SECOND-CLASS VALUE 050 THRU 059.
         88 THIRD-CLASS  VALUE 035 THRU 049.
         88 FAIL VALUE 000 THRU 034.
PROCEDURE DIVISION.
     ACCEPT STD-NUM.
     ACCEPT STD-NAME.
     ACCEPT STD-GENDER.
     ACCEPT STD-MARKS.
     DISPLAY ' STUDENT DETAILS . . . . .  '.
     DISPLAY ' STUDENT NUMBER : ' STD-NUM.
     IF STD-NAME-VALID
        DISPLAY ' STUDENT NAME : ' STD-NAME
     ELSE
        DISPLAY ' STUDENT NAME WAS INVALID '
     END-IF.
     IF VALID-GENDER
        IF MALE
           DISPLAY 'STUDENT GENDER : MALE'
        ELSE
           DISPLAY 'STUDENT GENDER : FEMALE'
        END-IF
     ELSE
        DISPLAY ' INVALID GENDER WAS GIVEN '
     END-IF.   
     EVALUATE TRUE
        WHEN FIRST-CLASS
             DISPLAY ' STUDENT GOT FIRST CLASS '
        WHEN SECOND-CLASS
             DISPLAY ' STUDENT GOT SECOND CLASS '
        WHEN THIRD-CLASS
             DISPLAY ' STUDENT GOT THIRD CLASS '
        WHEN OTHER
             DISPLAY ' STUDENT GOT FAILED '
     END-EVALUATE.        
STOP RUN.

THE OUT PUT WILL BE:

 STUDENT DETAILS . . . . .  
 STUDENT NUMBER : 000
STUDENT NAME WAS INVALID
 INVALID GENDER WAS GIVEN
 STUDENT GOT FAILED
COBOL Data Items And Declaration
COBOL Data Items And Declaration

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 are

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

DATA NAME (TOTAL-STUDENTS)
LEVEL NAME (01)
PICTURE CLAUSE (PIC9(5))
VALUE CLAUSE (VALUE'125')