TExcelTAD
Home › COBOL › COBOL REDEFINES

COBOL REDEFINES

REDEFINES lets two (or more) data descriptions share the same storage. Nothing is copied: the same bytes are simply viewed through a different layout. It is COBOL's equivalent of a C union, and it is used everywhere in record processing.

cobol
       01  WS-DATE-NUM            PIC 9(8).
       01  WS-DATE-PARTS REDEFINES WS-DATE-NUM.
           05  WS-YEAR            PIC 9(4).
           05  WS-MONTH           PIC 99.
           05  WS-DAY             PIC 99.

Rules: the redefining entry must directly follow the item it redefines, at the same level number, and must not be larger than it (for level 01 items GnuCOBOL allows larger, but avoid it). VALUE clauses belong on the original item, not on the redefinition.

Viewing a number as parts

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. REDEFINE-DATE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-DATE-NUM            PIC 9(8)    VALUE 20261231.
       01  WS-DATE-PARTS REDEFINES WS-DATE-NUM.
           05  WS-YEAR            PIC 9(4).
           05  WS-MONTH           PIC 99.
           05  WS-DAY             PIC 99.
       PROCEDURE DIVISION.
           DISPLAY "Year " WS-YEAR ", month " WS-MONTH ", day " WS-DAY
           MOVE 1 TO WS-DAY
           ADD 1 TO WS-YEAR
           MOVE 1 TO WS-MONTH
           DISPLAY "Next day as a number: " WS-DATE-NUM
           STOP RUN.
Year 2026, month 12, day 31
Next day as a number: 20270101

Changing WS-DAY changes the last two digits of WS-DATE-NUM, because they are the same two bytes.

Records with several layouts

Files often contain different record types in one stream: a header, many details and a trailer. A type code in the first byte tells you which layout applies. REDEFINES describes each layout over the same buffer:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. RECORD-TYPES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-RECORD              PIC X(30).
       01  WS-HEADER REDEFINES WS-RECORD.
           05  HDR-TYPE           PIC X.
           05  HDR-BATCH-DATE     PIC 9(8).
           05  FILLER             PIC X(21).
       01  WS-DETAIL REDEFINES WS-RECORD.
           05  DTL-TYPE           PIC X.
           05  DTL-ACCOUNT        PIC 9(6).
           05  DTL-AMOUNT         PIC 9(5)V99.
           05  FILLER             PIC X(16).
       01  WS-TRAILER REDEFINES WS-RECORD.
           05  TRL-TYPE           PIC X.
           05  TRL-COUNT          PIC 9(4).
           05  FILLER             PIC X(25).
       01  WS-INPUT-DATA.
           05  FILLER     PIC X(30) VALUE "H20260301".
           05  FILLER     PIC X(30) VALUE "D1000010012550".
           05  FILLER     PIC X(30) VALUE "D1000420000999".
           05  FILLER     PIC X(30) VALUE "T0002".
       01  WS-INPUT REDEFINES WS-INPUT-DATA.
           05  WS-LINE            PIC X(30) OCCURS 4 TIMES.
       01  WS-I                   PIC 9.
       PROCEDURE DIVISION.
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 4
               MOVE WS-LINE (WS-I) TO WS-RECORD
               EVALUATE HDR-TYPE
                   WHEN "H"
                       DISPLAY "Batch of " HDR-BATCH-DATE
                   WHEN "D"
                       DISPLAY "  Account " DTL-ACCOUNT
                               " amount " DTL-AMOUNT
                   WHEN "T"
                       DISPLAY "Trailer: " TRL-COUNT " details"
               END-EVALUATE
           END-PERFORM
           STOP RUN.
Batch of 20260301
  Account 100001 amount 00125.50
  Account 100042 amount 00009.99
Trailer: 0002 details

The test data uses one more REDEFINES to treat four FILLER lines as a table, the same trick as in the tables lesson.

Text and number views of the same field

Input from outside often arrives as text. A numeric REDEFINES of a text field lets you test it with IS NUMERIC and then use it as a number without a conversion:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TEXT-NUMBER.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-QTY-TEXT            PIC X(4).
       01  WS-QTY-NUM REDEFINES WS-QTY-TEXT PIC 9(4).
       01  WS-TOTAL               PIC 9(5)    VALUE 0.
       PROCEDURE DIVISION.
           MOVE "0150" TO WS-QTY-TEXT
           PERFORM ADD-QUANTITY
           MOVE "15X0" TO WS-QTY-TEXT
           PERFORM ADD-QUANTITY
           MOVE "0075" TO WS-QTY-TEXT
           PERFORM ADD-QUANTITY
           DISPLAY "Total quantity: " WS-TOTAL
           STOP RUN.
       ADD-QUANTITY.
           IF WS-QTY-TEXT IS NUMERIC
               ADD WS-QTY-NUM TO WS-TOTAL
           ELSE
               DISPLAY "Rejected quantity [" WS-QTY-TEXT "]"
           END-IF.
Rejected quantity [15X0]
Total quantity: 00225

Warning: REDEFINES does no conversion and no checking. If you read bytes through the wrong layout (a detail record through the header view, or letters through a numeric view), you get garbage or a data exception. Always check the type code or IS NUMERIC first.

Level 66 RENAMES

Less common, 66 name RENAMES a THRU b gives an alternative name to a consecutive range of fields in a record:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. RENAMES-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-ADDRESS.
           05  WS-STREET          PIC X(12)   VALUE "Main St 5".
           05  WS-CITY            PIC X(8)    VALUE "Leeds".
           05  WS-POSTCODE        PIC X(8)    VALUE "LS1 4AP".
       66  WS-CITY-AND-CODE RENAMES WS-CITY THRU WS-POSTCODE.
       PROCEDURE DIVISION.
           DISPLAY "[" WS-CITY-AND-CODE "]"
           STOP RUN.
[Leeds   LS1 4AP ]

Test yourself

What does REDEFINES do?

  1. Copies one item into another
  2. Gives the same storage a second data description
  3. Renames a paragraph
  4. Creates a new variable with the same value

Where must a REDEFINES entry be written?

  1. Anywhere in WORKING-STORAGE
  2. Immediately after the item it redefines, at the same level
  3. In the LINKAGE SECTION
  4. Before the original item

Why check IS NUMERIC before using a numeric REDEFINES of a text field?

  1. REDEFINES converts text automatically
  2. Because REDEFINES does no conversion, letters would be read as invalid digits
  3. It is required syntax
  4. To make it faster