Data Division is used to define the variables used in a program. To describe data in COBOL, one must understand the following terms −
- Data Name
- Level Number
- Picture Clause
- Value Clause
01 TOTAL-STUDENTS PIC9(5) VALUE '125'. | | | | | | | | | | | | Level Number Data Name Picture Clause Value Clause
Data Name
Data names must be defined in the Data Division before using them in the Procedure Division. They must have a user-defined name; reserved words cannot be used. Data names give reference to the memory locations where actual data is stored. They can be elementary or group type.
Example
The following example shows valid and invalid data names −
Valid: WS-NAME TOTAL-STUDENTS A100 100B Invalid:MOVE(Reserved Words)COMPUTE(Reserved Words)100(NoAlphabet)100+B (+isnot allowed)
Level Number
Level number is used to specify the level of data in a record. They are used to differentiate between elementary items and group items. Elementary items can be grouped together to create group items.
Sr.No. | Level Number & Description |
---|---|
1 | 01Record description entry |
2 | 02 to 49Group and Elementary items |
3 | 66Rename Clause items |
4 | 77Items which cannot be sub-divided |
5 | 88Condition name entry |
- Elementary items cannot be divided further. Level number, Data name, Picture clause, and Value clause (optional) are used to describe an elementary item.
- Group items consist of one or more elementary items. Level number, Data name, and Value clause (optional) are used to describe a group item. Group level number is always 01.
Example
The following example shows Group and Elementary items −
DATADIVISION.WORKING-STORAGESECTION.01 WS-NAME PICX(25). ---> ELEMENTARY ITEM 01 WS-CLASS PIC9(2)VALUE'10'. ---> ELEMENTARY ITEM 01 WS-ADDRESS. --->GROUP ITEM 05 WS-HOUSE-NUMBER PIC9(3). ---> ELEMENTARY ITEM 05 WS-STREET PICX(15). ---> ELEMENTARY ITEM 05 WS-CITY PICX(15). ---> ELEMENTARY ITEM 05 WS-COUNTRY PICX(15)VALUE'INDIA'. ---> ELEMENTARY ITEM
Picture Clause
Picture clause is used to define the following items −
- Data type can be numeric, alphabetic, or alphanumeric. Numeric type consists of only digits 0 to 9. Alphabetic type consists of letters A to Z and spaces. Alphanumeric type consists of digits, letters, and special characters.
- Sign can be used with numeric data. It can be either + or .
- Decimal point position can be used with numeric data. Assumed position is the position of decimal point and not included in the data.
- Length defines the number of bytes used by the data item.
Symbols used in a Picture clause −
Sr.No. | Symbol & Description |
---|---|
1 | 9Numeric |
2 | AAlphabetic |
3 | XAlphanumeric |
4 | VImplicit Decimal |
5 | SSign |
6 | PAssumed Decimal |
Example
The following example shows the use of PIC clause −
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICS9(3)V9(2).01 WS-NUM2 PICPPP999.01 WS-NUM3 PICS9(3)V9(2)VALUE-123.45.01 WS-NAME PICA(6)VALUE'ABCDEF'.01 WS-ID PICX(5)VALUE'A121$'.PROCEDUREDIVISION.DISPLAY"WS-NUM1 : "WS-NUM1.DISPLAY"WS-NUM2 : "WS-NUM2.DISPLAY"WS-NUM3 : "WS-NUM3.DISPLAY"WS-NAME : "WS-NAME.DISPLAY"WS-ID : "WS-ID.STOPRUN.
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C //STEP1 EXEC PGM = HELLO
When you compile and execute the above program, it produces the following result −
WS-NUM1 : +000.00 WS-NUM2 : .000000 WS-NUM3 : -123.45 WS-NAME : ABCDEF WS-ID : A121$
Value Clause
Value clause is an optional clause which is used to initialize the data items. The values can be numeric literal, alphanumeric literal, or figurative constant. It can be used with both group and elementary items.
Example
The following example shows the use of VALUE clause −
Open Compiler
IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC99V9VALUEIS3.5.01 WS-NAME PICA(6)VALUE'ABCD'.01 WS-ID PIC99VALUEZERO.PROCEDUREDIVISION.DISPLAY"WS-NUM1 : "WS-NUM1.DISPLAY"WS-NAME : "WS-NAME.DISPLAY"WS-ID : "WS-ID.STOPRUN.
JCL to execute the above COBOL program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C //STEP1 EXEC PGM = HELLO
When you compile and execute the above program, it produces the following result −
WS-NUM1 : 03.5 WS-NAME : ABCD WS-ID : 00
Leave a Reply