BookmarkSubscribeRSS Feed
Jonathanzz
Obsidian | Level 7

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?

8 REPLIES 8
Reeza
Super User

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?


 

novinosrin
Tourmaline | Level 20

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

Jonathanzz
Obsidian | Level 7

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

novinosrin
Tourmaline | Level 20

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

Jonathanzz
Obsidian | Level 7
I am happy now! lol
Thanks a lot for your help!
Reeza
Super User

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.

 

 

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

傳送自 Android 上的 Yahoo Mail
Ksharp
Super User
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;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 8 replies
  • 1288 views
  • 4 likes
  • 4 in conversation