MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL SET Statement

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.

What is the SET Statement?

The SET statement is used for assignments that require special handling:

  • Pointers: Set pointers to memory addresses or NULL
  • Condition names (88-level): Set condition names to true or false states
  • Indexes: Set indexes to array positions or increment/decrement them
  • ADDRESS OF: Get addresses of data items or associate BASED storage with pointers

SET vs MOVE:

SET vs MOVE Comparison
AspectSETMOVE
PurposeSet pointers, condition names, indexes, ADDRESS OFCopy data values between data items
Data TypesPointers, condition names (88-level), indexesRegular data items (numeric, alphanumeric, etc.)
Pointer AssignmentSET pointer TO addressCannot use MOVE for pointers
Condition NamesSET condition-name TO TRUE/FALSECannot use MOVE for condition names
IndexesSET index TO value or UP BY/DOWN BYCannot use MOVE for indexes
ADDRESS OFSET pointer TO ADDRESS OF itemCannot use MOVE with ADDRESS OF

SET with Pointers

SET is used to assign values to pointers. Pointers contain memory addresses and are used for dynamic memory management and BASED storage.

Setting Pointers to NULL

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-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.

Setting Pointer to Address

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
WORKING-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.

Copying Pointer Values

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-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 with Condition Names (88-level)

SET is used to set condition names (88-level) to true or false states. Condition names provide readable boolean logic.

Setting Condition Names to TRUE

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WORKING-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.

Setting Condition Names to FALSE

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WORKING-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.

Setting Multiple Condition Names

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WORKING-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 with Indexes

SET is used to set index values for table/array access. Indexes provide efficient array navigation.

Setting Index to Value

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WORKING-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.

Incrementing and Decrementing Indexes

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WORKING-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 with ADDRESS OF

SET is used with ADDRESS OF to get memory addresses and associate BASED storage with pointers.

Getting Address of Data Item

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-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.

Associating BASED Storage with Pointer

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
WORKING-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.

Complete SET Statement Example

Here's a complete example demonstrating various SET statement uses:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
IDENTIFICATION 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.

Best Practices for SET Statement

Follow these best practices for effective SET statement usage:

  • Use SET for special data types: Use SET for pointers, condition names, and indexes - not MOVE
  • Initialize pointers to NULL: Always set pointers to NULL before first use
  • Use meaningful condition names: Name condition names clearly to indicate their purpose
  • Set condition names for state management: Use condition names to manage program state clearly
  • Use UP BY/DOWN BY for index increments: More efficient than SET index TO index + value
  • Verify pointers before use: Check that pointers are not NULL before using ADDRESS OF
  • Set ADDRESS OF after ALLOCATE: Set ADDRESS OF BASED items after allocating memory
  • Document complex SET operations: Comment complex pointer manipulations and state changes
  • Set condition names to FALSE when resetting: Explicitly set to FALSE when resetting state
  • Test SET operations thoroughly: Test with various scenarios including edge cases

Common SET Patterns

Pattern 1: Initialize Pointer

cobol
1
SET POINTER TO NULL

Pattern 2: Set Condition Name

cobol
1
2
3
SET CONDITION-NAME TO TRUE *> or SET CONDITION-NAME TO FALSE

Pattern 3: Set Index

cobol
1
2
3
SET INDEX TO 1 SET INDEX UP BY 1 SET INDEX DOWN BY 1

Pattern 4: Get Address

cobol
1
SET POINTER TO ADDRESS OF DATA-ITEM

Pattern 5: Associate BASED Storage

cobol
1
SET ADDRESS OF BASED-ITEM TO POINTER

Explain Like I'm 5: SET Statement

Think of SET like special instructions for special things:

  • SET pointer is like writing down an address on a piece of paper - you're remembering where something is
  • SET condition-name TO TRUE is like turning on a light switch - you're making something active
  • SET index TO 5 is like pointing to the 5th item in a row - you're saying "this one"
  • SET index UP BY 1 is like moving your finger to the next item - you're going to the next one
  • SET ADDRESS OF is like getting the address of a house - you're finding where something lives

So SET is like special instructions for pointers (addresses), condition names (on/off switches), and indexes (pointers to positions in lists)!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Condition Names

Create a program that defines condition names for a status field, sets them to TRUE and FALSE, and uses them in IF statements.

Exercise 2: Indexes

Create a program with an array, set an index to different positions, increment and decrement it, and access array elements using the index.

Exercise 3: Pointers

Create a program that initializes a pointer to NULL, gets the address of a data item, copies the pointer value, and checks for NULL.

Exercise 4: BASED Storage

Create a program that allocates memory, sets ADDRESS OF a BASED item to the pointer, uses the BASED item, and frees the memory.

Exercise 5: Combined Usage

Create a program that uses SET with condition names, indexes, and pointers together to manage program state, navigate arrays, and handle dynamic memory.

Test Your Knowledge

1. What is the SET statement used for in COBOL?

  • To copy data values
  • To set pointers, condition names, indexes, and ADDRESS OF references
  • To perform calculations
  • To read files

2. How do you set a pointer to NULL?

  • MOVE NULL TO pointer
  • SET pointer TO NULL
  • MOVE 0 TO pointer
  • INITIALIZE pointer

3. How do you set a condition name (88-level) to true?

  • MOVE TRUE TO condition-name
  • SET condition-name TO TRUE
  • MOVE 1 TO condition-name
  • INITIALIZE condition-name

4. How do you get the address of a data item and store it in a pointer?

  • MOVE ADDRESS OF data-item TO pointer
  • SET pointer TO ADDRESS OF data-item
  • MOVE data-item TO pointer
  • INITIALIZE pointer WITH ADDRESS OF data-item

5. How do you associate a BASED data item with a pointer?

  • MOVE pointer TO BASED-item
  • SET ADDRESS OF BASED-item TO pointer
  • MOVE ADDRESS OF pointer TO BASED-item
  • INITIALIZE BASED-item WITH pointer

6. How do you increment an index by 1?

  • MOVE index + 1 TO index
  • SET index TO index + 1
  • SET index UP BY 1
  • ADD 1 TO index

7. What is the difference between SET and MOVE?

  • There is no difference
  • SET is for pointers/condition names/indexes, MOVE is for regular data
  • SET is faster than MOVE
  • MOVE is for pointers, SET is for data

8. Can you set multiple condition names in one SET statement?

  • No, only one at a time
  • Yes, if they're associated with the same data item
  • Only if they have the same value
  • Only in certain COBOL versions

Related Concepts

Related Pages