DATA Step, Macro, Functions and more

Questions about multidimensional array

Reply
Occasional Contributor
Posts: 7

Questions about multidimensional array

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?

Super User
Posts: 19,770

Re: Questions about multidimensional array

Posted in reply to Jonathanzz

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?


 

PROC Star
Posts: 283

Re: Questions about multidimensional array

Posted in reply to Jonathanzz

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

Occasional Contributor
Posts: 7

Re: Questions about multidimensional array

Posted in reply to novinosrin

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

PROC Star
Posts: 283

Re: Questions about multidimensional array

Posted in reply to Jonathanzz

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

Occasional Contributor
Posts: 7

Re: Questions about multidimensional array

Posted in reply to novinosrin
I am happy now! lol
Thanks a lot for your help!
Super User
Posts: 19,770

Re: Questions about multidimensional array

Posted in reply to novinosrin

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.

 

 

Occasional Contributor
Posts: 7

Re: Questions about multidimensional array

Yes! Agree! But my school assignment requires us to work with multidimensional array similar to this. /_\

傳送自 Android 上的 Yahoo Mail
Super User
Posts: 10,018

Re: Questions about multidimensional array

Posted in reply to Jonathanzz
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;


Ask a Question
Discussion stats
  • 8 replies
  • 140 views
  • 4 likes
  • 4 in conversation