Date: Sun, 19 Dec 2004 10:56:53 -0600
Reply-To: "Dunn, Toby" <Toby.Dunn@TEA.STATE.TX.US>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Dunn, Toby" <Toby.Dunn@TEA.STATE.TX.US>
Subject: Re: to iterate a macro
Content-Type: text/plain; charset="iso-8859-1"
I see I have a slight code issue with the second example, it should have been:
%macro iter;
%let cnt = %eval(&cnt + 1);
%mend;
wouldn't get a desired result when used in:
%let cnt = 0;
Data one;
do i = 1 to 100;
%iter;
end;
run;
Since as you pointed out that macro's compile and execute at a different time than open SAS code but, the code would get the job done in:
%macro loop (max = 100);
%let cnt = 0;
%do i = 1 to &stop;
%iter;
%end;
%mend;
Toby
________________________________
From: Dunn, Toby
Sent: Sat 12/18/2004 12:03 PM
To: iw1junk@comcast.net; SAS(r) Discussion
Cc: Mayukh Dass
Subject: RE: to iterate a macro
Ian,
What I was getting at was (I probrably didn't make myself very clear) say you have:
%macro iter;
iter + 1;
%mend;
Since it has open SAS code inside of it, it would work in the case of:
Data one;
do i = 1 to 100;
%iter;
end;
run;
Since the macro definition has open SAS code in it, but wouldn't work for:
%macro loop (max = 100);
%do i = 1 to &stop;
%iter;
%end;
%mend;
On the other hand:
%macro iter;
%let cnt = 0;
%let cnt = %eval(&cnt + 1);
%mend;
wouldn't get a desired result when used in:
Data one;
do i = 1 to 100;
%iter;
end;
run;
Since as you pointed out that macro's compile and execute at a different time than open SAS code but, the code would get the job done in:
%macro loop (max = 100);
%do i = 1 to &stop;
%iter;
%end;
%mend;
Thus, it depends on what kind of SAS code you have in the %macro definition (pure macro, pure open SAS, a mix, etc...) as to where and how you can use it in your program to achieve the desired results. The poster said if I remember correctly that they wanted the macro to iterate say 100 times. The code they presented showed a distinct lack of knowledge between open SAS code and macro code, I was very uncertain as to what exactly they truelly intended, hence if you remember I also said show us your code inside the %macro definition and we could help you more.
However, you did write the following could be used:
data one;
do i=1 to 100;
call execute ( '%iterate' ) ;
end;
run;
True it would work, however I am often leary of telling people to use call symput and call execute, especially when I am either uncertain of their SAS abilities or they show a lack of base SAS knowledge. Why? they tend to mix macro and open SAS code execution times and possibly compile. I have found it hard enough to get people to understand the difference between macro and open SAS execution times when they aren't mixed, it is even harder for them to grasp the concept when they are mixed. That being said both serve a valid purpos and should not be eliminated from use but rather in my opinion (which may change with time) their use should be limited and very well documented when they are used.
Toby
**********************************************************************************
Toby,
In response to Mayukh Dass <mayukh.dass@gmail.com> asking
how to repeatedly call a macro, e.g.
> data one;
> do i=1 to 100;
> %iterate;
> end;
> run;
You wrote in part:
>It depends one what your macro definition actual has in it
>to how you can call it.
NO! That code will never work for any macro since the macro
executes at compile time, there is no loop.
Mayukh could use your macro %LOOP (although the obvious parameter
is missing) suggestion or to keep it in a DATA step.
data one;
do i=1 to 100;
call execute ( '%iterate' ) ;
end;
run;
<mailto:Ian_whitlock@comcast.net> --
Ian_Whitlock@comcast.net
|