Data References


Every user-defined variable in the program should identify uniquely in the program. However, the same variable name is used multiple times to name the data. In this case, the variable name must be qualified to ensure the uniqueness of the reference. IN and OF are used for reference, and both are logically equivalent.

For example - Assume the same elementary variables are declared under two group variables. Referring to elementary variables should be like below -

----+----1----+----2----+----3----+----4----+----5----+
        01 WS-GROUP1.
           02 WS-ITEM1       PIC X(10).
           02 WS-ITEM2       PIC X(10).
		   
        01 WS-GROUP2.
           02 WS-ITEM1       PIC X(10).
           02 WS-ITEM2       PIC X(10).	
           ...
        PROCEDURE DIVISION.		   
           ...
           DISPLAY WS-ITEM1 OF WS-GROUP1.
		   
           MOVE WS-ITEM1 IN WS-GROUP1
             TO WS-ITEM2 IN WS-GROUP2.
           ... 

If the references are not used for the above case (i.e., WS-ITEM1 is coded without IN or OF), then the system throws the below error -

"WS-ITEM1" was not a uniquely defined name. The definition to be used could not be determined from the context. The reference to the name was discarded.

Rules -

  • Data references are not used for individual variables.
  • Data references are used only for the uniqueness of the same names declared under different hierarchies.
  • The data name associated with the highest level must be unique in any hierarchy and cannot be qualified.

Variable references -


Elementary variables under different group variables can have the same names. In this case, we can't directly code the elementary variable, because the same variable is declared twice. We should use referencing with their group variable to make them unique.

Syntax -

 elementary-variable IN|OF group-variable

Example -

Scenario1 - Describes how the references used for data division names in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DREFDDN.
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-GROUP1.
          05 WS-VAR1           PIC X(10) VALUE 'MAINFRAMES'. 
          05 WS-VAR2           PIC 9(04) VALUE 2021.
       01 WS-GROUP2.
          05 WS-VAR1           PIC X(10). 
          05 WS-VAR2           PIC 9(04). 

       PROCEDURE DIVISION. 

           MOVE WS-VAR1 IN WS-GROUP1 
             TO WS-VAR1 IN WS-GROUP2. 
           DISPLAY "WS-GROUP1.WS-VAR1: " WS-VAR1 OF WS-GROUP1.
           DISPLAY "WS-GROUP2.WS-VAR1: " WS-VAR1 OF WS-GROUP2.

           STOP RUN.

Output -

WS-GROUP1.WS-VAR1: MAINFRAMES
WS-GROUP2.WS-VAR1: MAINFRAMES

Explaining Example -

WS-VAR1, WS-VAR2 are elementary items declared under group items WS-GROUP1, WS-GROUP2. WS-VAR1 is not unique and should have reference while using in the program. So, WS-VAR1 under WS-GROUP1 refers as WS-VAR1 OF WS-GROUP1 or WS-VAR1 IN WS-GROUP1. Similarly, refernces should specify for WS-VAR2.

Condition name references -


Condition names under different variables can have the same names. In this case, we can't directly code the condition name, because the same condition name is declared twice. We should use referencing with their variable to make them unique.

Syntax -

 condition-name IN|OF variable

Example -

Scenario2 - Below example describes how the references used for condition names in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DREFCN.
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.
	   
       01 WS-GROUP1. 
          05 WS-GENDER         PIC X(01).
             88 MALE           VALUE 'M'.
             88 FEMALE         VALUE 'F'.  
       01 WS-GROUP2.
          05 WS-GENDER         PIC X(01).
             88 MALE           VALUE 'M'. 
             88 FEMALE         VALUE 'F'.
 
       PROCEDURE DIVISION. 

           MOVE 'M'     TO WS-GENDER OF WS-GROUP1.
           IF MALE OF WS-GROUP1 
              DISPLAY 'PERSON IS MALE'
           ELSE 
              DISPLAY 'PERSON IS FEMALE'
           END-IF.
           STOP RUN.

Output -

PERSON IS MALE

Explaining Example -

WS-GENDER is elementary item declared under group items WS-GROUP1, WS-GROUP2. WS-GENDER declared with condition names MALE and FEMALE. Those condition names are not unique and should have reference while using in the program. So, MALE condition name under WS-GROUP1 refers as MALE OF WS-GROUP1 or MALE IN WS-GROUP1. Similarly, refernces should specify for FEMALE condition name.

Paragraph references -


Paragraphs under different sections can have the same names. In this case, we can't directly code the paragraph, because the same paragraph is declared twice. We should use referencing with their section names to make them unique.

Syntax -

 paragraph-name IN|OF section-name

Example -

Scenario3 - Below example describes how the references used for procedure division names in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. DREFPDN. 
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-GROUP1.
          05 WS-GENDER         PIC X(01). 
             88 MALE           VALUE 'M'. 
             88 FEMALE         VALUE 'F'. 

       PROCEDURE DIVISION.

           MOVE 'M'     TO WS-GENDER OF WS-GROUP1. 
 
           PERFORM PARAGRAPH1 OF SECTION1.
           STOP RUN.

       SECTION1 SECTION.
       PARAGRAPH1.
           IF MALE OF WS-GROUP1
              DISPLAY 'PERSON IS MALE' 
           ELSE
              DISPLAY 'PERSON IS FEMALE'
           END-IF.

       SECTION2 SECTION.
       PARAGRAPH1.
           IF MALE OF WS-GROUP1 
              DISPLAY 'PERSON IS MALE'
           ELSE 
              DISPLAY 'PERSON IS FEMALE'
           END-IF.

Output -

PERSON IS MALE

Explaining Example -

PARAGRAPH1 is a paragraph declared under section SECTION1, SECTION2. The paragraph PARAGRAPH1 is not unique and should have reference while using in the program. So, PARAGRAPH1 under SECTION1 refers as PARAGRAPH1 OF SECTION1 or PARAGRAPH1 IN SECTION1. Similarly, refernces should specify for PARAGRAPH1 under SECTION2.

Copybook references -


Two files can have the same record structure, which should be declared a record structure for both files. In this case, we can't directly code the variables, because the same variable is declared twice. We should use referencing with their record name to make them unique.

Syntax -

 variable-name IN|OF record-name

Example -

Copybook -

Student copybook

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. DREFCL. 
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-STDREC1.
          COPY STDREC. 

       01 WS-STDREC2.
          COPY STDREC.

       PROCEDURE DIVISION.

           MOVE 1       TO STD-NO     OF WS-STDREC1.
           MOVE 'NAME1' TO STD-NAME   OF WS-STDREC1.
           MOVE 'MALE'  TO STD-GENDER OF WS-STDREC1.

           MOVE WS-STDREC1  TO WS-STDREC2.

           DISPLAY 'WS-STDREC1:  ' WS-STDREC1.
           DISPLAY 'WS-STDREC2:  ' WS-STDREC2. 

           STOP RUN.

Output -

Data references for copy library output

Explaining Example -

STDREC is a copybook added under records WS-STDREC1, WS-STDREC2. STD-NO, STD-NAME, STD-GENDER are the variables in the copybook STDREC. These variables are not unique and should have reference record name while using in the program. So, STD-NO under WS-STDREC1 refers as STD-NO OF WS-STDREC1 or STD-NO IN WS-STDREC1. Similarly, refernces should specify for STD-NAME and STD-GENDER.