COBOL Table Declaration: How to Use OCCURS for Data Arrays ????
1?? What is OCCURS in COBOL?
The OCCURS clause defines a repeating group of fields within a structure, allowing you to store multiple values under a single table name. Instead of creating separate variables (ITEM-1, ITEM-2, ITEM-3...), you define a single item with OCCURS.
?? Basic Syntax:
01 DATA-TABLE.
05 ITEM PIC X(10) OCCURS 5 TIMES.
Here, ITEM is an array of 5 elements, each holding 10 characters.
2?? Example: Storing 5 Customer Names in an Array
IDENTIFICATION DIVISION.
PROGRAM-ID. CUSTOMER-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER-NAMES.
05 CUSTOMER PIC X(20) OCCURS 5 TIMES.
PROCEDURE DIVISION.
MOVE 'John Doe' TO CUSTOMER(1).
MOVE 'Alice Smith' TO CUSTOMER(2).
MOVE 'Bob Johnson' TO CUSTOMER(3).
DISPLAY "First Customer: " CUSTOMER(1).
DISPLAY "Second Customer: " CUSTOMER(2).
STOP RUN.
?? Output:
First Customer: John Doe
Second Customer: Alice Smith
? Pros: Cleaner and more maintainable than declaring separate variables.
3?? Accessing OCCURS with PERFORM (Loops)
To process all elements in a table, use a PERFORM loop with an index:
WORKING-STORAGE SECTION.
01 CUSTOMER-TABLE.
05 CUSTOMER PIC X(20) OCCURS 5 TIMES INDEXED BY CUSTOMER-IDX.
PROCEDURE DIVISION.
PERFORM VARYING CUSTOMER-IDX FROM 1 BY 1 UNTIL CUSTOMER-IDX > 5
DISPLAY "Customer " CUSTOMER-IDX ": " CUSTOMER(CUSTOMER-IDX)
END-PERFORM.
? Best Use Case: When you need to iterate over an unknown number of records.
4?? OCCURS with DEPENDING ON – Dynamic Arrays
Want to define a flexible table size at runtime? Use OCCURS DEPENDING ON:
01 MAX-CUSTOMERS PIC 9(2) VALUE 3.
01 CUSTOMER-NAMES.
05 CUSTOMER PIC X(20) OCCURS 1 TO 10 TIMES DEPENDING ON MAX-CUSTOMERS.
?? Here, the array size can be changed dynamically based on MAX-CUSTOMERS.
5?? OCCURS with MULTIPLE LEVELS – 2D Tables
You can nest OCCURS inside OCCURS to create multi-dimensional tables.
?? Example: Storing 5 customers, each with 3 transaction amounts
01 CUSTOMER-DATA.
05 CUSTOMER OCCURS 5 TIMES. *> 5 Customers (1st dimension)
10 NAME PIC X(20).
10 TRANSACTIONS PIC 9(6)V99 OCCURS 3 TIMES. *> 3 Transactions per Customer (2nd dimension)
To access a specific transaction of a customer:
MOVE "John Doe" TO NAME(1). *> Customer 1
MOVE 1250.75 TO TRANSACTIONS(1,1). *> First transaction of first customer
MOVE 890.50 TO TRANSACTIONS(1,2). *> Second transaction of first customer
MOVE "Alice Smith" TO NAME(2). *> Customer 2
MOVE 3000.00 TO TRANSACTIONS(2,1). *> First transaction of second customer
? Best Use Case: When you need to store structured data like customer orders, sales, or matrix-based calculations.
6?? OCCURS vs. Indexed Files – When to Use?
?? Rule of Thumb:
7?? Key Takeaways
?? OCCURS simplifies handling multiple data items in a structured way.
?? PERFORM VARYING helps iterate over table elements efficiently.
?? OCCURS DEPENDING ON allows dynamic sizing.
?? Nested OCCURS enables multi-dimensional data storage.
Senior Front-end Developer | React - NextJS - Typescript - NodeJS - AWS
1 天前Great Content! Thanks!
Cobolskolan Sverige | Professionell utbildning f?r blivande COBOL-proffs | w ww.cobolskolan.se
1 天前Some notes: No 3: It is not an index, it is a subscript. Index is different. No 4: The size never changes (always max 10). Depending on tells COBOL how many elements COBOL uses when using COBOL Search. No 5: The table is not declared as a 2D correctly. By the way, its called a TABLE in COBOL, not Array ??
Senior Software Engineer | Java | Spring Boot | React | Angular | AWS | APIs
1 天前Very Nice Post!
Fullstack Software Engineer | Node | Typescript | React | Next.js | AWS | Tailwind | NestJS | TDD | Docker
2 天前Good to know. Thanks for sharing!
Mainframe Developer | COBOL CICS DB2 VSAM | NATURAL ADABAS | DevOps | Card Processing | Banking Systems
2 天前Great explanation. About OCCURS DEPENDING ON, it's a great resource for improve performance in online transactions.