BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
supmilk
Obsidian | Level 7
proc iml;
b={1 2,3 4,5 6};
k=nrow(b);
quit;

%macro try();

%do k=1 %to k;
proc iml;
t=exp(k);
print t;
quit;
%end;

%mend;

Hey Guys,

 

Here is an example. I want to transmit the number of rows of the matrix b to the interation steps in the macro. How can I do?

 

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

OK. Then use SYMPUTX, as suggested:

 

proc iml;
a=1;  b=2;  c=a+b;
call symputx("c", c);
quit;

%put &=c;

%macro test(val);
  data _null_;
     sq = &val.**2;
     put sq=;
  run; 
%mend;

%test(&c);

 

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Instead of doing this via a macro, you could do all the looping inside of PROC IML, in which case the problem goes away.

--
Paige Miller
supmilk
Obsidian | Level 7

Yes, i agree with you, but here is just an example. What I want to know is how to transmit the value but not just this example.

PaigeMiller
Diamond | Level 26

I will stick with my answer above. A macro isn't needed here.

 

If your example is really more complicated than this, then convince me that a macro is needed and that you can't do this via inside of one large PROC IML step. I cannot conceive of a situation where you need to run PROC IML to get a value, transfer that value to a macro variable, and then use the value in the macro variable in another run of PROC IML, when the very simple alternative is to calculate the value in PROC IML and then continue to use that value in IML as you do more calculations in IML.

--
Paige Miller
Tom
Super User Tom
Super User

No idea how you can create macro variables from IML.  Does it support the CALL SYMPUTX() function?

You could have IML store the value into a dataset and then use a data step to write the macro variable using CALL SYMPUTX().

Ksharp
Super User

Why not post it at IML forum since it is about IML question ?

@Rick_SAS is there.

 

proc iml;
b={1 2,3 4,5 6};
_k=nrow(b);
do k=1 to _k;
t=exp(k);
print t;
end;
quit;
Rick_SAS
SAS Super FREQ

As @PaigeMiller indicates, most experienced SAS/IML programmers avoid macro loops because they are inefficient and unnecessary in a language that has its own looping structures. 

 

You indicate that your "real" use case is more complicated than this example, If you tell us what you are trying to accomplish, we can probably advise a robust and efficient way to accomplish your goal.

 

If you wish to pursue this macro method, you can call the SYMPUTX subroutine in IML. Be sure to also read the article "Macros and loops in the SAS/IML language", which discusses an example that shows how to understand the macro pre-processor in an interactive procedure such as PROC IML.

supmilk
Obsidian | Level 7

My goal is to try to take some computed result as a parameter of macro step or data step. We can forget the loops in my example. Another example: I want to pick up the value of c, and then set it as a parameter of macro test.

 

proc iml;
a=1;
b=2;
c=a+b;
quit;

macro test(c);
********
mend;
Rick_SAS
SAS Super FREQ

OK. Then use SYMPUTX, as suggested:

 

proc iml;
a=1;  b=2;  c=a+b;
call symputx("c", c);
quit;

%put &=c;

%macro test(val);
  data _null_;
     sq = &val.**2;
     put sq=;
  run; 
%mend;

%test(&c);

 

supmilk
Obsidian | Level 7

Thanks a lot!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 9 replies
  • 1394 views
  • 5 likes
  • 5 in conversation