A frequent question in the SAS Community is to search for a value in an array or list of variables. Either numeric or character. This post demonstrates a few different approaches to the problem. Iterating through the array manually, using the Whichn/Whichc Function and the SAS In Operator. I will demonstrate small examples of the techniques and discuss pros and cons.

Iterate Through Array Elements

The first approach that comes to mind to most SAS programmers is probably to write a simple do-loop and iterate through the array elements. Suppose, I want to search for the value 5 in the numeric array below. I loop from i = 1 to the maximum index value for the array. If an array element equals 5, I set found to 1 and use the Vname Function to find the variable that contains the values. Finally, I use the Leave Statement. No need to keep iterating through the values.

data iterate;
   array n {10} (1 5 4 9 7 8 4 5 2 6);
   do i = 1 to dim (n);
      if n [i] = 5 then do;
         found = 1;
         v = vname(n [i]);
         leave;
      end;
   end;
run;

The Whichn and Whichc Function

Next, let us take a look at the Whichn and Whichc Function. These SAS functions are designed for this very purpose. As the documentation says, the Whichn Function “Searches for a numeric value that is equal to the first argument, and returns the index of the first matching value”. In the code below, I do the exact same thing as in the example above. Though with less and simpler code. I specify 5 as the first argument and use the Of keyword to target all array entries in the second argument.

data whichn;
   array n {10} (1 5 4 9 7 8 4 5 2 6);
   i = whichn (5, of n [*]);
   v = vname(n [i]);
run;

Also, this technique is easy to use on character values as well. Simply use the SAS Whichc Function instead. See the code example below.

data whichc;
   array c {10} $ ('a' 'e' 'd' 'i' 'g' 'h' 'd' 'e' 'b' 'f');
   i = whichc ('e', of c [*]);
   v = vname(c [i]);
run;

The In Operator

Finally, let us search for a value with the SAS In Operator. This technique is not commonly used. Which is a shame because the syntax is neat and the code is quite efficient. Consider the code below. Here, I simply set found equal to one if 5 is in n. The syntax is very intuitive. You can shorten the code further by writing found as a boolean expression. See the code that is commented out. This returns 0 if 5 is not present in n and 1 otherwise.

data in_num;
   array n {10} (1 5 4 9 7 8 4 5 2 6);
   if 5 in n then found = 1;
   /* found = (5 in n); */
run;

Again, the code is easy to extend to character arrays as well. Consider the example below.

data in_char;
   array c {10} $ ('a' 'e' 'd' 'i' 'g' 'h' 'd' 'e' 'b' 'f');
   if 'e' in c then found = 1;
   /* found = ('e' in c); */
run;

You can see another example of the In Operator with arrays in the SAS Sample Note 41182.

Summary

In this post we consider different methods to search for values in variable lists and arrays. We consider both character and numeric arrays. I recommend that you use wither the Whichn/Whichc approach or the In Operator. The code can be written short and simple. In the example in this post, we find the first occurrence of a value. However, all the examples can easily be extended to find the last. Hint: Turn the array around.

For other examples of array function see the blog post 8 SAS Array Functions You Should Know. Also, read the two related posts Binary Searching an Array in SAS and Use Binary Search of Array in SAS Table Lookup.

You can download the entire code from this post here.