Group and Elementary Items
Problem Description
Create a record structure with group and elementary items.
Expected Output
Hierarchical data structure
Hints
Group items contain other items. Elementary items have PIC clause.
Solution
IDENTIFICATION DIVISION.
PROGRAM-ID. GRPITEM.
DATA DIVISION.
WORKING-STORAGE SECTION.
* Level 01 - Record level (group)
01 WS-EMPLOYEE-REC.
* Level 05 - Group item (no PIC)
05 WS-EMP-NAME.
* Level 10 - Elementary items (have PIC)
10 WS-FIRST-NAME PIC X(15).
10 WS-LAST-NAME PIC X(20).
05 WS-EMP-ID PIC 9(5).
05 WS-EMP-SALARY.
10 WS-BASIC PIC 9(6)V99.
10 WS-ALLOWANCE PIC 9(5)V99.
05 WS-EMP-DOJ.
10 WS-DOJ-YEAR PIC 9(4).
10 WS-DOJ-MONTH PIC 9(2).
10 WS-DOJ-DAY PIC 9(2).
01 WS-TOTAL-SAL PIC 9(7)V99.
PROCEDURE DIVISION.
MOVE "JOHN" TO WS-FIRST-NAME.
MOVE "SMITH" TO WS-LAST-NAME.
MOVE 10001 TO WS-EMP-ID.
MOVE 50000.00 TO WS-BASIC.
MOVE 5000.00 TO WS-ALLOWANCE.
MOVE 2020 TO WS-DOJ-YEAR.
MOVE 01 TO WS-DOJ-MONTH.
MOVE 15 TO WS-DOJ-DAY.
COMPUTE WS-TOTAL-SAL = WS-BASIC + WS-ALLOWANCE.
DISPLAY "NAME: " WS-EMP-NAME.
DISPLAY "ID: " WS-EMP-ID.
DISPLAY "SALARY: " WS-TOTAL-SAL.
DISPLAY "DOJ: " WS-EMP-DOJ.
* Access entire record as one field
DISPLAY "FULL RECORD: " WS-EMPLOYEE-REC.
STOP RUN.
Explanation:
Group items (no PIC) contain other items. Elementary items (have PIC) hold data.