Data organization in COBOL refers to how data is structured and organized within programs using hierarchical data definitions. COBOL's powerful data organization capabilities allow you to create complex data structures that match business entities, organize related fields together, and create maintainable, readable data definitions. Understanding data organization is fundamental to effective COBOL programming.
This comprehensive guide will teach you how to organize data using level numbers, create hierarchical structures with group and elementary items, use OCCURS for arrays, reference data in hierarchies, and follow best practices for effective data organization. Whether you're defining record structures, organizing business data, or creating complex data hierarchies, mastering data organization is essential for COBOL programming.
Data organization in COBOL involves:
Level numbers (01-49, 66, 77, 88) indicate the hierarchical level of data items:
| Aspect | Group Items | Elementary Items |
|---|---|---|
| Contains Other Items | Yes | No |
| Has PICTURE Clause | No | Yes |
| Stores Values | No (organizational) | Yes (actual data) |
| Level Numbers | 01-49 | 02-49 |
| Purpose | Organization and structure | Data storage |
12345678910WORKING-STORAGE SECTION. 01 CUSTOMER-RECORD. *> Group item (level 01) 05 CUSTOMER-ID PIC 9(8). *> Elementary item 05 CUSTOMER-NAME PIC X(30). *> Elementary item 05 ADDRESS. *> Group item (level 05) 10 STREET PIC X(50). *> Elementary item 10 CITY PIC X(30). *> Elementary item 10 STATE PIC X(2). *> Elementary item 10 ZIP-CODE PIC 9(5). *> Elementary item 05 ACCOUNT-BALANCE PIC S9(8)V99. *> Elementary item
This example shows hierarchical organization: CUSTOMER-RECORD contains CUSTOMER-ID, CUSTOMER-NAME, ADDRESS group, and ACCOUNT-BALANCE. ADDRESS group contains STREET, CITY, STATE, and ZIP-CODE.
1234567WORKING-STORAGE SECTION. 01 CUSTOMER-TABLE. 05 CUSTOMER-ENTRY OCCURS 100 TIMES INDEXED BY CUST-INDEX. 10 CUST-ID PIC 9(8). 10 CUST-NAME PIC X(30). 10 CUST-BALANCE PIC S9(8)V99.
OCCURS creates arrays. This example creates an array of 100 customer entries, each with ID, name, and balance.
123456789101112131415161718PROCEDURE DIVISION. MAIN-LOGIC. *> Reference elementary items directly MOVE 12345678 TO CUSTOMER-ID MOVE 'JOHN SMITH' TO CUSTOMER-NAME *> Reference items in groups MOVE '123 MAIN ST' TO STREET MOVE 'NEW YORK' TO CITY *> Reference using qualified names (if needed) MOVE 'NY' TO STATE OF ADDRESS *> Reference array elements MOVE 1 TO CUST-INDEX MOVE 87654321 TO CUST-ID(CUST-INDEX) STOP RUN.
You can reference items directly if names are unique, or use qualified names for clarity or to avoid conflicts.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758IDENTIFICATION DIVISION. PROGRAM-ID. DATA-ORGANIZATION-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-RECORD. 05 EMP-ID PIC 9(8). 05 EMP-NAME PIC X(30). 05 EMP-DEPARTMENT PIC X(20). 05 SALARY-INFO. 10 BASE-SALARY PIC 9(7)V99. 10 BONUS PIC 9(6)V99. 10 TOTAL-COMP PIC 9(8)V99. 05 ADDRESS-INFO. 10 STREET PIC X(50). 10 CITY PIC X(30). 10 STATE PIC X(2). 10 ZIP PIC 9(5). 01 EMPLOYEE-TABLE. 05 EMP-ENTRY OCCURS 50 TIMES INDEXED BY EMP-INDEX. 10 TABLE-EMP-ID PIC 9(8). 10 TABLE-EMP-NAME PIC X(30). 10 TABLE-SALARY PIC 9(7)V99. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Data Organization Example ===" *> Populate hierarchical record MOVE 12345678 TO EMP-ID MOVE 'JOHN SMITH' TO EMP-NAME MOVE 'ENGINEERING' TO EMP-DEPARTMENT MOVE 75000.00 TO BASE-SALARY MOVE 5000.00 TO BONUS COMPUTE TOTAL-COMP = BASE-SALARY + BONUS MOVE '123 TECH ST' TO STREET MOVE 'SAN FRANCISCO' TO CITY MOVE 'CA' TO STATE MOVE 94102 TO ZIP DISPLAY "Employee ID: " EMP-ID DISPLAY "Name: " EMP-NAME DISPLAY "Department: " EMP-DEPARTMENT DISPLAY "Total Compensation: $" TOTAL-COMP DISPLAY "Address: " STREET ", " CITY ", " STATE " " ZIP *> Populate array MOVE 1 TO EMP-INDEX MOVE 87654321 TO TABLE-EMP-ID(EMP-INDEX) MOVE 'JANE DOE' TO TABLE-EMP-NAME(EMP-INDEX) MOVE 80000.00 TO TABLE-SALARY(EMP-INDEX) DISPLAY "Array element 1: " TABLE-EMP-ID(EMP-INDEX) " " TABLE-EMP-NAME(EMP-INDEX) STOP RUN.
This complete example demonstrates hierarchical data organization with group items, elementary items, and arrays.
Think of data organization like organizing a filing cabinet:
So data organization is like organizing information in a filing system - everything has its place in a hierarchy!
1. What are group items in COBOL?
2. What are elementary items?
3. What do level numbers indicate?
4. What is the OCCURS clause used for?