In some scenarios, identical data items required to define multiple times with multiple names and the process creates confusion to identify the identical data items. For example, capture the Btech first year student marks for all 6 subjects. The declaration would be -

01 BTECH-1ST-YEAR.
     03 SUBJECT1-MARKS    PIC 9(03).
     03 SUBJECT2-MARKS    PIC 9(03).
     03 SUBJECT3-MARKS    PIC 9(03).
     03 SUBJECT4-MARKS    PIC 9(03).
     03 SUBJECT5-MARKS    PIC 9(03).
     03 SUBJECT6-MARKS    PIC 9(03).

To avoid this, COBOL supports tables concept to deal with same set of data items. Tables concept reduces the need of declaring each item separately.

Table/Array having a set of data items(elements) that are having same data type and length. Each element should have the same data definition as the other element in the set.

These tables/arrays are like the common data tables that are represented as rows and columns. Array/Table concept also follows the same way to refer the data from the table.

The repeated data items can be declared by using the OCCURS clause in COBOL. The above example declaration with table concept is –

01 BTECH-1ST-YEAR.
     03 SUBJECT-MARKS  PIC 9(03) OCCURES 6 TIMES.

OCCURES Clause -

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.
  • 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 OCCURS 4 TIMES.
          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.

Let’s use some example to understand the above concept clearly.

The requirement is – capture the Btech first year 60 students 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 -

01 BTECH-1ST-YEAR.
     02 STUDENT OCCURS 60 TIMES.
          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 –

01 BTECH-1ST-YEAR.
     02 STUDENT OCCURS 60 TIMES.
          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 one student is –

01 BTECH-1ST-YEAR.
     02 STUDENT.
          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.

01 BTECH-1ST-YEAR.
     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.

How the table accessed in the program?

There are two ways to access the table in the program to process the data –

  • Subscript - Subscript is a number of occurrences of array element. Literal or a data-name can use as a subscript.
  • Index - Index is the number of displacement positions. Index can be created by using the INDEXED BY phrase of the OCCURS clause and the same used to identify an index-name.

Single Dimentional array example -

Example to use an array to calculate percentage of student.

Code -


 IDENTIFICATION DIVISION.                                        
 PROGRAM-ID. PERFTIMI.                                           
 ENVIRONMENT DIVISION.                                           
 DATA DIVISION.                                                  
 WORKING-STORAGE SECTION.                                        
 01 STD-DET OCCURES 6 TIMES INDEXED BY STD-INDEX.                
    05 STD-MARKS              PIC 9(03).                         
 01 TOTAL-MARKS               PIC 9(03) VALUE ZERO.              
 01 STD-PERCENT               PIC 9(03).9(02).                   
 01 I                         PIC 9(01).                         
 PROCEDURE DIVISION.                                             
     MOVE ZEROES     TO TOTAL-MARKS.                             
     PERFORM VARYING I FROM 1 BY 1                                
       UNTIL I > 6                                               
        SET STD-INDEX TO 1                                       
        ACCEPT STD-MARKS (STD-INDEX)                             
        ADD STD-MARKS (STD-INDEX) TO TOTAL-MARKS                 
        SET STD-INDEX UP BY 1                                    
     END-PERFORM.                                                
     COMPUTE STD-PERCENT = TOTAL-MARKS/6.                        
     DISPLAY 'STUDENT PERCENTAGE : ' STD-PERCENT.                
     STOP RUN.