MainframeMaster

COBOL Data Structures

Data structures in COBOL are how you describe records, groups of fields, arrays, and alternate views of the same storage. This page covers level numbers, group and elementary items, OCCURS (tables), REDEFINES, and 88 condition names so you can design clear, maintainable data layouts.

Explain Like I'm Five: What Are Data Structures?

Think of a form: the whole form is one big box (level 01). Inside it you have sections like "Name" and "Address." Each section is a smaller box (level 05). Inside "Name" you might have "First" and "Last" (level 10). The smallest boxes that you actually fill in (like one line of text) are elementary items—they have a PICTURE. Sometimes the same box is used in two ways: for example, one number stored as four digits or as four separate characters—that’s REDEFINES. And when you have a list of the same kind of thing (like 12 months), that’s OCCURS.

Level Numbers

Level numbers define hierarchy. Level 01 is the top of a record or structure. Numbers 02 through 49 are subordinates; a higher number is deeper in the tree. The same number can be used for siblings (e.g. several 05s under one 01). Special levels 66, 77, 88, and 78 have specific roles and do not follow the same hierarchy.

Level numbers and their roles
LevelPurposeNotes
01Start of a record or top-level group.Every record or major structure begins with 01.
02-49Subordinate items within a group.Lower numbers are higher in the hierarchy; 02 is under 01, 05 under 02, etc.
66RENAMES: regroup existing items under a new name.Used to give an alternate grouping of existing fields.
77Standalone elementary item (no hierarchy).No children; often used for simple working-storage variables.
88Condition name for specific values of the parent.No PICTURE; used in IF or SET ... TO TRUE.
78Constant name (compile-time constant).Defines a named constant value.

Group Items vs Elementary Items

A group item has a level number 01-49 and contains subordinate items. It does not have a PICTURE clause. Its length is the sum of its subordinates. You can MOVE a group as a whole, or reference any subordinate. An elementary item is a leaf: it has a PICTURE (and possibly USAGE, VALUE, etc.) and cannot be subdivided. Only elementary items represent actual data with a defined type and length.

cobol
1
2
3
4
5
6
01 CUSTOMER-RECORD. 05 CUSTOMER-NAME. *> Group item (no PICTURE) 10 FIRST-NAME PIC X(15). *> Elementary 10 LAST-NAME PIC X(20). *> Elementary 05 CUSTOMER-ID PIC 9(6). *> Elementary 05 BALANCE PIC 9(9)V99. *> Elementary

Here CUSTOMER-RECORD and CUSTOMER-NAME are group items. FIRST-NAME, LAST-NAME, CUSTOMER-ID, and BALANCE are elementary. Moving CUSTOMER-NAME moves all 35 characters (first + last). Moving CUSTOMER-ID moves only the 6-digit numeric field.

OCCURS: Tables and Repeated Structures

OCCURS defines a repeated structure—an array or table. The clause OCCURS n TIMES means the item is repeated n times. You reference one occurrence with a subscript (1-based in COBOL) or an index. OCCURS DEPENDING ON makes the number of occurrences variable at runtime, with a depending field controlling the count.

cobol
1
2
3
4
5
6
7
8
9
10
01 SALES-YEAR. 05 MONTHLY-SALES OCCURS 12 TIMES INDEXED BY IDX. 10 MONTH-AMOUNT PIC 9(7)V99. 10 MONTH-NAME PIC X(3). *> Reference: MONTHLY-SALES(3) or use IDX MOVE 1000.50 TO MONTH-AMOUNT(1) SET IDX TO 5 ADD 100 TO MONTH-AMOUNT(IDX)

OCCURS 12 TIMES creates 12 occurrences of the group; each has MONTH-AMOUNT and MONTH-NAME. Subscripts are in parentheses: MONTH-AMOUNT(1) through MONTH-AMOUNT(12). INDEXED BY declares an index you can SET and use in place of a subscript for efficient access. An item with OCCURS cannot be directly redefined; if you need both OCCURS and REDEFINES, put OCCURS in a group and REDEFINES the group.

REDEFINES: Alternate Views of the Same Storage

REDEFINES lets you describe the same bytes in more than one way. The redefining item must immediately follow the redefined item and be at the same level. Only one description is meaningful at a time—whichever you last wrote to. Common uses: treat four bytes as a binary fullword or as four characters; or overlay a numeric field with an alphanumeric for editing.

cobol
1
2
3
4
5
6
7
8
9
10
01 PACKED-DATA. 05 BINARY-VALUE PIC S9(9) COMP. 05 CHAR-VIEW PIC X(4) REDEFINES BINARY-VALUE. 01 DATE-FIELD. 05 DATE-YYYYMMDD PIC 9(8). 05 DATE-PARTS REDEFINES DATE-YYYYMMDD. 10 DATE-YYYY PIC 9(4). 10 DATE-MM PIC 9(2). 10 DATE-DD PIC 9(2).

BINARY-VALUE and CHAR-VIEW share the same four bytes. DATE-YYYYMMDD is 8 digits; DATE-PARTS overlays it so you can access year, month, and day separately. The redefined item cannot contain OCCURS. Both items share storage; changing one changes the other.

88 Condition Names

Level 88 defines a condition name: a meaningful name for one or more values of the parent data item. You use it in IF conditions (e.g. IF VALID-STATUS) or SET ... TO TRUE to assign the first value of that condition to the parent. This makes code clearer than comparing literals.

cobol
1
2
3
4
5
6
7
8
9
01 STATUS-CODE PIC X. 88 ACTIVE VALUE 'A'. 88 INACTIVE VALUE 'I'. 88 PENDING VALUE 'P'. 01 AMOUNT-TIER PIC 9(3). 88 SMALL VALUE 0 THRU 99. 88 MEDIUM VALUE 100 THRU 499. 88 LARGE VALUE 500 THRU 999.

In procedure code you write IF ACTIVE or IF SMALL instead of IF STATUS-CODE = 'A' or IF AMOUNT-TIER < 100. SET ACTIVE TO TRUE moves 'A' to STATUS-CODE. You can list multiple values (e.g. VALUE 'A' 'B' 'C') or a range with THRU. The 88 item has no PICTURE; it inherits the parent’s length and type.

Step-by-Step: Defining a Record with Groups and 88s

  1. Start with 01 and a name for the whole record or structure.
  2. Add subordinate levels (05, 10, etc.) for each logical group; group items have no PICTURE.
  3. Under each group, add elementary items with PICTURE (and USAGE if needed).
  4. Where a field has a fixed set of meanings, add 88 condition names under it with VALUE (and THRU for ranges).
  5. Use meaningful names so that IF and SET statements read clearly.

Step-by-Step: Adding an OCCURS Table

  1. Decide the maximum number of occurrences (or use OCCURS DEPENDING ON with a depending field).
  2. Define a group or elementary item and add OCCURS n TIMES (or OCCURS ... DEPENDING ON field).
  3. Add INDEXED BY index-name if you will use an index for performance or iteration.
  4. Reference elements with (subscript) or (index); subscripts are 1-based.
  5. If you need to REDEFINES the table, wrap the OCCURS in a group and redefine the group, not the OCCURS item.

RENAMES (Level 66)

Level 66 RENAMES regroups a range of existing elementary items under one name. The range is from one item through another. This does not create new storage; it gives an alternate name for a slice of the record. Useful for report lines or when the same bytes are logically part of more than one group.

cobol
1
2
3
4
5
6
01 DETAIL-LINE. 05 FILLER PIC X(5). 05 CUST-ID PIC 9(6). 05 FILLER PIC X(3). 05 CUST-NAME PIC X(30). 66 PRINT-AREA RENAMES CUST-ID THRU CUST-NAME.

PRINT-AREA refers to the 39-character area from CUST-ID through CUST-NAME. You might MOVE PRINT-AREA TO a report line or use it for display.

Best Practices

Test Your Knowledge

Test Your Knowledge

1. Which level number starts a new record or top-level group?

  • 05
  • 77
  • 01
  • 88

2. An item with OCCURS 10 TIMES and no PICTURE is a:

  • Group array
  • Elementary array
  • Condition name
  • REDEFINES

3. How do you assign a value using an 88 condition name?

  • MOVE 88-name TO field
  • SET 88-name TO TRUE
  • VALUE 88-name
  • 88-name = value