Table Subscript


Table data can be accessed by using Subscript. Subscript represents the number of table occurrences. It is a very straight forward value, and each occurrence can be accessed by using subscript.

Notes -

  • Subscript automatically created with OCCURS clause. But Subscript needs a separate variable to refer it.
  • A subscript can be any positive number.
  • Subscript values can range from 1 and maximum value limitation is equal to the number of OCCURS.

Diagram -

Memory Storage

In the above diagram, the OCCURS represents subscript. Let us use the below declaration, for a better understanding about subscript -

01 WS-CLASS. 
   03 WS-STUDENT  OCCURS 2 TIMES.
      05 WS-ROLL-NO      PIC X(03).       
      05 WS-NAME         PIC X(10).

In the above example, WS-STUDENT have 2 OCCURRENCES. Subscript always starts from 1 and increase by one to refer the next occurrence of data item. Total OCCURS can be accessed as - WS-STUDENT (1) and WS-STUDENT (2).

How the Subscript is declared?


Subscript should be defined in a WORKING-STORAGE SECTION. The subscript is declared with S9(04) COMP. If the number of occurrences is more, change the subscript declaration to accommodate the occurrences max value.

The subscript declaration is as follows -

01 WS-SUB         PIC S9(04).

How the table accessed using subscript in program?


Table elements can be accessed by using the subscript like below -

table-data-item (subscript-variable)

The student details can be accessed using subscript for the above example is -

WS-STUDENT (WS-SUB).

How the Subscript is initialized?


Subscript is initialized by using MOVE statement. Without initializing, the results are unpredictable. The lowest possible subscript value is 1. The subscript initialization for the above example is -

MOVE 1     TO WS-SUB.

How the subscript is incremented/decremented?


Subscript can be incremented by ADD operator and can be decremented by SUBTRACT operator. COMPUTE statement can be used to do both.

Incrementing subscript for the above example is -

ADD  1         TO WS-SUB.

COMPUTE WS-SUB = WS-SUB + 1.

Decrementing subscript for the above example is -

SUBTRACT 1     FROM WS-SUB.

COMPUTE WS-SUB = WS-SUB - 1.

Possible Errors (SOC4) -


  • If the Subscript increased beyond the maximum number of occurrences and tries to access the table item with it, that causes the S0C4 error.
  • The same error occurred if the Subscript is not initialized and trying to access the table element with uninitialized Subscript.

Solution: -

Debug the program to verify the Subscript value at the time of program abend. It is always good to validate the Subscript with maximum OCCURS value to avoid the error.

Practical Example -


Scenario - Declaring a subscript, initialized, incremented and used to navigate the table.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TBSUBSCR.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-CLASS.
          03 WS-STUDENT  OCCURS 2 TIMES. 
             05 WS-ROLL-NO      PIC X(03). 
             05 WS-NAME         PIC X(10). 
	  * Declaring a subscript
       01 WS-SUB                PIC S9(04) COMP. 

       PROCEDURE DIVISION. 
      * Initializing the subscript to 1 
           MOVE 1               TO WS-SUB. 
           MOVE "001PAWAN Y"    TO WS-STUDENT (WS-SUB).

      * Incrementing subscript by 1
           COMPUTE WS-SUB = WS-SUB + 1. 
           MOVE "002KUMAR"      TO WS-STUDENT (WS-SUB).

      * Displaying full table using subscript
           PERFORM VARYING WS-SUB FROM 1 BY 1 UNTIL WS-SUB > 2 
                   DISPLAY "STUDENT" WS-SUB " - " WS-STUDENT(WS-SUB)
           END-PERFORM.
           STOP RUN.

JCL to execute the above COBOL program −

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID             
//*                                                      
//STEP01  EXEC PGM=TBSUBSCR                              
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR         
//SYSOUT   DD  SYSOUT=*

Output -

STUDENT0001 - 001PAWAN Y 
STUDENT0002 - 002KUMAR