Discussion:
skip a loop to next in a sas macro
(too old to reply)
sas_9264
2007-07-26 16:59:53 UTC
Permalink
Hi, I have following test code:

%macro test2;

%do i = 1 %to 5;
%if i = 3 %then %goto continue;
%put i = &i.;
%continue:
%end;
run;
%mend;
%test2

it output to log as:

i = 1
i = 2
i = 3
i = 4
i = 5

what I expect:
i = 1
i = 2
i = 4
i = 5
What do I miss to get expect result?

Thanks a lot,

shiping
toby dunn
2007-07-26 17:15:40 UTC
Permalink
Shipng ,

If you are using V9 use %Return:


%macro test2;

%do i = 1 %to 5;
%if i = 3 %then %return ;
%put i = &i.;
%end;

%mend test2 ;

%test2



Toby Dunn

If anything simply cannot go wrong, it will anyway. Murphys Law #2.

The buddy system is essential to your survival; it gives the enemy somebody
else to shoot at.
Murphys Law #


Tell a man there are 300 billion stars in the universe and he'll believe
you. Tell him a bench has wet paint on it and he'll have to touch to be
sure. Murphys Law #9






From: sas_9264 <***@GMAIL.COM>
Reply-To: sas_9264 <***@GMAIL.COM>
To: SAS-***@LISTSERV.UGA.EDU
Subject: skip a loop to next in a sas macro
Date: Thu, 26 Jul 2007 16:59:53 -0000

Hi, I have following test code:

%macro test2;

%do i = 1 %to 5;
%if i = 3 %then %goto continue;
%put i = &i.;
%continue:
%end;
run;
%mend;
%test2

it output to log as:

i = 1
i = 2
i = 3
i = 4
i = 5

what I expect:
i = 1
i = 2
i = 4
i = 5
What do I miss to get expect result?

Thanks a lot,

shiping

_________________________________________________________________
Need a brain boost? Recharge with a stimulating game. Play now!
http://club.live.com/home.aspx?icid=club_hotmailtextlink1
Terjeson, Mark
2007-07-26 17:03:04 UTC
Permalink
Hi,

To have the %IF reference the macro
variable "i" you need an ampersand.


%macro test2;

%do i = 1 %to 5;
%if &i = 3 %then %goto continue;
%put i = &i.;
%continue:
%end;
run;
%mend;
%test2




Hope this is helpful.


Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investments


Russell Investments
Global Leaders in Multi-Manager Investing






-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
sas_9264
Sent: Thursday, July 26, 2007 10:00 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: skip a loop to next in a sas macro

Hi, I have following test code:

%macro test2;

%do i = 1 %to 5;
%if i = 3 %then %goto continue;
%put i = &i.;
%continue:
%end;
run;
%mend;
%test2

it output to log as:

i = 1
i = 2
i = 3
i = 4
i = 5

what I expect:
i = 1
i = 2
i = 4
i = 5
What do I miss to get expect result?

Thanks a lot,

shiping
Fehd, Ronald J. (CDC/CCHIS/NCPHI)
2007-07-26 17:15:24 UTC
Permalink
Post by toby dunn
From: sas_9264
%macro test2;
%do i = 1 %to 5;
%if i = 3 %then %goto continue;
%put i = &i.;
%end;
run;
%mend;
%test2
i = 1
i = 2
i = 3
i = 4
i = 5
i = 1
i = 2
i = 4
i = 5
What do I miss to get expect result?
This paper explains do loops in both data step and macro code,
and provides examples that you may find helpful.

sgf2007.067:
DO Which? Loop, UNTIL, or WHILE?
A Review of DATA Step and macro algorithms
http://www2.sas.com/proceedings/forum2007/067-2007.pdf

Ron Fehd the macro maven CDC Atlanta GA USA RJF2 at cdc dot gov
toby dunn
2007-07-26 18:14:11 UTC
Permalink
Ahh sorry I guess I didnt read far enough, thats my fault and I apologies:

For that you need :

%macro test2;

%do i = 1 %to 5;
%if ( &i ne 3 ) %then %put i = &i ;
%end;

%mend test2 ;

%test2




Toby Dunn

If anything simply cannot go wrong, it will anyway. Murphys Law #2.

The buddy system is essential to your survival; it gives the enemy somebody
else to shoot at.
Murphys Law #


Tell a man there are 300 billion stars in the universe and he'll believe
you. Tell him a bench has wet paint on it and he'll have to touch to be
sure. Murphys Law #9






From: "Shiping Wang" <***@gmail.com>
To: "toby dunn" <***@hotmail.com>
Subject: Re: skip a loop to next in a sas macro
Date: Thu, 26 Jul 2007 13:07:28 -0500

Hi toby, thanks for your response. I tried your code it print out:
i = 1
i = 2
seems %return is completely out of loop. What I want is skip i=3 but
continue with next loop. So it can print:
i = 1
i = 2
i = 4
i = 5

Shiping
Post by toby dunn
Shipng ,
%macro test2;
%do i = 1 %to 5;
%if i = 3 %then %return ;
%put i = &i.;
%end;
%mend test2 ;
%test2
Toby Dunn
If anything simply cannot go wrong, it will anyway. Murphys Law #2.
The buddy system is essential to your survival; it gives the enemy
somebody
else to shoot at.
Murphys Law #
Tell a man there are 300 billion stars in the universe and he'll believe
you. Tell him a bench has wet paint on it and he'll have to touch to be
sure. Murphys Law #9
Subject: skip a loop to next in a sas macro
Date: Thu, 26 Jul 2007 16:59:53 -0000
%macro test2;
%do i = 1 %to 5;
%if i = 3 %then %goto continue;
%put i = &i.;
%end;
run;
%mend;
%test2
i = 1
i = 2
i = 3
i = 4
i = 5
i = 1
i = 2
i = 4
i = 5
What do I miss to get expect result?
Thanks a lot,
shiping
_________________________________________________________________
Need a brain boost? Recharge with a stimulating game. Play now!
http://club.live.com/home.aspx?icid=club_hotmailtextlink1
_________________________________________________________________
http://liveearth.msn.com

Continue reading on narkive:
Search results for 'skip a loop to next in a sas macro' (Questions and Answers)
3
replies
programing excel with visual basic?
started 2011-07-07 12:32:41 UTC
programming & design
Loading...