Hello,
I'd like to ask for help.
Here is my main body of PROC IML:
proc iml;
weights = {0 0.5,
0.5 0,
1 1};
counts = {145 34,
71 59,
76 38};
...
I am wondering how I can write a macro such that "counts" is entered as a macro variable when calls for the macro. And then, this variable will be read into proc iml just as what I showed above?
I am new to this area, so my question sounds naive.
Any suggestion is appreciated.
Thanks
Jerry
You could use something like my example below, but clearly if the matrices that you want to pass as macro parameters are not very small, then it would be much better to use Rick's solution and read the data from SAS data sets.
options macrogen;
%macro testmacro(count=);
proc iml;
c = &count;
d = 2#c;
print c d;
quit;
%mend;
%testmacro(count=%str({11 26, 42 8, 79 33}));
%let matrix_name=counts;
proc iml;
weights = {0 0.5,
0.5 0,
1 1};
&matrix_name = {145 34,
71 59,
76 38};
So that's how you would use a macro variable. I don't see a need for a macro here, you'd have to explain a lot more why a macro is needed and what a macro would do here.
Thank you so much for the quick response.
Sorry that I did not explain my question clearly.
What I intend to do is:
I want to write a macro, for example: %macro testmacro(counts=);, where counts is a 3*2 matrix. Next, inside this macro, I will run proc iml to perform several tasks (based on some algorithm) via the elements of "counts" matrix.
Now, can I run such macro as: %testmacro(count=(17 24, 36 49, 62, 98)), so that I can import this 3*2 matrix into proc iml?
Sincerely
Jerry
As Paige says, it is not clear what the macro is for. Maybe you are asking about reading the data from a data set and you want the name of the data set in a macro variable? If so, you can use this
data Weights;
input w1-w2;
datalines;
0 0.5
0.5 0
1 1
;
data Counts;
input c1-c2;
datalines;
145 34
71 59
76 38
;
%let WtName = Weights;
%let CntName = Counts;
proc iml;
use &WtName;
read all var _num_ into Weights;
close;
use &CntName;
read all var _num_ into Counts;
close;
print Weights, Counts;
For more about reading data into matrices, see https://blogs.sas.com/content/iml/2012/01/16/reading-all-variables-into-a-matrix.html
Hi, Rick:
This might be a possible solution.
However, what I truly need to do is: define a macro (similar to a function) to perform some calculations based on matrix and return a final answer. Doing so, I'd like to enter the matrix as part of the parameter when I call to run this macro, so that it can be read into IML.
Hopefully it explains.
Sincerely
Jerry
You could use something like my example below, but clearly if the matrices that you want to pass as macro parameters are not very small, then it would be much better to use Rick's solution and read the data from SAS data sets.
options macrogen;
%macro testmacro(count=);
proc iml;
c = &count;
d = 2#c;
print c d;
quit;
%mend;
%testmacro(count=%str({11 26, 42 8, 79 33}));
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.