Date: Tue, 4 Apr 2000 09:28:43 +0200
Reply-To: Jim Groeneveld <J.Groeneveld@ITGROUPS.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jim Groeneveld <J.Groeneveld@ITGROUPS.COM>
Subject: Re: MACRO error: Invalid branch into iterative %DO
Content-Type: text/plain; charset="iso-8859-1"
Torben,
Right, it will almost (but not yet completely correct) work that way; I
still have some comments.
My first solution introduced the macro variables Start and Stop, just like
MN and Loop are macro variables. These are not dataset variables.
> %MACRO DO_Q2; %* [Why not a parameter mn here as an argument?];
> >>> %* [I think (but I can be wrong ?) that parameters used as arguments
> are mostly for "manually" run macros ?]
What do you mean with "manually run macros"? I run all my programs in batch
and use a set of standard macros for standard functionality. These macros
have several parameters, which take different values each time. Any macro
may have parameters. You do not need to define the value of a macro variable
before calling the macro, but you may do it while calling the macro. In your
case the macro would start with <%MACRO DO_Q2 (mn);> and would be called
from other code with <%DO_Q2 (25);> or whatever value, like 999.
> %IF %EVAL(&mn) EQ 999 %THEN
> %DO mn= 1 %TO 3;
> %LET loop = yes;
> %CODE;
> %END;
> %ELSE %CODE;
> %IF &loop eq 'yes' %THEN %LET mn=%EVAL(&mn-1);
> %MEND DO_Q2;
You added the macro variable Loop. There is nothing wrong with it, but I
thought a value of 999 for &mn would be as dintinctive for the last
statement in the macro. Furthermore you define your Loop variable as many
times as the loop is run, 3 times here. And you may have to reset Loop to a
different value in case MN is not 999 to prevent unwanted effect from a
previous run of the macro if Loop is a global macro variable. Finally, in
your piece of code <%IF &loop eq 'yes' %THEN> the quotes around 'yes' _must_
be removed: a macro statement like this always compares text to other text,
a macro variable's contents to text. Leaving the quotes never makes your
condition true.
I would suggest modifying your code a little, becoming:
%IF %EVAL(&mn) EQ 999 %THEN
%DO; %* an extra %DO-%END block, in which Loop is defined;
%LET loop = yes;
%DO mn= 1 %TO 3; %* The actual loop;
%CODE;
%END;
%END;
%ELSE
%DO; %* an extra %DO-%END block, in which Loop is defined;
%LET Loop = no; %* else Loop might remain yes from a previous run;
%CODE;
%END;
%IF &loop eq yes %THEN %LET mn=%EVAL(&mn-1); %* no quotes around yes;
%MEND DO_Q2;
An other alternative for the whole macro, without a Loop variable, is:
%MACRO DO_Q2 (mn);
%IF %EVAL(&mn) EQ 999 %THEN
%DO;
%DO mn= 1 %TO 3; %* The actual loop;
%CODE;
%END;
%LET mn=%EVAL(&mn-1); %* note this statement has been moved and no
condition needed;
%END;
%ELSE %CODE;
%MEND DO_Q2;
I hope this gives you some more insight and ideas. Good luck.
Regards - Jim.
--
Y. Groeneveld, MSc IMRO TRAMARKO tel. +31 412 407 070
senior statistician, P.O. Box 1 fax. +31 412 407 080
head IT department 5350 AA BERGHEM IMRO TRAMARKO: a CRO
J.Groeneveld@ITGroups.com the Netherlands in clinical research
My computer allows me to be buggy, imperfect; my wife ......
> -----Original Message-----
> From: Torben Haslund [SMTP:Torben.Haslund@vbiol.slu.se]
> Sent: Tuesday, April 04, 2000 8:56 AM
> To: Jim Groeneveld
> Subject: RE: MACRO error: Invalid branch into iterative %DO
>
> Jim,
>
> Thank you for considering.
> Your first solution introduces new variables start and stop. I prefer your
> second solution with a sligth modification, which I find the most clear:
>
> %MACRO DO_Q2; %* [Why not a parameter mn here as an argument?];
>
> >>> %* [I think (but I can be wrong ?) that parameters used
> as arguments
> are mostly for "manually" run macros ?]
>
> %IF %EVAL(&mn) EQ 999 %THEN
> %DO mn= 1 %TO 3;
> %LET loop = yes;
> %CODE;
> %END;
> %ELSE %CODE;
> %IF &loop eq 'yes' %THEN %LET mn=%EVAL(&mn-1);
> %MEND DO_Q2;
>
[.........]