Open In App

Data Layout in COBOL

Last Updated : 19 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

COBOL is a programming language that was developed in the 1950s for business and financial applications. In COBOL, the data layout is the arrangement of data items in a program. It specifies how the data is organized and how it is accessed. 

COBOL programs are organized into four divisions: the identification division, the environment division, the data division, and the procedure division.

  • The identification division contains general information about the program, such as its name and the date it was written.
  • The environment division specifies the hardware and software environment in which the program will run, including details such as the computer’s operating system and the type of input/output devices that will be used.
  • The data division is used to define the data layout of the program, including the names and types of each piece of data. It consists of three sections: the file section, the working-storage section, and the linkage section. 
    • The file section is used to define data that is stored in external files, such as databases or text files. 
    • The working-storage section is used to define data that is used temporarily within the program, such as intermediate calculations. 
    • The linkage section is used to define data that is passed between different programs or subprograms.
  • The procedure division is where the program’s instructions are written. These instructions tell the computer what to do with the data that has been defined in the data division.

In COBOL, data description entries are used to define the characteristics of data items, such as their names, data types, and sizes. Some of the different data description entries used in COBOL include:

  • 01-level data description entry: This is the most common type of data description entry in COBOL and is used to define individual data items.
  • 77-level data description entry: This type of data description entry is used to define a group of related data items.
  • 88-level data description entry: This type of data description entry is used to define a condition or value that can be used to evaluate expressions in the program.
  • 66-level data description entry: This type of data description entry is used to define a set of mutually exclusive conditions for a group of related data items.

In addition to these data description entries, COBOL also includes other entries, such as FILE, WORKING-STORAGE, and LOCAL-STORAGE, which are used to define the organization and storage of data in a COBOL program.

In the data division, data items are defined using COBOL data descriptions. A data description specifies the name, type, and size of a data item. It can also specify the value or range of values that the data item can take on.

For example, the following COBOL data description defines a data item called “name” that is a character string of length 20:

01 name PIC X(20)

This data description specifies that “name” is a data item of a type character string (PIC X), and it has a length of 20 characters. Data descriptions can also include additional information, such as the level number, which is used to specify the hierarchy of data items in the program, and the usage clause, which specifies how the data item is used in the program.

In COBOL, the data layout is an important aspect of program design and implementation. It determines how the data is stored and accessed, and it plays a critical role in the performance and reliability of the program.

COBOL Layout is a description of the usage of each field and its values. The data description entries in COBOL are as follows:

  1. Redefines Entries
  2. Renames Entries
  3. Usage Clause
  4. Copybooks

Redefines Entries: The REDEFINES clause in COBOL allows you to define a new data item that shares the same storage location as an existing data item. This allows multiple data items to share the same storage location in memory, which can be useful for optimizing the storage and processing of data in a COBOL program. For example:

01 old-data PIC X(10).
01 new-data REDEFINES old-data PIC 9(4)

In this example, the data item “new-data” is defined as a 4-digit integer (PIC 9(4)) that shares the same storage location as the data item “old-data”, which is a character string of length 10 (PIC X(10)).

The level numbers of redefined items and redefining items must be the same, and the level numbers cannot be 66 or 88. With a redefining object, we cannot use the VALUE clause.

Program to demonstrate Redefines Clause:

Cobol




IDENTIFICATION DIVISION.  
    PROGRAM-ID. HELLO.  
    
DATA DIVISION.  
    WORKING-STORAGE SECTION.  
    01 WS-DESCRIPTION.  
    05 WS-DATE1 VALUE 20221214.  
    10 WS-YEAR PIC X(4).  
    10 WS-MONTH PIC X(2).  
    10 WS-DATE PIC X(2).  
    05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(8).  
    
PROCEDURE DIVISION.  
    DISPLAY "WS-DATE1 : "WS-DATE1.  
    DISPLAY "WS-DATE2 : "WS-DATE2.  
    
STOP RUN.


Output: 

 

Renames Entries: The RENAMES clause in COBOL allows you to give an alternative name to an existing data item. For example:

Cobol




01 original-name PIC X(10).
01 new-name RENAMES original-name


In this example, the data item “new-name” is defined as an alternative name for the data item “original-name”. The two names refer to the same data item and can be used interchangeably in the program.

There are some rules for RENAMES clause:

  • RENAMES entries must be in sequential order.
  • There is no PIC clause for the 66-level numbers.
  • At the end of the group, RENAMES must be coded.
  • Level-66 can’t rename level-01, 77, 88, or another level-66 entry.
  • The OCCURS clause should not be used to RENAMED elementary items.

Program to demonstrate Renames Clause:

Cobol




IDENTIFICATION DIVISION.  
    PROGRAM-ID. HELLO.  
    
DATA DIVISION.  
    WORKING-STORAGE SECTION.  
    01 WS-DESCRIPTION.  
    05 WS-NUM.  
    10 WS-NUM1 PIC 9(2) VALUE 20.  
    10 WS-NUM2 PIC 9(2) VALUE 49.  
    05 WS-CHAR.  
    10 WS-CHAR1 PIC X(2) VALUE 'AA'.  
    10 WS-CHAR2 PIC X(2) VALUE 'BB'.  
    66 WS-RENAME RENAMES WS-NUM2 THRU WS-CHAR2.  
    
PROCEDURE DIVISION.  
    DISPLAY "WS-RENAME : " WS-RENAME.  
    
STOP RUN.


Output: 

 

Usage Clause: The USAGE clause in COBOL specifies how a data item is used in the program. It can be used to specify the type of data that a data item can contain, such as numeric, alphabetic, or alphanumeric data. For example:

Cobol




01 numeric-data PIC 9(4) USAGE COMP.
01 alphabetic-data PIC X(10) USAGE DISPLAY.


In this example, the data item “numeric-data” is defined as a 4-digit integer that is stored in a compressed form (USAGE COMP). The data item “alphabetic-data” is defined as a character string of length 10 that is intended for display (USAGE DISPLAY).

  • This clause tells the operating system how the data is stored internally.
  • The usage clause is used for every data item declared in COBOL.
  • This clause cannot be used in level number 66, 77, and 88.

The following usage clauses are frequently used in COBOL:

  • DISPLAY: DISPLAY is the default usage clause. ASCII is the memory format for storing data items. Each character in the data will take 1 byte i.e 1 digit/char =1 byte. This clause is relevant to all data types in COBOL.
  • COMP / COMPUTATIONAL: The COMP usage clause is useful only for the numeric data type. The data item in the COMP variable is stored in binary format.
    Syntax:

01 NUMB PIC S9 (n) USAGE IN COMP

  • In the above syntax, if the value of ‘n’ is 1 to 4, it takes 2 bytes. If the value of ‘n’ is 5 to 9, it takes 4 bytes. If the value of ‘n’ is 10 to 18, it takes 8 bytes.
  • COMP-1: It is relevant to the float point data type. In COMP-1, the data is internally stored in hexadecimal format. Here, 1 word = 4 bytes.
  • COMP-2: COMP-2 represents the double-precision floating point number. In COMP-2, 2 words = 8 bytes.
  • COMP-3: In COMP-3, the data item is stored in a pack of decimal format. Each digit size is 1 nibble or half a byte.

Copybooks: A copybook in COBOL is a file that contains a set of COBOL data descriptions that can be included in one or more COBOL programs. Copybooks are used to define common data structures that are used by multiple programs, such as records or data files. For example:

Copybook file:
01 customer-record.
02 customer-name PIC X(30).
02 customer-address PIC X(50).
02 customer-phone PIC X(15).
Program A:
COPY customer-record.
...
Program B:
COPY customer-record.

...

In this example, the copybook file defines a data structure called “customer record” that consists of three data items: “customer name”, “customer address”, and “customer phone”. The COPY statement in Program A and Program B includes the data definitions from the copybook file, making the data items from the copybook available for use in the programs. Copybooks can be used to improve code reuse and maintainability in COBOL programs.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads