Summary -

In this topic, we described about the Working-storage Section with detailed example.

WORKING-STORAGE SECTION defines the data names that are needed for processing of data in the program.

WORKING-STORAGE SECTION also defines data items and general variables which might initialized either at the definition or during the program flow.

WORKING-STORAGE SECTION fields defined for internal usage in the program and cann’t be accessed outside of the program.

All the WORKING-STORAGE SECTION fields initialized at the starting of program execution.

Memory allocated to all WORKING-STORAGE SECTION fields when the program execution starts and gets deallocated when the program execution ends.

The WORKING-STORAGE SECTION must begin with the section header WORKING-STORAGE SECTION and followed by a separator period.

WORKING-STORAGE SECTION defines the records (record-description-entries) that are not part of file definition but still used to write into the file.

WORKING-STORAGE section can define individual, elementary and group data items (data-item-description-entries).

Syntax -

[DATA DIVISION.]
[WORKING-STORAGE SECTION.]
	[record-description-entries.]
	[data-item-description-entries.]

Practical Example -

Scenario - Below example describes how to declare different types of variables in WORKING-STORAGE section.

Code -

WORKING-STORAGE section program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. WSSECTN.                                             
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
      *INDIVIDUAL VARIABLES / DATA ITEMS                                
       01 WS-LEVEL01           PIC 9(03) VALUE 128.                     
                                                                        
      *NON-CONTIGIOUS VARIABLE /DATA ITEM                               
       77 WS-LEVEL77           PIC X(03).                               
      *GROUP VARIABLE / DATA ITEM                                       
       01 WS-GROUP.                                                     
      *ELEMENTARY VARIABLE / DATA ITEM                                  
          05 WS-ELEM1          PIC 9(03).                               
      *REDEFINES  VARIABLE / DATA ITEM                                  
          05 WS-REDEF          REDEFINES WS-ELEM1 PIC X(03).            
          05 WS-ELEM2          PIC 9(03).                               
      *RENAMES    VARIABLE / DATA ITEM                                  
          66 WS-LEVEL66 RENAMES  WS-ELEM2.                              
      *CONDITIONAL VARIABLE                                             
       01 WS-GENDER            PIC X(01).                               
          88 WS-MALE           VALUE "M".                               
          88 WS-FEMALE         VALUE "F".                               
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           MOVE 099            TO WS-LEVEL01                            
                                  WS-LEVEL77.                           
           DISPLAY "INDIVIDUAL VARIABLE VALUE:     " WS-LEVEL01.        
           DISPLAY "NON-CONTIGIOUS VARIABLE VALUE: " WS-LEVEL77.        
                                                                        
           MOVE 128            TO WS-ELEM1                              
           MOVE 256            TO WS-ELEM2.                             
           DISPLAY "ELEMNTARY VARIABLE1 VALUE:     " WS-ELEM1.          
           DISPLAY "ELEMNTARY VARIABLE2 VALUE:     " WS-ELEM2.          
           DISPLAY "GROUP VARIABLE VALUE:          " WS-GROUP.          
           DISPLAY "REDEF   VARIABLE VALUE:        " WS-REDEF.          
           DISPLAY "RENAMES VARIABLE VALUE:        " WS-LEVEL66.        
                                                                        
           MOVE "M"            TO WS-GENDER.                            
           IF WS-MALE                                                   
              DISPLAY "GENDER IS MALE"                                  
           ELSE                                                         
              DISPLAY "GENDER IS FEMALE"                                
           END-IF.                                                      
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

WORKING-STORAGE section program Output

Explaining Example -

In the above example, we have shown different types of variables declaration, assigning values to them and displaying them in spool.