8 SAS Array Function You Should Know

Disclaimer: There is no such thing as an array function in SAS. There are functions that accept lists of arguments though. This is why people tend to think of them as array functions. Because we can use an array reference directly in the function. This post introduces 8 functions that are commonly used with array references. These are the Coalesce, Coalescec, Whichn, Whichc, Dim, Range, Nmiss and Cmiss Functions.

In the examples to come, I will use the following simple data

data have;
input n1-n5 (c1-c5)(:$);
infile datalines dsd dlm=',';
datalines;
2,4,.,6,8,Two,Four,,Six,Eight
;

Coalesce and Coalescec

First, let us take a look at the Coalesce and Coalescec Functions. The Coalesce(c) Function returns the first non-missing value from a list of numeric/character values. Below, I set up two SAS arrays. One with all the numeric variables and one with all the character variables. Next, I use the Coalesce and CoalesceC Functions to find the first non missing value in both of the arrays. As you can see from the results, these are 2 and “Two” respectively.

data coalesce;
   set have;
   array num  {5} n1-n5;
   array char {5} c1-c5;
   a=coalesce(of num[*]);   /* coalesce(of _numeric_); */
   b=coalescec(of char[*]); /* coalescec(of _character_); */
run;

In the commented part in the SAS code above, I use the specially named variables lists _numeric_/_character_ to specify the list of variables instead of the Of array[*] syntax. This is a common alternative to the Of Array[*] syntax. However, the it limits itself to all the numeric/character variables. With the array, we can specify exactly the variables we want.

Whichn and Whichc

Next, let us look at the Whichn and Whichc Functions. The two functions searches for the first argument in the next n arguments. Then, it returns the index of the first mathching value. The Whichn function does so for numeric values. The Whichc function does it for character values. The functions are very popular among SAS programmers. And it is justified why. No other function lets you easily determine if a value is present in 100 other variables or not.

You can see a small example below. The variables a and b both take on the value 2 because that is the index of the first matching value. Be aware, that if there are more than one matching value, the functions return only the index of the first.

data which;
set have;
array num {5} n1-n5;
array char {5} c1-c5;
a=whichn(4, of num[*]);
b=whichc(‘Four’, of char[*]);
run;

Dim and Range

Next, let us look at the Dim and Range Functions. The Dim Function is quite simple. It returns the number of elements in an array. You will often see SAS programmers use the dim function in a Do Loop because it lets them iterate over each element in an array.

The Range Function takes a list of values as argument and returns the difference between the largest and the smallest value. It does not consider missing values. You can see a small example of the two functions below. The value of a is five because there are five elements in the array char. The value of b is 6 because that is the difference between the largest and the smallest number in the array of numbers (8 and 2 respectively).

data DimRange;
set have;
array num {5} n1-n5;
array char {5} c1-c5;
a=dim(char);
b=range(of num[*]);
run;

Nmiss and Cmiss

Finally, let us take a look at the Nmiss and Cmiss Functions. These functions return the number of missing values for numeric/character value lists. Most programmers know this. However, not too many are aware that the Cmiss Function handles numeric input as well as character. That is not the case for Nmiss. This makes the Cmiss Function extremely powerful. We can safely use the function with any list of variables, including the specially named variables list _ALL_.

You can see a small example of all cases below. As you can see in the variable c, the Cmiss Function accepts numeric and character lists. All at once. The value of c is not surprisingly the sum of a and b.

data NCMiss;
set have;
array num {5} n1-n5;
array char {5} c1-c5;
a=nmiss(of num[*]);
b=cmiss(of char[*]);
c=cmiss(of num[*], of char[*]);
run;

Summary

In this post, I have presented eight ‘Array Function’. That is, functions that accept lists of values as argument. Naturally, there are many more functions that do so. In this post, I choose eight that are very common among SAS programmers. For each of them, I show a small example of the functionality. Obviously, there is much more to say about each function. I let this post be a teaser and let the reader explore further on their own. In each of the example sections above, I link to the relevant part of the SAS Documentation. Needless to say, you should always consult the documentation when you deal with a new SAS Function.

If you want to know more about how to create lists of variables in SAS, check out the post 6 easy ways to specify a list of variables in SAS by Rick Wicklin.

You can download the entire code from this post here.