SAS Temporary Arrays, Not Just for Experts


sas-temporary-arrays-01SAS temporary arrays are an underutilized jewel in the SAS toolbox. I find that many beginning to intermediate SAS programmers are not familiar with temporary arrays. The good news is that there is nothing complicated about them and they are very useful. First of all, what is a temporary array?

Let's start with a "regular" array like this:

array x[10] x1-x10;

This array, called x is associated with the 10 variables x1-x10. (Remember, the array name can be any valid SAS name, it doesn't have to have any relationship to the variables—but it usually does.) Now, how about a temporary array?

array y[10] _temporary_;

You use the keyword _TEMPORARY_ where you usually enter your variable names. There are no variables associated with a temporary array. In this example, you could reference y[1] or y[2], etc. but there are no variables y1, y2, etc.

My favorite use of a temporary array is for table lookup. You can load the elements of a temporary array and then use those elements to hold values. Here is an example:

You have sales goals for years 1-10 and the sales figures for all of your salespeople for these 10 years. You want to compare each person's sales with the goals for the 10 years. By placing the 10 sales goals in a temporary array, you can retrieve the goal amount by knowing the year. The temporary array elements are automatically retained and stored in memory, so they are a perfect for tasks such as this. Take a look at the following program to see how this works:

*Program to demonstrate temporary arrays;
data Revenue;
   array Goal[10] _temporary_;
   *Load the temporary array with values;
   if _n_ = 1 then do Year = 1 to 10;
      input Goal[Year] @;
   *Now input the sales data;
   input Name $ Year Sales;
   Difference = Sales – Goal[Year];
10 11 14 15 18 20 23 28 30 33
Fred 3 16
Joan 1 11
Helen 8 45
title "Listing of Data Set Revenue";
proc print data=Revenue noobs;

Here is the output:

You might want to define an array such as Goal like this:

array Goal[2001:2010] _temporary_;

where the subscript can be the actual value for a year. Notice the colon between the two years. This causes the subscripts for this array range from 2001 to 2010. Also, because arrays can be multi-dimensional, you can perform multi-way lookups. Give it a try.

You can learn more about temporary arrays from my book, Learning SAS by Example: A Programmer's Guide, available from SAS Press.


About Author

Ron Cody

Private Consultant

Ron Cody, EdD is a retired professor from the Robert Wood Johnson Medical School. He now works as a private consultant and a national instructor for SAS Institute Inc. A SAS user since 1977, Ron's extensive knowledge and innovative style have made him a popular presenter at local, regional, and national SAS conferences. He has authored or co-authored numerous books, as well as countless articles in medical and scientific journals.


  1. Good post. One thing to add: your examples are all numeric variables. Arrays can be derived for character variables, including temporary arrays. For character variable arrays, $ and the variable length must be added to the ARRAY statement.

  2. Thanks, Ron, I was just trying to find a way to explain temporary arrays and why you might want to use them to a more junior programmer and this is absolutely perfect. And I'll suggest the book :)

  3. Being a teacher, I'm a big fan of another example you used in a paper a while back (we are both old).

    * passing grade for each quiz in a temporary array;
    data pass;
    array pass_score (5) _temporary_ (85 90 88 90 95);
    array score(5);
    input id score1-score5;
    pass = 0;
    do j=1 to 5;
    taken = n(of score:);
    pass + score(j) ge pass_score(j);
    drop j;
    1 80 91 90 . 99
    2 70 90 88 90 95

  4. Rick Wicklin

    Great reminder, Ron. You never explicitly said it, but the primary value of a temporary arry is that the values do not appear in the final data set. Thus they are great for storing parameters or meta-parameters that you need to construct a data set, but do not want to appear in the final data. This means that you can use temporary arrays whose dimensions are different from the number of variables in data. For a cute example, see the article "How spammers generate random comments for blogs."

Leave A Reply

Back to Top