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

Dr. Ron Cody was a Professor of Biostatistics at the Rutgers Robert Wood Johnson Medical School in New Jersey for 26 years. During his tenure at the medical school, he taught biostatistics to medical students as well as students in the Rutgers School of Public Health. While on the faculty, he authored or co-authored over a hundred papers in scientific journals. His first book, Applied Statistics and the SAS Programming Language, was first published by Prentice Hall in 1985 and is now in its fifth edition. Since then, he has published over a dozen books on SAS programming and statistical analysis using SAS. His latest book, A Gentle Introduction to Statistics Using SAS Studio was published this year. Ron has presented numerous papers at SAS Global forums, regional conferences, as well as local user groups. He is presently a contract instructor for SAS Institute and continues to write books on SAS and statistical topics.


  1. Jongdeuk Hong on

    you have to correct the minus symbol on this code.
    you may be copy this code and paste it on sas.
    if you do, retype "-"

  2. I am getting the following error while running Ron's code.

    77 if _n_ = 1 then do Year = 1 to 10;
    78 input Goal[Year] @;
    79 end;
    80 *Now input the sales data;
    81 input Name $ Year Sales;
    82 Difference = Sales – Goal[Year];
    ERROR 388-185: Expecting an arithmetic operator.

    ERROR 202-322: The option or parameter is not recognized and will be ignored.

  3. 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."

  4. 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

  5. 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 :)

  6. 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.

Back to Top