Discussion:
Leaving Do loop in %Do
(too old to reply)
taran gujral
2004-01-29 04:34:24 UTC
Permalink
hello folks ,

i wanted to know how we could leave a %do loop as
we leave Do loop with a leave .

like

Do i =1 to 5;

if i = 4 then leave;
end;

what can i use if i have %do loop

thanks


---------------------------------
Chat instantly with your online friends?
Get the FREE Yahoo!Messenger
Jack Hamilton
2004-01-29 06:54:39 UTC
Permalink
Use a %GOTO to a label immediately after the loop

=====
%macro x;
%do i = 1 %to 12;
%if &i = 4 %then %goto exitloop;
&i
%end;
%exitloop:
%mend x;

%put %x;
=====

prints

1 2 3

Gotos can lead to trouble, and so their use should be restricted to
cases like this one.




--
***@FirstHealth.com
Manager, Technical Development
Metrics Department, First Health
West Sacramento, California USA
hello folks ,

i wanted to know how we could leave a %do loop as
we leave Do loop with a leave .

like

Do i =1 to 5;

if i = 4 then leave;
end;

what can i use if i have %do loop

thanks


---------------------------------
Chat instantly with your online friends?
Get the FREE Yahoo!Messenger
SUBSCRIBE SAS-L Stephane
2004-01-29 09:28:46 UTC
Permalink
Your %do statement is open when you do this. Be careful with the nested loops.

Stéphane.


----Message d'origine----
Date: Wed, 28 Jan 2004 23:54:39 -0700
Sujet: Re: Leaving Do loop in %Do
Use a %GOTO to a label immediately after the loop
=====
%macro x;
%do i = 1 %to 12;
%if &i = 4 %then %goto exitloop;
&i
%end;
%mend x;
%put %x;
=====
prints
1 2 3
Gotos can lead to trouble, and so their use should be restricted to
cases like this one.
--
Manager, Technical Development
Metrics Department, First Health
West Sacramento, California USA
hello folks ,
i wanted to know how we could leave a %do loop as
we leave Do loop with a leave .
like
Do i =1 to 5;
if i = 4 then leave;
end;
what can i use if i have %do loop
thanks
---------------------------------
Chat instantly with your online friends?
Get the FREE Yahoo!Messenger
Chang Y. Chung
2004-01-29 14:56:01 UTC
Permalink
On Thu, 29 Jan 2004 04:34:24 +0000, =?iso-8859-1?q?taran=20gujral?=
Post by taran gujral
hello folks ,
i wanted to know how we could leave a %do loop as
we leave Do loop with a leave .
like
Do i =1 to 5;
if i = 4 then leave;
end;
what can i use if i have %do loop
Hi, taran_jit,

To my knowledge (which isn't much -- I should add :-)), there is no %leave
macro statement. You can %goto out of the loop, but your boss may not
approve of your using any kinds of goto's. In this case, what you can do
is re-write the %do loop using either %do %while or %do %until. For
example,

%macro test1;
%local i;
%let i = 1;
%do %while (&i. ^= 4);
%put &i.;
%let i = %eval(&i. + 1);
%end;
%mend;
%test1;
/* on log
1
2
3
*/

The difference between %do %while and %do %until is when the check for the
condition occurs -- top of the loop for %while, and the bottom for %until.

And there is this hack that manipulates the index -- which your boss may
find not acceptable, either. :-)

%macro test2;
%local i;
%do i = 1 %to 5;
%if &i.=4 %then %let i = 6; /* exit loop */
%else %put &i.;
%end;
%mend;
%test2;
/* on log
1
2
3
*/

Cheers,
Chang
Jack Hamilton
2004-01-29 19:12:18 UTC
Permalink
Post by SUBSCRIBE SAS-L Stephane
Your %do statement is open when you do this. Be careful with the nested loops.
Really ?

could you explain this ?

Under what circumstances will the code below cause a problem? The documentation warns against branching into the middle of a loop, but not against branching out.





--
***@FirstHealth.com
Manager, Technical Development
Metrics Department, First Health
West Sacramento, California USA
Your %do statement is open when you do this. Be careful with the nested loops.

Stéphane.


----Message d'origine----
Post by SUBSCRIBE SAS-L Stephane
Date: Wed, 28 Jan 2004 23:54:39 -0700
Sujet: Re: Leaving Do loop in %Do
Use a %GOTO to a label immediately after the loop
=====
%macro x;
%do i = 1 %to 12;
%if &i = 4 %then %goto exitloop;
&i
%end;
%mend x;
%put %x;
=====
prints
1 2 3
Gotos can lead to trouble, and so their use should be restricted to
cases like this one.
--
Manager, Technical Development
Metrics Department, First Health
West Sacramento, California USA
hello folks ,
i wanted to know how we could leave a %do loop as
we leave Do loop with a leave .
like
Do i =1 to 5;
if i = 4 then leave;
end;
what can i use if i have %do loop
thanks
---------------------------------
Chat instantly with your online friends?
Get the FREE Yahoo!Messenger
Loading...