Summary -

COBOL uses OCCURES clause to define a table/Array in the programming structure. OCCURES clause specifies the number that represents how many times the data item repeated in the table.

There are two parts in the table/array i.e., first one is the table name and the second one is data item which repeats in the table. Table name should be declared with 01 level number and should not have OCCURES clause associated with it.

Data item always declared as elementary item and can be declared with level numbers from 02 to 49. Data item always declared with PICTURE clause but may or may not with OCCURES clause associated with it.

But sometimes both can declare at different levels where OCCURS clause should be at higher level than PICTURE clause. The OCCURS clause specifies tables where elements can be referred by indexing or subscripting.

The OCCURS clause cannot be specified with level number of 01, 66, 77 or 88 and redefined data item. Each OCCURS clause specified is called as a dimension and up to 7 dimensions can be specified in a table.

Note! Six nested levels OCCURS and one outermost OCCURS clause are allowed.

How many types of tables?

COBOL supports two types of tables based on declaration –

  • Fixed-length tables - Specifies the number of OCCURS statically/directly. The table defines with the number of OCCURS specified with a static value while writing the program.

    FIXED-LENGTH Tables Syntax

  • Variable-length tables - Specifies the number of OCCURS dynamically. The number of occurrences value specified while running the program.


How the table/Array Can be defined?

Before defining the table in COBOL, identify the below things from the requirement -

  1. Identify the table hierarchy structure.
  2. Identify the occurrences in the table structure.
  3. Declare the table according to the occurrences.

Let’s take a table structure with one row and five columns to see how it defines in program.

Structure Example

Step-1: - For the above table, the hierarchy is -

Example Structure

Step-2: - The ROW data has 4 occurrences. So, the ROW should be declared as OCCUS 4 TIMES.
The COLUMN has the occurrences of 5 and should be declared as OCCURS 5 TIMES.

Step-3: - Based on the above specification, the declaration would be -

 01 TABLE.
     02 ROW.
          03 COLUMN PIC  X(05) OCCURES 5 TIMES.

Note! To understand the concept clearly, we have used ROW and COLUMNS as a dimension. In real-time, those dimensions can be anything based on the requirement.

Example -

Scenario – capture the Btech first year student marks for 6 subjects. Follow the above steps to define the array for the requirement.

Step-1: - Hierarchy -

Example hierarchy

Step-2: - STUDENT are OCCURS 60 TIMES as the total students are 60.The SUBJECT-MARKS are OCCURS 6 TIMES as the total subjects are 6.The maximum marks are 100. So, it should define with PICTURE clause 9(3).

Step-3: - The final declaration is -

     02 STUDENT.
          03 SUBJECT-MARKS  PIC 9(03) OCCURES 6 TIMES.

The last OCCURS clause come up with PICTURE clause of the data item. If you want to separate the PICTURE clause from the OCCURS, the above declaration is equal to the below –

     02 STUDENT.
          03 SUBJECT  OCCURES 6 TIMES.
            04 MARKS  PIC 9(03).

Note! If the item repeated more than once, the OCCURS clause required to specify. No OCCURS clause required to specify for singe occurrence.

For example, the above declaration for 60 students is –

          03 SUBJECT-MARKS  PIC 9(03) OCCURES 6 TIMES.

How the data stored in memory for table OCCURES?

The table occurs generally stores in subsequent memory locations. Use the same table declaration for one student to understand how the data stored in memory.

     02 STUDENT.
          03 SUBJECT-MARKS  PIC 9(03) OCCURES 6 TIMES.

The below diagram shows the memory storage, if they are stored in subsequent memory locations.

Memory Storage

In the above diagram, OCCURRENCES represent the how many times the data repeated. A memory position (item level) represents the OCCURRENCE level memory representation because each occurrence occupies 3 bytes according to the declaration. Memory positions (Byte level) represent each byte level memory representation.

Practical Example -

Scenario - Below example describes how the occurs clause used in COBOL programming.

Code -

Occurs Clause program Code
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. TOCCURS.                                             
       AUTHOR. MTH.                                                     
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
       01 WS-STUDENT.                                                   
          05 WS-MARKS       PIC 9(03) OCCURS 6 TIMES.                   
          05 WS-TOTAL-MARKS PIC 9(03) VALUE ZEROES.                     
       01 WS-I              PIC 9(02) VALUE 01.                         
       PROCEDURE DIVISION.                                              
           PERFORM UNTIL WS-I > 6                                       
               ACCEPT WS-MARKS (WS-I)                                   
               COMPUTE WS-I = WS-I + 1                                  
           DISPLAY "STUDENT TOTAL MARKS: " WS-TOTAL-MARKS.              
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Run Jcl -

Occurs Clause Program Run Jcl

Output -

Occurs Clause Program Output