COBOL Redefines Clause how to use effectively

Best COBOL Redefines clause examples and important rules you can apply for your COBOL development projects.

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

Utilize your memory effectively using REDEFINES clause.

In the above example, original data type is PICX(10). So using REDEFINES, the same data type you can use as either PIC 9(8) or PIC A(10).

COBOL REDEFINES Rules

The REDEFINES clause allows you to have multiple field definitions for the same piece of storage. The same data then can be referenced in multiple ways.

Take a simple example, useful for data validation:

01 WS-NUMBER-X PIC X(8).
01 WS-NUMBER REDEFINES WS-NUMBER-X
PIC 9(6)V99.

This portion of the data division only consumes 8 bytes of storage, not 16. Each of the two PIC clauses is describing the same 8 bytes of data, just doing it differently.

Once data is in WS-NUMBER-X it can be checked to see if it is numeric (IF WS-NUMBER-X IS NUMERIC). If so, WS-NUMBER can then be used as part of a calculation.

If the data happens to be non-numeric then this type of code will prevent the program from choking. We access the data as alphanumeric (any data allowed) to see if it is safe to access it as numeric before actually attempting to do so.

Note that once the data was moved to WS-NUMBER-X it was also moved to WS-NUMBER because they both describe the same portion of storage.

Top REDEFINES Rules

  1. A redefinition must have the same level number as the field it is redefining
  2. The redefinition must immediately follow the field it is redefining (i.e. if an 05-level field is being redefined then the redefinition must be the next 05-level field)
  3. Cannot have a REDEFINES of an 01-level field in an FD
  4. The redefinition should be the same size as the field it is redefining though not all compilers require this
  5. It is possible to redefine at the group level though each group-level field does not have to the same number of elementary fields
  6. Not all compilers allow VALUE with REDEFINES. It’s a bad idea in any case
    Can have multiple REDEFINES of the same field

Recent Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.