Implicit Vs Explicit Array in SAS

Most SAS programmers are familiar with the use of arrays in the data step. The documentation covers the syntax and use well. However, did you know that the current documentation only covers explicit arrays? This post explains the difference between an explicit and implicit array in SAS. We will see small code snippets of how to define, reference and iterate over both types. Finally, I will discuss why SAS only documents the explicit array and why most programmers recommends not using the implicit version.

The Explicit Array

The term explicit array means an array with an explicit subscript. This means that when we define it, we specify a subscript in parenthesis after the array name. Either as a numeric value, a numeric range or and asterisk. Now, we can reference elements in the array with any numeric value or variable that fits in the subscript. Similarly, we can assign new values to specific elements in the array. We can even use any variable to iterate over the entire array as you can see in the code snippet below.

data _null_;
   array a {5} a1-a5 (5*1);
   a[3]=10;
   b=a[3];
   put b= //;
 
   do x=1 to dim(a);
      put (a[x])(=);
   end;
run;

I intentionally spend little time going over the explicit array. Emphasis will be on the implicit array.

The Implicit Array

Consistent with the terminology above, an implicit array means an array with an implicit subscript. When we define an implicit array, we do not explicitly specify how many entries the array has. Rather, we specify an index variable. When we reference the elements in the array, we do so only with this index variable. This means, we can not reference the first element in an array a like a[1]. We have to be aware of the index variable, let’s say i. Set i=1 and reference a with no subscript. See the code below.

As an exercise, try to reference a[1] in the code below. This gives the error message: “ERROR: Mixing of implicit and explicit array subscripting is not allowed.”.

data _null_;
   array a (i) a1-a5  (2 4 6 8 10);
 
   i=3;
   put a=;
 
   /* b=a[1]; ERROR: Mixing of implicit and explicit array subscripting is not allowed. */
run;

The Do Over Syntax

The implicit array offers an alternative syntax to iterate over the elements in it. This is the Do Over Statement. Below, I iterate over a. First, I use the index variable i in a regular do loop and reference the i’th element of a step wise. Next, I do the exact same thing, but with the Do Over syntax. The two loops produce the same output. Furthermore, behind the scenes, the exact same thing happens in the two loops. SAS uses the index variable i and reference the i’th element in a.

data _null_;
   array a (i) a1-a5  (1:5);
 
   do i=1 to dim(a);
      put a=;
   end;
 
   do over a;
      put a=;
   end;
run;

Just like regular do loops, you can nest Do Over loops for different arrays. However, be aware that you must specify different index variables for each implicit array. Otherwise SAS keeps iterating the same index for different arrays, which is not what we want. See a small example below. Here, SAS iterates through each element in b with the index j for each element in a with index i. Just as intended. As an exercise, try to set the same index variable for the two arrays and run the program. SAS does not yield an error, but the result is probably not as you desire.

data _null_;
   array a (i) a1-a5  (1:5);
   array b (j) b1-b5  (6:10);
 
   do over a;
      do over b;
         put a= b=;
      end;
   end;
run;

The _I_ Index Variable

When SAS programmers specify an implicit array, they often do not even specify an index variable. When this is the case, SAS creates a temporary variable _I_ as the index variable. See a small example below. Here, I use a Do Over loop to iterate through the elements. I put the value of each element and the value of _I_ in the log. You can see that _I_ behaves just like any index variable except that it is automatically dropped. Needless to say, you should never nest Do Over Loops when you use _I_ as the index variable for both arrays.

data _null_;
   array a a1-a5 (2 4 6 8 10);
 
   do over a;
      put @3 a= @10 _I_=;
   end;
run;

An Implicit Array That Contains Implicit Arrays

Unlike arrays with explicit subscripts, implicit arrays can contain other implicit arrays. This is a feature that I did not know of until recently. Also, a feature that the explicit arrays do not have. You can see a small example below. Here, I define the implicit array ab with the arrays a and b as its elements. Next, I loop over ab and put each element of a and b in the log. You have to be very careful with the indices of each array when you do this. Usually, it is a good idea to keep the same index for each array that you put into another array. I encourage you to play around with the code below to get this technique under your skin.

data _null_;
   array a (i) a1-a5  (1:5);
   array b (i) b1-b5  (6:10);
   array ab a b;
 
   do over ab;
      do i=1 to 5;
         put ab=;
      end;
   end;
run;

Discusion

So why bother with implicit arrays? Many SAS programmers never use the implicit version. Furthermore, SAS recommends that you stick to the explicitly subscripted arrays. They even removed the implicit notation from the documentation. You have do find a SAS 5 or 6 version to see the documentation. I understand this logic and I do rarely use the implicit notation. However, I do see a few things that make the implicit arrays worth having in the SAS toolkit still. First of all, the syntax seems cleaner. When we simply want to iterate through each element in an array, the Do Over notation is as short and clean as it gets. Furthermore, explicit arrays can not contain other arrays. Only implicit arrays can do that.

Summary

In this post, we have investigated explicit and implicit arrays. We have seen a few code example of the differences in use and syntax between the two. Furthermore, we have discussed if the use of implicit arrays should be part of your SAS toolkit or not. In my opinion, it should. Even though it is not documented anymore.

For further reading, see the article SAS Arrays, Both Implicit and Explicit.

For related posts, see 8 SAS Array Function You Should Know, Set All Array Elements to Zero in SAS and Use Temporary Arrays to Store Lagged Values in SAS. Also, did you know that The Implicit Array Does not Exist in PROC FCMP?

You can download the entire code from this post here.