Data Layout in COBOL

Introduction

A layout gives detailed representation about the use of each field and the values present in it.

Data description entries

The data description entries used in COBOL are as follows:

1. Redefines Clause

To define a storage with different data description, redefines clause is used.

Syntax:
01 WS-OLD PIC X(10).
01 WS-NEW1 REDEFINES WS-OLD PIC 9(8).
01 WS-NEW2 REDEFINES WS-OLS PIC A(10).

In the above syntax, WS-OLD is redefined item and WS-NEW1 and WS-NEW2 are redefining item.

The level number should be same for redefining item and redefined item. It cannot be 66 or 88.

Example : Program to demonstrate Redefines Clause

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-DESCRIPTION.
   05 WS-DATE1 VALUE '2016-07-28'.
   10 WS-YEAR PIC X(5).
   10 WS-MONTH PIC X(3).
   10 WS-DATE PIC X(2).
   05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(10).

PROCEDURE DIVISION.
   DISPLAY "WS-DATE1 : "WS-DATE1.
   DISPLAY "WS-DATE2 : "WS-DATE2.

STOP RUN.


Output:
WS-DATE1 : 2016-07-28
WS-DATE2 : 2016-07-28

2. Renames Clause

  • This clause is used for regrouping the data names and renaming the elementary or group item.
  • Level number 66 is reserved for the rename clause.
  • Renaming is applicable only for the same level .
  • Level numbers 01,77, 88 or 66 are not used with the renames clause.
  • The renames entries should be in sequence.
  • Cannot rename the data items with OCCUR clause.

Example : Program to demonstrate Renames Clause

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-DESCRIPTION.
   05 WS-NUMBERS.
   10 WS-N1 PIC 9(2) VALUE 20.
   10 WS-N2 PIC 9(2) VALUE 56.
   10 WS-N3 PIC 9(3) VALUE 456.
   05 WS-NAMES.
   10 WS-NAME-1 PIC X(3) VALUE 'XYZ'.
   10 WS-NAME-2 PIC X(3) VALUE 'PQR'.
   10 WS-NAME-3 PIC X(3) VALUE 'ABC'.
   66 WS-RENAME RENAMES WS-N2 THRU WS-NAME-2.

PROCEDURE DIVISION.
   DISPLAY "Output of WS-RENAME : " WS-RENAME.
   
STOP RUN.


Output:
Output of WS-RENAME : 56456XYZPQR

3. Usage clause

  • This clause tells the operating system that how the data is stored internally.
  • Usage clause is used for every data item declared in COBOL.
  • This clause cannot be used in the level number 66, 77 and 88.
Following usage clauses are frequently used in COBOL:

I) 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.
II) COMP / COMPUTATIONAL
  • The COMP usage clause is useful only for the numeric data type.
  • The data item in COMP variable is stored in binary format.
Syntax:
01 NUMB PIC S9 (n) USAGE IN COMP

In 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 byte.

III) COMP-1
  • It is relevant to floating point data type.
  • In COMP-1, the data is internally stored in hexadecimal format.
  • Here, 1 word = 4 bytes.
IV) COMP-2
  • COMP-2 represents the double precision floating point number.
  • In COMP-2, 2 word = 8 bytes.
V) COMP-3
  • In COMP-3, data item is stored in a pack of decimal format.
  • Each digit size is 1 nibble or half byte.

4. Copybooks

  • In COBOL, copybook is a selection of code which defines data structure.
  • If the same data structure is used in many programs, then instead of writing same data structure copybook is used.
  • To include copybook in a program, COBOL program uses the COPY statement. This copy statement is used in a working-storage section.