Skip to content

SET Statement

The SET statement can be used to set data types and environment variables to prescribed values.

Format 1

The Format 1 SET statement sets the value of an environment variable. If the environment variable does not exist, it is created, and assigned the given value.

SET ENVIRONMENT { literal-1 TO identifier-1 } ...

Syntax:

  1. literal-n is a character string.
  2. identifier-n is a data element, literal, or data returned from a function call.

General Rules:

  1. In the Format 1 SET statement, literal-1 is the name of an environment variable that is set to the value of identifier-1.
  2. The value of the environment variable can be retrieved by the ACCEPT... FROM ENVIRONMENT statement by the COBOL-IT program, and any subprograms called by the COBOL-IT program.

Code Sample:

       77 COBOL-TYPE PIC X(10) VALUESPACES. 
       ... 
              SET ENVIRONMENT "COBOL" TO "COBOL-IT". 
              ACCEPT COBOL-TYPE FROM ENVIRONMENT "COBOL". 
              DISPLAY COBOL-TYPE LINE 13 COL 10. 
       ...

Format 2

The Format 2 SET statement sets a data item to a given value.

SET {data-1} ... TO identifier-1.

Syntax:

  1. data-n is a data item.
  2. identifier-n is a data element, literal, or data returned from a function call.

General Rules:

The Format 2 SET statement has the effect of a MOVE statement, assigning a given value to a data item.

Code Sample:

       77 NUMERIC-1  PIC 99 VALUE 0. 
       77 ALPHA-1    PIC X(5) VALUE SPACES. 
       ... 
              SET NUMERIC-1 TO 10. 
              DISPLAY NUMERIC-1 LINE 5 COL 10. 
              SET ALPHA-1 TO "HELLO". 
              DISPLAY ALPHA-1 LINE 5 COL 20. 
       ...

Format 3

The Format 3 SET statement assigns data item with USAGE POINTER to the ADDRESS of a variable.

       SET {data-ptr-1}... TO { ADDRESS OF identifier-1 } 
                              { NULL                    }

Syntax:

  1. data-ptr-n is a data item declared with USAGE POINTER.
  2. identifier-n is a data element, literal, or data returned from a function call.

General Rules:

There are no General Rules.

Note

The Format 3 SET statement can be used in conjunction with the Format 4 SET statement to enable the use of a Linkage item in a sub-program which has not been part of a CALL USING statement. For an example, see the Code Sample included with the Format 4 Set Statement.

Format 4

The Format 4 SET statement assigns a linkage data item the ADDRESS contained in a data item with USAGE POINTER.

SET {ADDRESS OF linkage-data-item-1}... TO data-ptr-1

Syntax:

linkage-data-item-n is a data item declared in the LINKAGE SECTION.

General Rules:

There are no General Rules.

Note

The Format 3 SET statement can be used in conjunction with the Format 4 SET statement to enable the use of a Linkage item in a sub-program which has not been part of a CALL USING statement.

Referencing a Linkage item that had no address associated with it would normally abort a runtime session. In the sample below, the linkage item acquires a valid address, and can be referenced without a problem.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. SUBPGM. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
       77 COBOL-TYPE PIC X(8) VALUE "COBOL-IT". 
       77 DATA-PTR-1 USAGE POINTER. 
       77 DATA-PTR-2 USAGE POINTER. 
       LINKAGE SECTION. 
       01 LINK-1 PIC X(8). 
       PROCEDURE DIVISION. 
       MAIN. 
              SET DATA-PTR-1 TO ADDRESS OF COBOL-TYPE. 
              DISPLAY "DATA-PTR SET TO ADDRESS" LINE 6 COL 10. 
              SET DATA-PTR-2 TO NULL. 
              DISPLAY "DATA-PTR SET TO NULL" LINE 7 COL 10. 
              SET ADDRESS OF LINK-1 TO DATA-PTR-1. 
              DISPLAY "LINK-1: " LINE 8 COL 10. 
              DISPLAY LINK-1 LINE 8 COL 20. 
              GOBACK.

Format 5

       SET {data-1} ... {UP } BY integer-1 
                                   {DOWN}

Syntax:

  1. data-n is a data element that is an integer.
  2. integer-n is a data element, literal or data returned from a function call that is an integer.

General Rules:

  1. The Format 5 SET statement has the effect of an ADD or SUBTRACT statement, incrementing or decrementing a numeric data item with an integer value be a given integer value.
  2. The Format 5 SET statement may be used with indexes, as a means of directing table handling.

Code Sample:

       77 NUMERIC-1 PIC 99 VALUE 20. 
       ...
              SET NUMERIC-1 UP BY 10. 
              DISPLAY NUMERIC-1 LINE 6 COL 10. 
              SET NUMERIC-1 DOWN BY 5. 
              DISPLAY NUMERIC-1 LINE 7 COL 10. 
       ...

Format 6

       SET { {condition-1} ... TO {TRUE } } ... 
                                  {FALSE}

Syntax:

condition-n is a condition name that is described in a level 88 statement.

General Rules:

  1. The Format 6 SET statement has the effect of MOVE statement, MOVE'ing a value to the parent data item to create a TRUE condition when the condition is SET to TRUE, and MOVE'ing a value to the parent data item to create a FALSE condition when the condition is SET TO FALSE.
  2. IF the level 88 condition contains a WHEN SET TO FALSE value, then the effect of the SET [condition-1] TO FALSE is to MOVE that value to the parent data item.
  3. If an attempt is made to set a level-88 condition to FALSE, and that condition does not have an explicit WHEN SET TO FALSE clause, the compiler will generate an error.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. SET3. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
       77 DECISION-FLAG PIC 9 VALUE 0. 
              88 DECISION-YES VALUES 1 THRU 5. 
                     WHEN SET TO FALSE 6. 
       77 WS-DUMMY PIC X. 
       PROCEDURE DIVISION. 
       MAIN. 
              SET DECISION-YES TO TRUE. 
              DISPLAY DECISION-FLAG LINE 8 COL 10. 
              SET DECISION-YES TO FALSE. 
              DISPLAY DECISION-FLAG LINE 9 COL 10. 
              DISPLAY "ALL DONE!" LINE 10 COL 10. 
              ACCEPT WS-DUMMY LINE 10 COL 30. 
              STOP RUN.

Format 7

       SET { {mnemonic-name-1}  TO (ON ) }  
                                    (OFF)

Syntax:

mnemonic-name is a user defined word.

General Rules:

  1. The Format 7 SET statement SET's Switches that are described in SPECIAL-NAMES to ON or OFF. If the switch is described with an ON STATUS and OFF STATUS, these conditions can programmatically be checked for truth.

Code Sample:

       ... 
              SPECIAL-NAMES. 
                     SWITCH 1IS SWITCH-1 
                     ON STATUS IS SET-CHECK 
                     OFF STATUS IS SKIP-CHECK. 
       ... 

              SET SWITCH-1 TO ON. 
       ... 
              IF SET-CHECK 
                     DISPLAY "SWITCH 1 SET" LINE 4 COL 10 
              END-IF. 
       ...

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. SET-1. 
       ENVIRONMENT DIVISION. 
       CONFIGURATION SECTION. 
       SPECIAL-NAMES. 
              SWITCH 1IS SWITCH-1 
              ON STATUS IS SET-CHECK 
              OFF STATUS IS SKIP-CHECK. 

       DATA DIVISION. 
       WORKING-STORAGE SECTION. 
       77 NUMERIC-1 PIC 99 VALUE 0. 
       77 DECISION-FLAG PIC 9 VALUE 0. 
              88 DECISION-YES VALUE 1 
                     WHEN SET TO FALSE 0. 
       77 COBOL-TYPE PIC X(10) VALUE SPACES.
       77 DATA-PTR-1 USAGE POINTER. 
       77 DATA-PTR-2 USAGE POINTER. 
       01 DUMMY PIC X. 
       LINKAGE SECTION. 
       01 LINKAGE-1 PIC X(10). 
       PROCEDURE DIVISION. 
       MAIN. 
      * FORMAT 1 
              SET ENVIRONMENT "COBOL" TO "COBOL-IT". 
              ACCEPT COBOL-TYPE FROM ENVIRONMENT "COBOL". 
              DISPLAY COBOL-TYPE LINE 4 COL 10. 
      * 
      * FORMAT 2 
      * 
              SET NUMERIC-1 TO 10. 
              DISPLAY NUMERIC-1 LINE 5 COL 10. 
      * 
      * FORMAT 3 
      * 
              SET DATA-PTR-1 TO ADDRESS OF COBOL-TYPE. 
              DISPLAY "DATA-PTR SET TO ADDRESS" LINE 6 COL 10. 
              SET DATA-PTR-2 TO NULL. 
              DISPLAY "DATA-PTR SET TO NULL" LINE 7 COL 10. 
      * 
      * FORMAT 4 
      * 
              SET ADDRESS OF LINKAGE-1 TO DATA-PTR-1. 
              DISPLAY "ADDRESS OF LINKAGE ITEM SET" LINE 8 COL 10. 
      * 
      * FORMAT 5 
      * 
              SET NUMERIC-1 UP BY 10. 
              DISPLAY NUMERIC-1 LINE 9 COL 10. 
              SET NUMERIC-1 DOWN BY 5. 
              DISPLAY NUMERIC-1 LINE 10 COL 10. 
      * 
      * FORMAT 6 
      * 
              SET DECISION-YES TO TRUE. 
              DISPLAY DECISION-FLAG LINE 8 COL 10. 
              SET DECISION-YES TO FALSE. 
              DISPLAY DECISION-FLAG LINE 9 COL 10. 
      * 
      * FORMAT 7 
      * 
              SET SWITCH-1 TO ON. 
              IF SET-CHECK 
                     DISPLAY "SWITCH 1 SET" LINE 11 COL 10 
              END-IF. 
      * 
              DISPLAY "SET-1 FINISHED!" LINE 15 COL 10. 
              ACCEPT DUMMY LINE 15 COL 30. 

              STOP RUN.
Back to top