Hello everyone. I wonder how I can multiply and substract matrices if they are presented as datasets in SAS. It would be great if anyone can show me an example of how I can do it with the two datasets below:
data matrix_1;
input a b c;
cards;
2 6 7
1 8 3
6 3 8
;
data matrix_2;
input a b c;
cards;
8 3 6
1 7 9
3 6 2
;
run;
proc print data = matrix_1 noobs; run;
proc print data = matrix_2 noobs; run;
Hi,
Surely, there is a way to do it with data steps, but you can also do it with the IML procedure.
data matrix_1;
input a b c;
cards;
2 6 7
1 8 3
6 3 8
;
data matrix_2;
input a b c;
cards;
8 3 6
1 7 9
3 6 2
;
run;
proc iml;
edit work.matrix_1;
read all var _NUM_ into matrix_1[colname=numVars];;
close work.matrix_1;
print matrix_1;
edit work.matrix_2;
read all var _NUM_ into matrix_2[colname=numVars];;
close work.matrix_2;
print matrix_2;
mult = matrix_1*matrix_2;
print mult;
subs = matrix_1 - matrix_2;
print subs;
run;
results
Hope that this can help you
Enrique
Hi,
Surely, there is a way to do it with data steps, but you can also do it with the IML procedure.
data matrix_1;
input a b c;
cards;
2 6 7
1 8 3
6 3 8
;
data matrix_2;
input a b c;
cards;
8 3 6
1 7 9
3 6 2
;
run;
proc iml;
edit work.matrix_1;
read all var _NUM_ into matrix_1[colname=numVars];;
close work.matrix_1;
print matrix_1;
edit work.matrix_2;
read all var _NUM_ into matrix_2[colname=numVars];;
close work.matrix_2;
print matrix_2;
mult = matrix_1*matrix_2;
print mult;
subs = matrix_1 - matrix_2;
print subs;
run;
results
Hope that this can help you
Enrique
Enrique gave the correct answer, although I recommend that you use the USE statement to read the matrices:
use matrix_1;
read all var _NUM_ into matrix_1;
close;
If needed, you can also read individual variables into SAS/IML vectors.
Moved the thread to the IML Forum.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.