Skip to main content
eLearner.app
Module 4 · Lesson 1 of 27/10 in the course~12 min
Module lessons (1/2)

Group Items and Records

In COBOL, elementary variables can be grouped together into a hierarchical structure called a group item or record. This allows you to treat a set of related data either as a single entity (for example, to copy or display them) or as individual fields.

Level Numbers and Hierarchical Relations

To define a record, you use level numbers in the DATA DIVISION:

  • Level 01: Defines the main record (the group name). It does not have its own PICTURE clause because its size is determined by the sum of its internal fields.
  • Subordinate levels (05, 10, 15, etc.): Identify the subordinate fields belonging to the higher-level group. They can be elementary variables (with a PIC clause) or, in turn, other group items (sub-records).
Code
01 WS-EMPLOYEE-RECORD.
    05 WS-EMP-ID       PIC 9(5).
    05 WS-EMP-NAME     PIC X(20).
    05 WS-EMP-SALARY   PIC 9(6)V99.

In this example, WS-EMPLOYEE-RECORD is a group structure containing three subordinate fields. If we reference WS-EMPLOYEE-RECORD, we read or write all 5 + 20 + 8 = 33 characters of the record at once.

The MOVE Statement and Group Items

We can assign values to individual elementary fields:

Code
MOVE 10023 TO WS-EMP-ID.
MOVE "JOHN DOE" TO WS-EMP-NAME.

Or we can copy an entire record into another compatible record in a single operation:

Code
MOVE WS-EMPLOYEE-RECORD TO WS-EMPLOYEE-BACKUP.

Try it yourself

Exercise#cobol.m4.l1.e1
Attempts: 0Loading…

Declare a group structure 01 named WS-STUDENT-RECORD containing three sub-fields of level 05: WS-STUDENT-ID (numeric of 5 digits), WS-STUDENT-NAME (alphanumeric of 15 characters), and WS-STUDENT-GPA (numeric with 1 integer digit and 2 decimals).

Loading editor…
Show hint

Use the syntax: 01 WS-STUDENT-RECORD. (with a period and no PIC), followed on the next lines by level 05 WS-STUDENT-ID PIC 9(5)., 05 WS-STUDENT-NAME PIC X(15). and 05 WS-STUDENT-GPA PIC 9(1)V99.

Solution available after 3 attempts

Exercise#cobol.m4.l1.e2
Attempts: 0Loading…

Complete the PROCEDURE DIVISION by moving the value 10023 into WS-EMP-ID, the text 'JOHN DOE' into WS-EMP-NAME, and finally display the group structure WS-EMPLOYEE-RECORD using DISPLAY.

Loading editor…
Show hint

Use MOVE 10023 TO WS-EMP-ID. and MOVE 'JOHN DOE' TO WS-EMP-NAME. to assign values, then write DISPLAY WS-EMPLOYEE-RECORD. and finally STOP RUN.

Solution available after 3 attempts