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;
do k=1 to 5;
 call symputx('kk',k);
 j=k+1;
 s=J(1,2,0);
 s[1]=k;
 s[2]=j;
 create s&kk. from s;
 append from s;
 close s&kk.;
end;
quit;

 

Here is one example, and it seems that call symputx does not work in loops?

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

And Ian's answer not only app[lies to macro loops but to te standard DO loop as well.

 

data _null_;
do i = 1 to 6;
end;
put i;
run;

In fact, this behavior is not unique to SAS. It is the same in all programming languages that I have ever seen.

View solution in original post

9 REPLIES 9
Reeza
Super User
General rule of thumb is you can't use a macro variable in the same step as you create it due to timing issues. There are some ways to get around that using SYMGET but I'm not sure how that works in IML. You'd get similar behaviour in a data step.
supmilk
Obsidian | Level 7
%macro a();
proc iml;
%do k=1 %to 5;
 k=%sysevalf(&k);
 call symputx('kk',k);
 j=k+1;
 s=J(1,2,0);
 s[1]=k;
 s[2]=j;
 create s&kk. from s;
 append from s;
%end;
quit;
%mend;

%a();

I find that it works when I put iml into a macro, but I still do not know why?

IanWakeling
Barite | Level 11

As a general rule, you will almost never need to mix macros within IML, as a pure IML solution will exist.  For example you can use an IML loop instead of the macro loop, making data set names on the fly using string concatenation:

 

proc iml;
do k=1 to 5;
 j=k+1;
 s=J(1,2,0);
 s[1]=k;
 s[2]=j;
 create (cats('s',char(k))) from s;
 append from s;
end;
quit;
Rick_SAS
SAS Super FREQ

See the article "Macros and loops in the SAS/IML language," which explains why your program doesn't work the way you expected it to.

supmilk
Obsidian | Level 7

thank you for your reply, and one more question:

 

%macro stat(s_t);

proc iml;

%do kk=1 %to %sysevalf(&s_t);

kk=%sysevalf(&kk);

%end;

quit;

%put &kk.;

%mend;

%stat(6);

when I put iml program into a macro, the mixed program works, but %put result goes to 7. I can't find the answer from the blog "macros and loops in the SAS/IML language".

IanWakeling
Barite | Level 11

This is how an iterative macro do loop normally operates.  The documentation states "the value of macro-variable changes by the value of increment until the value of macro-variable is outside the range of integers included by start and stop".   So with a default increment of 1, the loop macro variable will be one more than than stop after the loop.

supmilk
Obsidian | Level 7

Thanks a lot for your help!

Rick_SAS
SAS Super FREQ

And Ian's answer not only app[lies to macro loops but to te standard DO loop as well.

 

data _null_;
do i = 1 to 6;
end;
put i;
run;

In fact, this behavior is not unique to SAS. It is the same in all programming languages that I have ever seen.

Rick_SAS
SAS Super FREQ

Do you still have questions? If not, please mark a response as the answer.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1182 views
  • 8 likes
  • 4 in conversation