RENAMES Clause
- RENAMES clause regroups the existing group of items and assigns a new name.
- It creates another logical group by regrouping some or all elementary variables of a group.
- RENAMES clause and 66-level numbers are used to rename a group.
- RENAMES clause is crucial in providing a flexible way to access and manipulate data.
- It does not modify or insert items into the storage. Instead, it provides an alternative group for already-defined items.
Syntax -
----+----1----+----2----+----3----+----4----+----5----+
01 WS-VAR-GRP1.
05 WS-VAR-A PIC ...
05 WS-VAR-B PIC ...
.
.
05 WS-VAR-N PIC ...
05 WS-VAR-O PIC ...
.
.
05 WS-VAR-Z PIC ...
66 WS-VAR-GRP2 RENAMES VAR-A THRU VAR-N.
In the above syntax,
- WS-VAR-GRP1 - Specifies source group variable.
- WS-VAR-A, ..., WS-VAR-N - Specifies starting and ending elementary variables to be renamed.
- WS-VAR-GRP2 - Specifies target variable.
Points to note -
- THRU or THROUGH keyword is used only when renaming some elementary variables.
- THRU or THROUGH can be ignored when renaming the entire group.
- The source group name is used to rename the whole group.
Rules to Remember -
- Renaming elementary variables should be in sequential order.
- 66 level number shouldn't have a PIC or PICTURE clause.
- The RENAMES clause should follow the target variable in the declaration.
- Level-01, level-77, level-88, or other level-66 entries can't be renamed.
- Elementary variables that are declared with the OCCURS clause should not be renamed.
Explaining in detail -
Scenario1 - Renaming group variable.
The declaration of the group variable and renaming of the group variable are as follows -
02 A.
05 ITEM1 PIC X(5).
05 ITEM2 PIC X(5).
05 ITEM3 PIC X(5).
05 ITEM4 PIC X(5).
05 ITEM5 PIC X(5).
66 B RENAMES A.
In the above example, group variable A is declared with five elementary variables from ITEM1 to ITEM5. B is defined as the renaming of A without the THROUGH clause. Here, B is just a renaming variable for the data in variable A and uses the same memory location used by A.
The below diagram can explain how A and B represent memory -
Scenario2 - Renaming some elementary variables under a group.
The declaration of group variables and renaming of some elementary variables as follows -
02 A.
05 ITEM1 PIC X(5).
05 ITEM2 PIC X(5).
05 ITEM3 PIC X(5).
05 ITEM4 PIC X(5).
05 ITEM5 PIC X(5).
05 ITEM6 PIC X(5).
05 ITEM7 PIC X(5).
05 ITEM8 PIC X(5).
05 ITEM8 PIC X(5).
05 ITEM10 PIC X(5).
66 B RENAMES ITEM1 THRU ITEM6.
In the above example, group variable A is declared with ten elementary variables from ITEM1 to ITEM10. B is defined as the renaming of A with six elementary variables from ITEM1 to ITEM6. Here, B is just a renaming variable for the data from ITEM1 to ITEM6 and uses the same memory location used by A.
The below diagram can explain how A and B represent memory -
Practical Example -
Scenario - Renaming a group with some elementary variable in it.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
WORKING-STORAGE SECTION.
01 WS-VARIABLE.
02 WS-GRP-ITEM1.
05 WS-VAR1 PIC X(10) VALUE "MAINFRAMES".
05 FILLER PIC X(01).
05 WS-VAR2 PIC X(08) VALUE "ARE VAST".
05 FILLER PIC X(01).
05 WS-VAR3 PIC X(01) VALUE "&".
05 FILLER PIC X(01).
05 WS-VAR4 PIC X(10) VALUE "LEGENDARY".
05 FILLER PIC X(01).
05 WS-VAR5 PIC X(10) VALUE "SYSTEMS".
* Renaming WS-GRP-ITEM1
66 WS-GRP-ITEM2 RENAMES WS-VAR1 THROUGH WS-VAR2.
...
PROCEDURE DIVISION.
DISPLAY "GROUP ITEM1: " WS-GRP-ITEM1.
DISPLAY "GROUP ITEM2: " WS-GRP-ITEM2.
...
Output -
GROUP ITEM1: MAINFRAMES ARE VAST & LEGENDARY SYSTEMS GROUP ITEM2: MAINFRAMES ARE VAST