BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JerryWu
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

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}));

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller
JerryWu
Fluorite | Level 6

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

Rick_SAS
SAS Super FREQ

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

 

JerryWu
Fluorite | Level 6

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

IanWakeling
Barite | Level 11

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}));
JerryWu
Fluorite | Level 6
This is exactly what I need! Yes, I agreed with you that to do so, the macro parameters should be relatively small (which in our situation, yes).
Thank you all for the valuable suggestions! These are extremely helpful!
Jerry

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1140 views
  • 5 likes
  • 4 in conversation