BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Minh2710
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
joseenrique1
SAS Employee

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;

resultsresults

Hope that this can help you

 

Enrique

View solution in original post

4 REPLIES 4
joseenrique1
SAS Employee

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;

resultsresults

Hope that this can help you

 

Enrique

Rick_SAS
SAS Super FREQ

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.

Minh2710
Obsidian | Level 7
Thank you very much. It worked like a charm.
PeterClemmensen
Tourmaline | Level 20

Moved the thread to the IML Forum.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 753 views
  • 0 likes
  • 4 in conversation