The REDEFINES Clause
The REDEFINES clause lets a second data item share the exact same storage bytes as a previously defined item at the same level, giving a program two different interpretations of one physical location in memory without allocating additional space. Written as 05 WS-DATE-NUMERIC REDEFINES WS-DATE-TEXT PIC 9(8), the redefining item must appear immediately after the item it redefines, at the same level number, and its total size in bytes normally should not exceed the original item's size, though COBOL permits it to be smaller. REDEFINES is fundamentally different from a group item's subordinate breakdown, since a redefined item does not add new bytes to the record; it is purely an alternate lens for viewing storage that already exists.
Cricket analogy: REDEFINES is like reading the exact same over of play from two different scorers' notation systems, one tallying balls faced and one tallying dot balls, both describing the identical physical over without adding a new over to the match.
Practical Uses of REDEFINES
One of the most common production uses of REDEFINES is splitting a single incoming field into differently typed views for validation or arithmetic, such as receiving an 8-character date as PIC X(8) from an external file, then REDEFINING it with a numeric picture broken into year, month, and day subfields so PROCEDURE DIVISION can validate that the month falls between 01 and 12 using numeric comparisons rather than string parsing. REDEFINES is also used to build tagged unions of record types in a single file, where an initial classification field determines which of several REDEFINED layouts applies to the rest of the record, letting one FD handle multiple record formats, for example a transaction file mixing PAYMENT and REFUND records that share a common header but differ in their trailing fields.
Cricket analogy: Splitting a date field with REDEFINES for validation is like a third umpire reviewing the same raw ball-tracking data through both a stump-cam angle and a Hawk-Eye trajectory view before ruling on an LBW.
The OCCURS Clause for Tables
The OCCURS clause turns a data item into an array, called a table in COBOL terminology, by repeating its definition a fixed number of times, as in 05 WS-MONTHLY-SALES OCCURS 12 TIMES PIC 9(7)V99, which creates twelve consecutive numeric fields accessed by subscript like WS-MONTHLY-SALES(3). OCCURS can also apply to a group item, in which case every subordinate field is likewise repeated for each occurrence, letting a single 01-level record represent, for example, a table of twenty employee sub-records each containing name, ID, and salary. For tables whose actual length varies at runtime, OCCURS DEPENDING ON ties the table's active size to a separate counter field, so OCCURS 1 TO 50 TIMES DEPENDING ON WS-ITEM-COUNT lets a program process anywhere from 1 to 50 real entries without wasting space or risking an out-of-bounds reference beyond the counted portion.
Cricket analogy: OCCURS 12 TIMES building a monthly-sales table is like a season's fixture list with a fixed twelve-match slot, each match accessed by its match number in the schedule.
01 WS-INCOMING-DATE.
05 WS-DATE-TEXT PIC X(8).
01 WS-DATE-NUMERIC REDEFINES WS-INCOMING-DATE.
05 WS-DATE-YEAR PIC 9(4).
05 WS-DATE-MONTH PIC 9(2).
05 WS-DATE-DAY PIC 9(2).
01 WS-SALES-TABLE.
05 WS-MONTHLY-SALES OCCURS 12 TIMES
INDEXED BY WS-MONTH-IDX
PIC 9(7)V99.
01 WS-ITEM-COUNT PIC 9(3) VALUE ZERO.
01 WS-LINE-ITEMS.
05 WS-LINE-ITEM OCCURS 1 TO 50 TIMES
DEPENDING ON WS-ITEM-COUNT
INDEXED BY WS-LINE-IDX.
10 WS-ITEM-SKU PIC X(10).
10 WS-ITEM-QTY PIC 9(3).
PROCEDURE DIVISION.
IF WS-DATE-MONTH < 1 OR WS-DATE-MONTH > 12
DISPLAY 'INVALID MONTH IN DATE'
END-IF
PERFORM VARYING WS-MONTH-IDX FROM 1 BY 1
UNTIL WS-MONTH-IDX > 12
ADD WS-MONTHLY-SALES(WS-MONTH-IDX) TO WS-TOTAL-SALES
END-PERFORM.Indexing and Subscripting
A table's entries can be accessed with either a numeric subscript, an ordinary WORKING-STORAGE field like WS-MONTH-IDX PIC 9(2), or with a proper COBOL index declared via INDEXED BY, which is a compiler-managed occurrence displacement rather than an ordinary integer, making index-based access noticeably faster than subscript-based access since the compiler avoids recomputing the byte offset from scratch on every reference. The SEARCH statement performs a linear scan of an indexed table looking for a matching WHEN condition, while SEARCH ALL performs a binary search but requires the table's contents to be sorted in ascending or descending order as declared with the ASCENDING KEY or DESCENDING KEY clause on the OCCURS entry, making SEARCH ALL dramatically faster for large tables at the cost of requiring pre-sorted data.
Cricket analogy: SEARCH ALL requiring a sorted table is like using a scorebook's alphabetical player index to jump straight to a name, which only works because the names were filed alphabetically in the first place, unlike flipping page by page through a match-by-match log.
PERFORM VARYING combined with a numeric subscript is the most common way to walk a table sequentially, while SEARCH and SEARCH ALL are built-in COBOL verbs specifically for locating a matching entry, and SEARCH ALL's binary search only produces correct results if the table was genuinely populated in the declared key order beforehand.
A REDEFINES item whose total size exceeds the original item it redefines can read or write past the intended storage boundary into whatever field follows in the record, silently corrupting adjacent data. Always verify byte sizes match, especially after any PICTURE clause is edited during maintenance.
- REDEFINES lets a second data item share the exact same storage as a prior item at the same level, without allocating new bytes.
- REDEFINES is commonly used to reinterpret an incoming field's type or to build tagged-union record layouts sharing a common header.
- OCCURS repeats a data item or group item a fixed number of times to build a table, accessed by subscript or index.
- OCCURS DEPENDING ON creates a variable-length table whose active size is tied to a separate counter field.
- INDEXED BY declares a compiler-managed index that is faster than an ordinary numeric subscript for table access.
- SEARCH performs a linear scan; SEARCH ALL performs a binary search but requires the table to be pre-sorted by its declared key.
- A REDEFINES item larger than the original can silently corrupt adjacent storage; sizes must always be verified.
Practice what you learned
1. What does the REDEFINES clause do to a data item's storage?
2. What is the primary purpose of the OCCURS clause?
3. What does OCCURS DEPENDING ON accomplish that a plain OCCURS n TIMES cannot?
4. What precondition must be true for SEARCH ALL to produce correct results?
5. What risk arises when a REDEFINES item's total size exceeds the original item it redefines?
Was this page helpful?
You May Also Like
Level Numbers and Group Items
How COBOL uses level numbers from 01 to 49, plus the special 66, 77, and 88 levels, to build hierarchical record structures out of group and elementary items.
PICTURE Clauses Explained
A detailed guide to COBOL's PICTURE (PIC) clause, the syntax that defines a data item's category, size, and display format.
Data Division Basics
An introduction to the COBOL Data Division, the section of a COBOL program where every variable, file layout, and record structure is declared before any executable logic runs.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics