Understand the _IORC_ Automatic Variable in SAS

Last week, I blogged about the well-known automatic variable in the SAS Data Step: The _N_ Variable. Today, the topic will be another automatic variable in the data step, though not as famous. Today’s topic is the _IORC_ automatic variable. Never heard of it? Fear not. Not many SAS programmers have. In this post, I will introduce the variable, show you where programmers commonly use it and why.

A Small Introduction

IORC stands for Input Output Return Code. The _IORC_ variable is commonly used in conjunction with a Modify Statement or a Set Statement where the Key= Option is used on a SAS Data Set Index. In this context, SAS returns a numeric value to the _IORC_ variable, which indicates whether the Index search was successful or not. You can see a classic example in the blog post Using the Modify Statement in the SAS Data Step.

However, _IORC_ is not limited to this situation. Even though the SAS Documentation lists only two automatic variables in the data step, the _IORC_ variable is present even SAS does not perform an index search. Take a look at this small code example

data test1;
	set sashelp.class;
	put _IORC_=;
run;

Here, I simply read the sashelp.class data set. Despite the fact that I use no indexes, the _IORC_ variable is available for us to put in the log.

Four Facts to Understand the _IORC_ Variable

Now, to understand the _IORC_ variable, let me list a few rules of thumb to better understand it. SAS

  1. Automatically drops the _IORC_ variable.
  2. Initializes _IORC_ with a zero value.
  3. Automatically retain the variable
  4. Does not change the value of _IORC_ unless it performs an index search

Rule 1 and 2 above are easy to verify. If you check the log from the small code snippet, you can see that _IORC_=0. Furthermore, the variable is automatically dropped. This is no surprise. Rule 3 may surprise you though. Take a look at the code snippet below

data test2;
	set sashelp.class;
	if _N_=5 then _IORC_=1;
	put _IORC_=;
run;

When you check the log you can see that when we change the value of _IORC_ this value stays intact until we change it again. This is in contrast to the _N_ variable, which is changed every time the data step iterates through the Data Statement. This brings us to rule 4. SAS does not change the value of _IORC_ unless you do so yourself or an index search is performed. This makes the _IORC_ variable usable in other contexts than the _N_ variable because we know that in most circumstances, _IORC_ is zero by default, retained and untouched by SAS.

Summary

So should we use the _IORC_ variable more? Well, it depends. The _IORC_ variable is commonly used among advanced SAS programmers when a temporary, retained variable is needed. For example, as a return code for a Hash Object Search, the variable is popular. Browsing the SAS Community, you will see many examples of using the _IORC_ variable. Though we can recode all of them with a regular variable, a retain statement and a drop statement. There is nothing magic about the _IORC_ variable that can not be replicated. One could even argue that the code looks messier and that other coders may have a harder time understanding your code.

I encourage you to browse the community and see where programmers use the _IORC_ variable. Take a look at the slick example here for example. Also, you can read about another non-familiar automatic variable in the post Implicit and Explicit Arrays in SAS.

You can download the entire code from this post here.