BookmarkSubscribeRSS Feed
lilam
Calcite | Level 5

I have several multidimensional arrays created within a datastep. Is there an easy way to save these arrays as datasets outside of the datastep in which they have been created?

3 REPLIES 3
Cynthia_sas
SAS Super FREQ

Hi,
A SAS Array is a convenient way to create a reference to a group of variables. So unless you defined your arrays as temporary, your variables are already stored in a SAS dataset, as variables.

Let's say that you have an ARRAY statement like this in your program:
ARRAY ARR (5) v1 v2 v3 v4 v5;
or
ARRAY LL (4) $ lucy ricky fred ethel;

So, depending on whether your variables exist already or whether you are creating them, the ARRAY statement gives you a way to reference the variables as though they were members of an ARRAY. But the ARRAY in SAS is for reference purposes only in a program; outside of the program, in your SAS dataset, the variable values are stored under the individual variable names.

Please see this paper for a longer explanation:
https://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

cynthia

Reeza
Super User

Arrays in SAS are not objects they are simply a way to reference variables. 

 

As Cynthia has indicated they'll be saved, but probably not in a format you want. 

Kurt_Bremser
Super User

Just a short code snippet to illustrate how one could write the contents of an array to a dataset; note that the dataset in question is always named in the data ...; statement.

data test1 (drop=i j);
array x{2,3};
do i = 1 to 2;
  do j = 1 to 3;
    x{i,j} = i*j;
  end;
end;
run;

proc print noobs;
run;

data test2 (keep=i j value);
array x{2,3};
do i = 1 to 2;
  do j = 1 to 3;
    x{i,j} = i*j;
    value = x{i,j};
    output;
  end;
end;
run;

proc print noobs;
run;

This creates the following outputs:

x1    x2    x3    x4    x5    x6

 1     2     3     2     4     6
                                

        i    j    value

        1    1      1  
        1    2      2  
        1    3      3  
        2    1      2  
        2    2      4  
        2    3      6  

Also note that no input dataset was involved, which means both data steps underwent only 1 iteration. With an input dataset, everything would be repeated for each observation in the dataset.

If you wanted to create a separate dataset for eacjh input observation, more complicated programming would be necessary; I could imagine solving that with the use of a hash object.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 968 views
  • 1 like
  • 4 in conversation