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?
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.
%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?
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;
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.
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".
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.
Thanks a lot for your help!
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.
Do you still have questions? If not, please mark a response as the answer.