BookmarkSubscribeRSS Feed
RobertWF1
Quartz | Level 8

I don't recall how to create, initialize, and populate an array in SAS within a data step if the dimension of the array is a variable.

 

I've experimented with using dim_array and the macro variable &dim_array_m in the following code but neither work.

 

Any help is much appreciated! Thanks

 

data _null_;
  rows = 3;
  columns = 4;
  dim_array = rows*columns;
  put dim_array;
  /* %let dim_array_m = dim_array;*/
  /* %put &dim_array_m;*/
  array test_array{*} (dim_array_m*(0)); /* Create array and initialize with 12 zeroes. */
  do i=1 to dim_array; /* Populate array with values 1-12. */
    test_array{i} = i;
    put test_array{i};
  end;
run;

11 REPLIES 11
PaigeMiller
Diamond | Level 26

What are the variable names in this array? Give examples, please.

--
Paige Miller
RobertWF1
Quartz | Level 8
Do we need variable names or can we use test_array{1}, test_array{2}, etc. as the variables?
PaigeMiller
Diamond | Level 26

@RobertWF1 wrote:
Do we need variable names or can we use test_array{1}, test_array{2}, etc. as the variables?

That's pretty much up to you ... but its really really really hard to give advice without concrete examples that illustrate the differences in variable names that you will see from data set to data set in the variable names that go into the array. We need a few concrete examples.

--
Paige Miller
Reeza
Super User
data _null_;
  rows = 3;
  columns = 4;
  dim_array = rows*columns;
  call symputx('dim_array', dim_array);
run;

data want;
set .....;
  array test_array{&dim_array} (&dim_array*(0)); /* Create array and initialize with 12 zeroes. */
  do i=1 to &dim_array; /* Populate array with values 1-12. */
    test_array{i} = i;
    put test_array{i};
  end;
run;

@RobertWF1 wrote:

I don't recall how to create, initialize, and populate an array in SAS within a data step if the dimension of the array is a variable.

 

I've experimented with using dim_array and the macro variable &dim_array_m in the following code but neither work.

 

Any help is much appreciated! Thanks

 

data _null_;
  rows = 3;
  columns = 4;
  dim_array = rows*columns;
  put dim_array;
  /* %let dim_array_m = dim_array;*/
  /* %put &dim_array_m;*/
  array test_array{*} (dim_array_m*(0)); /* Create array and initialize with 12 zeroes. */
  do i=1 to dim_array; /* Populate array with values 1-12. */
    test_array{i} = i;
    put test_array{i};
  end;
run;


 

RobertWF1
Quartz | Level 8
Thanks Reeza!

And looks like I can combine the two data steps together:

data _null_;
rows = 3;
columns = 4;
dim_array = rows*columns;
call symputx('dim_array', dim_array);
array test_array{&dim_array} (&dim_array*(0)); /* Create array and initialize with 12 zeroes. */
do i=1 to &dim_array; /* Populate array with values 1-12. */
test_array{i} = i;
put test_array{i};
end;
run;
Reeza
Super User

@RobertWF1 wrote:
Thanks Reeza!

And looks like I can combine the two data steps together:

data _null_;
rows = 3;
columns = 4;
dim_array = rows*columns;
call symputx('dim_array', dim_array);
array test_array{&dim_array} (&dim_array*(0)); /* Create array and initialize with 12 zeroes. */
do i=1 to &dim_array; /* Populate array with values 1-12. */
test_array{i} = i;
put test_array{i};
end;
run;

 If you create a macro variable it's not usable in that same data step. If it works in your tests, it's likely because it was defined before as well (initial test). 

Try it in a new session and see if your code still works. 

 

 

Tom
Super User Tom
Super User

No. No. No. No.

 

You cannot use the value of a macro variable you have not created yet to generate the code to create a macro variable.  The CALL SYMPUTX() function cannot travel back in time.

RobertWF1
Quartz | Level 8
Ah you're right, has to be two data steps (can both be _null_).

It actually worked in one data step, but only because I had already run Reeza's code as two separate data steps so dim_array had been defined.
Tom
Super User Tom
Super User

The data step complier needs to know the dimensions so it can finish compiling the data step.  So you cannot use the value of any variable to define an array.

 

But if you have the value in a macro variable you should not have any trouble. The macro processor finishes converting your macro code into SAS code BEFORE that datastep compiler starts trying to understand what SAS code you want to run.

%let n_rows=3;
%let n_cols=4;
data _null_;
  array test_array[&n_rows,&n_cols] (%eval(&n_rows*&n_cols)*0);
  do i=1 to dim(test_array,1);
    do j=1 to dim(test_array,2);
      test_array[i,j] = (i-1)*&n_cols + j;
      put i= j= test_array[i,j]=;
    end;
  end;
run;

Results

1178    %let n_rows=3;
1179    %let n_cols=4;
1180    data _null_;
1181      array test_array[&n_rows,&n_cols] (%eval(&n_rows*&n_cols)*0);
1182      do i=1 to dim(test_array,1);
1183        do j=1 to dim(test_array,2);
1184          test_array[i,j] = (i-1)*&n_cols + j;
1185          put i= j= test_array[i,j]=;
1186        end;
1187      end;
1188    run;

i=1 j=1 test_array1=1
i=1 j=2 test_array2=2
i=1 j=3 test_array3=3
i=1 j=4 test_array4=4
i=2 j=1 test_array5=5
i=2 j=2 test_array6=6
i=2 j=3 test_array7=7
i=2 j=4 test_array8=8
i=3 j=1 test_array9=9
i=3 j=2 test_array10=10
i=3 j=3 test_array11=11
i=3 j=4 test_array12=12
Astounding
PROC Star

A few ideas in case they will help ...

 

If you don't want to name the variables, you don't have to.  The alternative is to create a temporary array, one that vanishes once the DATA step is over:

array t {100} _temporary_;

Just as with a regular array composed of variables,  you can assign initial values.  One major difference is that _temporary_ array elements are automaticvally retained.  Define enough variables that you won't need to use them all.

 

Even if you want a two-dimensional array, you can use a one-dimensional array instead.  Just use a formula to indicate which array element you want to refer to.  For example, if you have a DATA step variable named ROW_SIZE, you can refer to the second element of the 4th row using:

 

t{(4-1)*(row_size) + 2} = 0;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 11 replies
  • 1223 views
  • 4 likes
  • 5 in conversation