DATA Step, Macro, Functions and more

How do I create a dataset from a Multidimensional Array?

Reply
New User
Posts: 1

How do I create a dataset from a Multidimensional Array?

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?

SAS Super FREQ
Posts: 8,866

Re: How do I create a dataset from a Multidimensional Array?

[ Edited ]

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

Super User
Posts: 19,817

Re: How do I create a dataset from a Multidimensional Array?

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. 

Super User
Posts: 7,809

Re: How do I create a dataset from a Multidimensional Array?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 3 replies
  • 214 views
  • 1 like
  • 4 in conversation