If you wish to define a particular storage with a different data descriptions, then you can use COBOL Redefines clause. In addition, it is also a faster way to reinterpret the same data.
In this way, different data items can refer to the same memory location to reduce the memory space and the program is easier to maintain as well.
For instance, you can store a alphanumeric & a numeric data type at the same location using the Redefines in COBOL. Also, you can redefine alphanumeric to numeric using this clause.
Syntax:
level-number redefining-item REDEFINES redefined-item. level-number FILLER REDEFINES redefined-item.
There are many ways to use REDEFINES Clause. To illustrate, we are going to cover many examples here –
01 WS-VAR1 PIC X(12). 01 WS-VAR2 REDEFINES WS-VAR1 PIC X(12).
Here, WS-VAR1 is redefined to WS-VAR2. It does not mean that you cannot refer WS-VAR1, rather it means that WS-VAR2 shares the same memory location as WS-VAR1. So, WS-VAR2 is the redefining item while WS-VAR1 is redefined item.
Basic Example
IDENTIFICATION DIVISION. PROGRAM-ID. REDEFCLA. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ITEM1 PIC 9(2) VALUE 4. 01 WS-ITEM2 REDEFINES WS-ITEM1 PIC 9(2). PROCEDURE DIVISION. DISPLAY 'VALUE OF WS-ITEM1 IS - ' WS-ITEM1. DISPLAY 'VALUE OF WS-ITEM2 IS - ' WS-ITEM2. STOP RUN.
JCL to run the above program
//REDEFJCL JOB(TUTORIAL,XXXXXX),CLASS = A,MSGCLASS = A //STEP1 EXEC PGM = REDEFINE
Output
VALUE OF WS-ITEM1 IS – 4
VALUE OF WS-ITEM2 IS – 4
Rules for REDEFINES
- First, both the items i.e. redefined item as well as the redefining item must be at the same level.
The below example shows a wrong way of using Redefines because both the data items are at different level.
01 WS-VAR1 PIC X(12). 01 WS-VAR2. 05 WS-VAR3 REDEFINES WS-VAR1 PIC X(12). 05 WS-VAR4 PIC X(5).
- Level Number 66 and 88 cannot be used for Redefines in COBOL as it is not allowed to redefine level numbers 66 and 88.
- The most common level numbers to use a REDEFINES clause are from 01 to 49 but Level number 77 can also be used.
- Obviously, you must NOT code the VALUE clause with a redefining item.
- In contrast, do not use the redefines clause in FILE SECTION with level number 01.
- REDEFINES may or may not have a picture clause.
- The value of Redefined item and REDEFINING item is always same when you use the REDEFINES clause.
Example
IDENTIFICATION DIVISION. PROGRAM-ID. REDEFCLA. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ITEM1 PIC 9(2) VALUE 4. 01 WS-ITEM2 REDEFINES WS-ITEM1 PIC 9(2). PROCEDURE DIVISION. DISPLAY 'OLD VALUE OF WS-ITEM1 IS - ' WS-ITEM1. DISPLAY 'OLD VALUE OF WS-ITEM2 IS - ' WS-ITEM2. ADD WS-ITEM1 TO WS-ITEM2. DISPLAY ‘NEW VALUE OF WS-ITEM1 IS - ' WS-ITEM1. DISPLAY ‘NEW VALUE OF WS-ITEM2 IS - ' WS-ITEM2. STOP RUN.
Output
OLD VALUE OF WS-ITEM1 IS – 4
OLD VALUE OF WS-ITEM2 IS – 4
NEW VALUE OF WS-ITEM1 IS – 8
NEW VALUE OF WS-ITEM2 IS – 8
Note:
- The redefined and redefining items will always have the same value at any point of time but the only exception is when the length of Redefined and Redefining items are different. In that case the value of data item which has lesser length might get truncated. Refer Rule 11 to understand more.
- Usage of the redefined data item can be different from the usage of the redefining item.
Example
01 ITEM-1 PIC 9(5) USAGE DISPLAY. 01 ITEM-2 REDEFINES ITEM-1 PIC 9(03) USAGE COMP-3.
- You can redefine the redefined data item any number of times but you cannot redefine the redefining item.
Example
01 WS-VAR1 PIC X(12). 01 WS-VAR2 REDEFINES WS-VAR1 PIC X(12). 01 WS-VAR3 REDEFINES WS-VAR1 PIC 9(10). 01 WS-VAR4 REDEFINES WS-VAR1 PIC A(10).
The above code is right but the below code is wrong.
01 WS-VAR1 PIC X(12). 01 WS-VAR2 REDEFINES WS-VAR1 PIC X(12). 01 WS-VAR3 REDEFINES WS-VAR2 PIC 9(10).
- The length of the REDEFINING item can be same or different than the length of the redefined field.
Example
01 ITEM-1 PIC 9(06). 01 ITEM-2 REDEFINES ITEM-1 PIC 9(06).
Here, ITEM-1 and ITEM-2 are numbers and both occupies the same length. Suppose, ITEM-1 is stored starting from 0 to 6 bytes in the memory then ITEM-2 will point to the same memory location starting from 0 to 6 bytes as well.
01 ITEM-1 PIC X(10). 01 ITEM-2 REDEFINES ITEM-1 PIC 9(09).
Similarly, ITEM-1 is alphanumeric while ITEM-2 is numeric. Suppose, ITEM-1 is stored starting from 0 to 10 bytes in the memory then ITEM-2 will point to the same memory location starting from 0 to 9 bytes only because its length is 9.
- The data type of the redefined item and redefining item can be same or different.
Example
IDENTIFICATION DIVISION. PROGRAM-ID. REDEFINE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ALPHANUM PIC X(13). 01 WS-NUM REDEFINES WS-ALPHANUM PIC 9(8). PROCEDURE DIVISION. MOVE "TUTORIALBRAIN" TO WS-ALPHANUM. DISPLAY "WELCOME TO : " WS-ALPHANUM. DISPLAY "BEST MAINFRAME " WS-NUM. STOP RUN.
Output
WELCOME TO TUTORIALBRAIN
BEST MAINFRAME TUTORIAL
Note(Fun Fact):
- WS-ALPHANUM holds a value of TUTORIALBRAIN while WS-NUM has a value of TUTORIAL because it can hold 10 bytes only.
- Even though WS-NUM is declared as Numeric, it can still hold alphabets of length 8 because WS-ALPHANUM and WS-NUM refers to the same memory location.
- As WS-NUM is defined as Numeric but it has a value of TUTORIAL so you cannot use this variable in any arithmetic operation or logical comparisons else you will get SOC7 ABEND.
- OCCURS clause can also be used along with REDEFINES.
Example
IDENTIFICATION DIVISION. PROGRAM-ID. REDEF. DATA DIVISION. 01 WEEK. 05 DAY-1 PIC X(12) VALUE ' MONDAY '. 05 DAY-2 PIC X(12) VALUE ' TUESDAY '. 05 DAY-3 PIC X(12) VALUE ' WEDNESDAY '. 05 DAY-4 PIC X(12) VALUE ' THURSDAY '. 05 DAY-5 PIC X(12) VALUE ' FRIDAY '. 05 DAY-6 PIC X(12) VALUE ' SATURDAY '. 05 DAY-7 PIC X(12) VALUE ' SUNDAY '. 01 WEEK1 REDEFINES WEEK PIC X(12) OCCURS 7 TIMES. 01 Y PIC 9 VALUE 1. PROCEDURE DIVISION. PERFORM VARYING Y FROM 1 BY 1 UNTIL Y>7 DISPLAY Y " DAY OF WEEK - " WEEK1(Y) END-PERFORM. STOP RUN.
Output
1 DAY OF WEEK – MONDAY
2 DAY OF WEEK – TUESDAY
3 DAY OF WEEK – WEDNESDAY
4 DAY OF WEEK – THURSDAY
5 DAY OF WEEK – FRIDAY
6 DAY OF WEEK – SATURDAY
7 DAY OF WEEK – SUNDAY
- The redefining item can also contain elementary items.
Example
01 WS-VAL1 PIC X(08). 01 WS-VAL2 REDEFINES WS-VAL1. 05 WS-VAL21 PIC X(05). 05 WS-VAL22 PIC 9(03).
- Likewise, the redefined item can also contain elementary items.
Example
IDENTIFICATION DIVISION. PROGRAM-ID. REDEFNES. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CHECK-DATE. 05 WS-DOB VALUE '20200205'. 10 WS-YY PIC X(4). 10 WS-MM PIC X(2). 10 WS-DD PIC X(2). 05 WS-EXPIRY-DATE REDEFINES WS-DOB PIC 9(8). PROCEDURE DIVISION. DISPLAY "DOB IS : " WS-DOB. DISPLAY "EXPIRY DATE IS : " WS-EXPIRY-DATE. STOP RUN.
Output
DOB IS : 20200205
EXPIRY DATE IS : 20200205