The SET statement in COBOL is used to set values for special data types that require specific assignment operations: pointers, condition names (88-level), indexes, and ADDRESS OF references. SET is different from MOVE - while MOVE copies data values between regular data items, SET handles assignments for special data types that have unique requirements. Understanding SET is essential for working with pointers, dynamic memory, condition-based logic, and efficient array access.
This comprehensive guide will teach you how to use SET with pointers for memory management, SET with condition names for readable boolean logic, SET with indexes for array navigation, SET with ADDRESS OF for dynamic storage, and best practices for effective SET usage. Whether you're managing dynamic memory, implementing condition-based logic, or working with arrays, mastering the SET statement is crucial for modern COBOL programming.
The SET statement is used for assignments that require special handling:
SET vs MOVE:
| Aspect | SET | MOVE |
|---|---|---|
| Purpose | Set pointers, condition names, indexes, ADDRESS OF | Copy data values between data items |
| Data Types | Pointers, condition names (88-level), indexes | Regular data items (numeric, alphanumeric, etc.) |
| Pointer Assignment | SET pointer TO address | Cannot use MOVE for pointers |
| Condition Names | SET condition-name TO TRUE/FALSE | Cannot use MOVE for condition names |
| Indexes | SET index TO value or UP BY/DOWN BY | Cannot use MOVE for indexes |
| ADDRESS OF | SET pointer TO ADDRESS OF item | Cannot use MOVE with ADDRESS OF |
SET is used to assign values to pointers. Pointers contain memory addresses and are used for dynamic memory management and BASED storage.
1234567891011121314WORKING-STORAGE SECTION. 01 DATA-POINTER USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Initialize pointer to NULL SET DATA-POINTER TO NULL *> Check if pointer is NULL IF DATA-POINTER = NULL DISPLAY "Pointer is NULL" END-IF STOP RUN.
Setting a pointer to NULL indicates it doesn't reference any memory. Always initialize pointers to NULL before use.
12345678910111213WORKING-STORAGE SECTION. 01 DATA-ITEM PIC X(50). 01 DATA-POINTER USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Get address of data item and store in pointer SET DATA-POINTER TO ADDRESS OF DATA-ITEM *> Now DATA-POINTER contains the address of DATA-ITEM DISPLAY "Pointer set to address of DATA-ITEM" STOP RUN.
SET pointer TO ADDRESS OF identifier gets the memory address of the data item and stores it in the pointer.
1234567891011121314WORKING-STORAGE SECTION. 01 POINTER-1 USAGE IS POINTER. 01 POINTER-2 USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Set pointer-1 to some address SET POINTER-1 TO ADDRESS OF DATA-ITEM *> Copy pointer value SET POINTER-2 TO POINTER-1 *> Now both pointers reference the same memory STOP RUN.
You can copy pointer values using SET pointer-1 TO pointer-2. Both pointers will reference the same memory location.
SET is used to set condition names (88-level) to true or false states. Condition names provide readable boolean logic.
1234567891011121314151617WORKING-STORAGE SECTION. 01 STATUS-FLAG PIC X(1). 88 ACTIVE-STATUS VALUE 'A'. 88 INACTIVE-STATUS VALUE 'I'. 88 PENDING-STATUS VALUE 'P'. PROCEDURE DIVISION. MAIN-LOGIC. *> Set condition name to TRUE SET ACTIVE-STATUS TO TRUE *> Now STATUS-FLAG contains 'A' IF ACTIVE-STATUS DISPLAY "Status is active" END-IF STOP RUN.
SET condition-name TO TRUE sets the associated data item to one of its condition values, making the condition true.
12345678910111213141516WORKING-STORAGE SECTION. 01 PROCESSING-FLAG PIC X(1) VALUE 'Y'. 88 PROCESSING VALUE 'Y'. 88 NOT-PROCESSING VALUE 'N'. PROCEDURE DIVISION. MAIN-LOGIC. *> Set condition name to FALSE SET PROCESSING TO FALSE *> Now PROCESSING-FLAG contains 'N' IF NOT-PROCESSING DISPLAY "Processing is stopped" END-IF STOP RUN.
SET condition-name TO FALSE sets the associated data item to a value that doesn't match the condition, making it false.
123456789101112131415WORKING-STORAGE SECTION. 01 RECORD-TYPE PIC X(1). 88 HEADER-RECORD VALUE 'H'. 88 DETAIL-RECORD VALUE 'D'. 88 TRAILER-RECORD VALUE 'T'. PROCEDURE DIVISION. MAIN-LOGIC. *> Set multiple condition names (must be same data item) SET HEADER-RECORD TO TRUE *> Can also set to FALSE SET HEADER-RECORD TO FALSE STOP RUN.
You can set multiple condition names in one SET statement if they're associated with the same data item. The first one listed takes precedence.
SET is used to set index values for table/array access. Indexes provide efficient array navigation.
1234567891011121314151617WORKING-STORAGE SECTION. 01 CUSTOMER-TABLE. 05 CUSTOMER-ENTRY OCCURS 100 TIMES INDEXED BY CUST-INDEX. 10 CUST-ID PIC 9(8). 10 CUST-NAME PIC X(30). PROCEDURE DIVISION. MAIN-LOGIC. *> Set index to position 5 SET CUST-INDEX TO 5 *> Access array element at index 5 MOVE 12345678 TO CUST-ID(CUST-INDEX) MOVE 'JOHN SMITH' TO CUST-NAME(CUST-INDEX) STOP RUN.
SET index-name TO integer-value sets the index to a specific position. Use the index to access array elements.
123456789101112131415161718192021WORKING-STORAGE SECTION. 01 DATA-TABLE. 05 TABLE-ENTRY OCCURS 50 TIMES INDEXED BY TABLE-INDEX. 10 ENTRY-VALUE PIC 9(5). PROCEDURE DIVISION. MAIN-LOGIC. *> Initialize index to 1 SET TABLE-INDEX TO 1 *> Increment index by 1 SET TABLE-INDEX UP BY 1 *> Decrement index by 1 SET TABLE-INDEX DOWN BY 1 *> Increment by multiple SET TABLE-INDEX UP BY 5 STOP RUN.
SET index-name UP BY integer increments the index. SET index-name DOWN BY integer decrements the index. This is more efficient than SET index TO index + value.
SET is used with ADDRESS OF to get memory addresses and associate BASED storage with pointers.
1234567891011WORKING-STORAGE SECTION. 01 DATA-ITEM PIC X(100). 01 DATA-POINTER USAGE IS POINTER. PROCEDURE DIVISION. MAIN-LOGIC. *> Get address of data item SET DATA-POINTER TO ADDRESS OF DATA-ITEM *> DATA-POINTER now contains the address of DATA-ITEM STOP RUN.
SET pointer TO ADDRESS OF identifier gets the memory address of the data item and stores it in the pointer.
12345678910111213141516171819202122WORKING-STORAGE SECTION. 01 RECORD-POINTER USAGE IS POINTER. 01 DYNAMIC-RECORD BASED. 05 RECORD-ID PIC 9(8). 05 RECORD-NAME PIC X(30). PROCEDURE DIVISION. MAIN-LOGIC. *> Allocate memory ALLOCATE DYNAMIC-RECORD RETURNING RECORD-POINTER *> Associate BASED record with pointer SET ADDRESS OF DYNAMIC-RECORD TO RECORD-POINTER *> Now use DYNAMIC-RECORD MOVE 12345678 TO RECORD-ID MOVE 'JOHN SMITH' TO RECORD-NAME *> Free memory FREE RECORD-POINTER STOP RUN.
SET ADDRESS OF based-item TO pointer associates a BASED data item with the memory location specified by the pointer. This enables dynamic memory management.
Here's a complete example demonstrating various SET statement uses:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107IDENTIFICATION DIVISION. PROGRAM-ID. SET-STATEMENT-EXAMPLE. AUTHOR. MainframeMaster Tutorial. REMARKS. Complete example demonstrating SET statement usage. DATA DIVISION. WORKING-STORAGE SECTION. 01 PROCESSING-CONTROL. 05 STATUS-FLAG PIC X(1) VALUE 'N'. 88 ACTIVE VALUE 'Y'. 88 INACTIVE VALUE 'N'. 05 PROCESS-TYPE PIC X(1). 88 BATCH-PROCESS VALUE 'B'. 88 ONLINE-PROCESS VALUE 'O'. 01 DATA-ARRAYS. 05 CUSTOMER-TABLE. 10 CUSTOMER-ENTRY OCCURS 100 TIMES INDEXED BY CUST-IDX. 15 CUST-ID PIC 9(8). 15 CUST-NAME PIC X(30). 01 DYNAMIC-MEMORY. 05 RECORD-PTR USAGE IS POINTER VALUE NULL. 05 DYNAMIC-RECORD BASED. 10 REC-ID PIC 9(8). 10 REC-NAME PIC X(30). PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== SET Statement Examples ===" DISPLAY " " PERFORM SET-CONDITION-NAMES PERFORM SET-INDEXES PERFORM SET-POINTERS DISPLAY " " DISPLAY "SET statement examples completed" STOP RUN. SET-CONDITION-NAMES. DISPLAY "Setting condition names..." *> Set condition name to TRUE SET ACTIVE TO TRUE IF ACTIVE DISPLAY " Status is now ACTIVE" END-IF *> Set condition name to FALSE SET ACTIVE TO FALSE IF INACTIVE DISPLAY " Status is now INACTIVE" END-IF *> Set another condition name SET BATCH-PROCESS TO TRUE IF BATCH-PROCESS DISPLAY " Process type is BATCH" END-IF. SET-INDEXES. DISPLAY "Setting indexes..." *> Set index to position 1 SET CUST-IDX TO 1 MOVE 12345678 TO CUST-ID(CUST-IDX) MOVE 'JOHN SMITH' TO CUST-NAME(CUST-IDX) DISPLAY " Set customer at index 1: " CUST-ID(CUST-IDX) *> Increment index SET CUST-IDX UP BY 1 MOVE 87654321 TO CUST-ID(CUST-IDX) MOVE 'JANE DOE' TO CUST-NAME(CUST-IDX) DISPLAY " Set customer at index 2: " CUST-ID(CUST-IDX) *> Set index to specific position SET CUST-IDX TO 10 DISPLAY " Index set to position 10". SET-POINTERS. DISPLAY "Setting pointers..." *> Initialize pointer to NULL SET RECORD-PTR TO NULL IF RECORD-PTR = NULL DISPLAY " Pointer initialized to NULL" END-IF *> Allocate memory and get pointer ALLOCATE DYNAMIC-RECORD RETURNING RECORD-PTR IF RECORD-PTR NOT = NULL *> Associate BASED record with pointer SET ADDRESS OF DYNAMIC-RECORD TO RECORD-PTR *> Use BASED record MOVE 99999999 TO REC-ID MOVE 'DYNAMIC RECORD' TO REC-NAME DISPLAY " Dynamic record created: " REC-ID " " REC-NAME *> Free memory FREE RECORD-PTR SET RECORD-PTR TO NULL DISPLAY " Memory freed, pointer set to NULL" END-IF.
This complete example demonstrates SET with condition names, indexes, and pointers. It shows various SET operations in a practical context.
Follow these best practices for effective SET statement usage:
1SET POINTER TO NULL
123SET CONDITION-NAME TO TRUE *> or SET CONDITION-NAME TO FALSE
123SET INDEX TO 1 SET INDEX UP BY 1 SET INDEX DOWN BY 1
1SET POINTER TO ADDRESS OF DATA-ITEM
1SET ADDRESS OF BASED-ITEM TO POINTER
Think of SET like special instructions for special things:
So SET is like special instructions for pointers (addresses), condition names (on/off switches), and indexes (pointers to positions in lists)!
Complete these exercises to reinforce your understanding:
Create a program that defines condition names for a status field, sets them to TRUE and FALSE, and uses them in IF statements.
Create a program with an array, set an index to different positions, increment and decrement it, and access array elements using the index.
Create a program that initializes a pointer to NULL, gets the address of a data item, copies the pointer value, and checks for NULL.
Create a program that allocates memory, sets ADDRESS OF a BASED item to the pointer, uses the BASED item, and frees the memory.
Create a program that uses SET with condition names, indexes, and pointers together to manage program state, navigate arrays, and handle dynamic memory.
1. What is the SET statement used for in COBOL?
2. How do you set a pointer to NULL?
3. How do you set a condition name (88-level) to true?
4. How do you get the address of a data item and store it in a pointer?
5. How do you associate a BASED data item with a pointer?
6. How do you increment an index by 1?
7. What is the difference between SET and MOVE?
8. Can you set multiple condition names in one SET statement?