I want to edit the table using multidimensional array. The codings are below:
Data dummytable;
input w1 w2 w3 w4;
cards;
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
;
run;
Data ABC;
array w(4,4)_temporary_;
set dummytable;
w(1,2)=1;
run;
[w(1,2)=1 --> I want to set the second variable of the first observation to 1. However, my code doesn't work.]
I want the table to be look like this:
0 1 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Can anyone tell me how to do it?
A 'multidimension' array still only works on a single line at a time.
This is not a good way to implement this in SAS unless you're trying to learn IML which is similar to R or Python.
One way to accomplish your requirements in this case is using the row number with the automatic variable (_N_) and then code w2 directly. You could create an array for that part if desired.
data want;
set dummytable;
array w(4) w1-w4;
if _n_=2 then w(2) = 1;
*if _n_ = 2 then w2=1; *Equivalent code but doesn't need array statement;
run;
@Jonathanzz wrote:
I want to edit the table using multidimensional array. The codings are below:
Data dummytable;
input w1 w2 w3 w4;
cards;
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
;
run;
Data ABC;
array w(4,4)_temporary_;
set dummytable;
w(1,2)=1;
run;
[w(1,2)=1 --> I want to set the second variable of the first observation to 1. However, my code doesn't work.]
I want the table to be look like this:
0 1 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Can anyone tell me how to do it?
Hi, Your approach is almost right, but you didn't frame the array statement into multidimension table at compile time. I like the way you are learning though. Here is your solution:
Data dummytable;
input w1 w2 w3 w4;
cards;
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
;
run;
/*solution*/
data abc;
set dummytable;
array w(2,2) w1 w2 w3 w4;
if _n_=1 then w(1,2)=1;
run;
Regards,
Naveen Srinivasan
Good solution! Thanks a lot for your help! But I have further questions want to ask.
If I also want to edit w(3,3)=1
0 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
Or w(4,4)=1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
How should I do it?
They will out of range went the array is w(2,2).
No, won't go out of range-->
Data dummytable;
input w1 w2 w3 w4;
cards;
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
;
run;
/* Your additional needs*/
data abc;
set dummytable;
array w(2,2) w1 w2 w3 w4;
if _n_=1 then w(1,2)=1;
else if _n_=3 then w(2,1)=1;
else if _n_=4 then w(2,2)=1;
run;
Happy now? lol
Regards,
Naveen Srinivasan
Just to be pedantic there is absolutely no value in doing it this way, except to demonstrate a multidimension array. The multidimension array still only references a single row and a single set of variables. This method would fall under code obfuscation IMO.
Data step is not matrix oriented language, so switch into IML code to make it happen . Data dummytable; input w1 w2 w3 w4; cards; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; run; proc iml; use dummytable; read all var _all_ into w[c=vnames]; close; w[1,2]=1; print w[c=vnames]; create want from w[c=vnames]; append from w; close; run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.