MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Data Organization

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.

What is Data Organization?

Data organization in COBOL involves:

  • Hierarchical Structures: Organizing data in parent-child relationships
  • Group Items: Containers that hold related data items
  • Elementary Items: Actual data fields with values
  • Level Numbers: Indicating hierarchy levels
  • Arrays and Tables: Repeating data structures with OCCURS

Level Numbers

Level numbers (01-49, 66, 77, 88) indicate the hierarchical level of data items:

  • 01: Record level (highest level)
  • 02-49: Subordinate levels (lower numbers = higher hierarchy)
  • 66: RENAMES clause
  • 77: Independent data items
  • 88: Condition names

Group Items vs Elementary Items

Group Items vs Elementary Items
AspectGroup ItemsElementary Items
Contains Other ItemsYesNo
Has PICTURE ClauseNoYes
Stores ValuesNo (organizational)Yes (actual data)
Level Numbers01-4902-49
PurposeOrganization and structureData storage

Hierarchical Data Organization Example

cobol
1
2
3
4
5
6
7
8
9
10
WORKING-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.

OCCURS Clause for Arrays

cobol
1
2
3
4
5
6
7
WORKING-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.

Referencing Hierarchical Data

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PROCEDURE 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.

Complete Data Organization Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
IDENTIFICATION 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.

Best Practices for Data Organization

  • Use meaningful level numbers: Consistent numbering (01, 05, 10, 15, etc.)
  • Group related fields: Organize data logically by business entity
  • Use descriptive names: Clear, meaningful names for all items
  • Match business structure: Organize data to match business relationships
  • Keep hierarchies reasonable: Avoid excessive nesting depth
  • Document complex structures: Comment complex data organizations
  • Use OCCURS for repeating data: Arrays for lists and tables

Explain Like I'm 5: Data Organization

Think of data organization like organizing a filing cabinet:

  • Level 01 is like a drawer (CUSTOMER-RECORD)
  • Level 05 is like a folder inside the drawer (ADDRESS)
  • Level 10 is like papers in the folder (STREET, CITY)
  • OCCURS is like having multiple copies of the same folder

So data organization is like organizing information in a filing system - everything has its place in a hierarchy!

Test Your Knowledge

1. What are group items in COBOL?

  • Data items that contain other data items
  • Data items with PICTURE clauses
  • Independent data items
  • Condition names

2. What are elementary items?

  • Items that contain other items
  • The actual data fields with PICTURE clauses
  • Level 01 items
  • Arrays

3. What do level numbers indicate?

  • Data size
  • Hierarchical level in data structure
  • Array size
  • Memory address

4. What is the OCCURS clause used for?

  • Defining groups
  • Creating arrays or repeating data structures
  • Defining conditions
  • Renaming items

Related Concepts

Related Pages