Statistical programming, matrix languages, and more

Proc IML- Matrix element operation

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Proc IML- Matrix element operation

Hi Guys,

 

I'm new to IML and need some help to start with. hope someone will help.

 

Below ,are the two matrix which i have. I want a new matrix, by dividing the Sum_row values of second matrix with each element of the first. So, the output matrix first row should be: 

A 11/185   22/185 33/185   44/185  55/185

B 89/346   45/346   55/346  59/346  63/346

... so on and so forth.  Any help, will really be appreciated.

 

product.png


Accepted Solutions
Solution
‎02-14-2018 12:57 PM
Super User
Posts: 10,770

Re: Proc IML- Matrix element operation

Posted in reply to akpattnaik
data a;
input a b c d e;
cards;
11 22 33 44 55
89 45 55 59 63
66  0 99  0  0 
66  0 99  0  0 
66  0 99  0  0 
;
run;

data b;
input a b c d e;
cards;
11 22 33 44 55
89 45 55 59 63
66  0 99  0  0 
66  0 99  0  0 
66  0 99  0  0 
;
run;

proc iml;
use a;
read all var _num_ into x[c=vname];
close;
use b;
read all var _num_ into y;
close;

colSums = y[+, ];
want=x/colSums ;

create want from want[c=vname r=vname];
append from want[r=vname];
close;
quit;

proc print noobs;run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 4,240

Re: Proc IML- Matrix element operation

[ Edited ]
Posted in reply to akpattnaik

SAS/IML supports a shorthand notation for common operations on rows and columns, including sums, products, sums of squares, min, max, and means. Collectively, these are known as "subscript reduction operators." You can read about them at

"Use subscript reduction operations."

 

For your problem, use 

 


proc iml;
M = {11 22 33 44 55,
     89 45 55 59 63,
     66  0 99  0  0 
     /* etc */   };
colSums = M[+, ];
print colSums;
P = M / colSums;
Super User
Posts: 10,770

Re: Proc IML- Matrix element operation

Posted in reply to akpattnaik
proc iml;
first={11 22 33 44 55,
     89 45 55 59 63,
     66  0 99  0  0 
     /* etc */   };

second = {11 22 33 44 55,
     89 45 55 59 63,
     66  0 99  0  0 
     /* etc */   };
colSums = M[+, ];
print colSums;


want=first/colSums ;
print want;
quit;
Contributor
Posts: 20

Re: Proc IML- Matrix element operation

Hi Ksharp,

 

Thanks for your reply. Its all good and its all working fine. However, it only gives me the numeric values. I want all the variables together.

Output i receive is as follows:

 ABCDE
ROW1ValueValueValueValueValue
ROW2ValueValueValueValueValue
ROW3ValueValueValueValueValue
ROW4ValueValueValueValueValue
ROW5ValueValueValueValueValue

 

P.S. the value represents the resultant value post division.( as per my expectation)

 

But, i need  below as output, i.e. the character value(Product name) columwise and calculated values in the rows. 

Product NameABCDE
AValueValueValueValueValue
BValueValueValueValueValue
CValueValueValueValueValue
DValueValueValueValueValue
EValueValueValueValueValue

 

Massive thanks for your support Sir.

 

 

Solution
‎02-14-2018 12:57 PM
Super User
Posts: 10,770

Re: Proc IML- Matrix element operation

Posted in reply to akpattnaik
data a;
input a b c d e;
cards;
11 22 33 44 55
89 45 55 59 63
66  0 99  0  0 
66  0 99  0  0 
66  0 99  0  0 
;
run;

data b;
input a b c d e;
cards;
11 22 33 44 55
89 45 55 59 63
66  0 99  0  0 
66  0 99  0  0 
66  0 99  0  0 
;
run;

proc iml;
use a;
read all var _num_ into x[c=vname];
close;
use b;
read all var _num_ into y;
close;

colSums = y[+, ];
want=x/colSums ;

create want from want[c=vname r=vname];
append from want[r=vname];
close;
quit;

proc print noobs;run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 295 views
  • 5 likes
  • 3 in conversation