BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Yoko
Obsidian | Level 7

I created a macro with iteration of 3000. The first part looks like this: 

Yoko_0-1638553465078.png

The last part looks like this (there are more codes between these): 

Yoko_2-1638553531920.png

 

I want to end the iteration at the end if hosp_2more_timeframe1 = 1, and want to keep going if it's not 1. 

Does anyone know how to do this? 

 

Thank you, 

 

Yoko

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Quite similar to @Reeza suggestion:

 

Add just after the %macro statement:

%let stop = no;

Next, modify this statement:

if hosp_visit_&i >= 2 then hosp_2more_timeframe1=1;

Instead, use:

if hosp_visit_&i >= 2 then do;
   hosp_2more_timeframe1=1;
   call symputx('stop', 'yes');
end;

Finally, just before the %END statement, add a statement:

%if &stop = yes %then %let i=4000;

 

View solution in original post

5 REPLIES 5
Reeza
Super User

Some options:

Astounding
PROC Star

Quite similar to @Reeza suggestion:

 

Add just after the %macro statement:

%let stop = no;

Next, modify this statement:

if hosp_visit_&i >= 2 then hosp_2more_timeframe1=1;

Instead, use:

if hosp_visit_&i >= 2 then do;
   hosp_2more_timeframe1=1;
   call symputx('stop', 'yes');
end;

Finally, just before the %END statement, add a statement:

%if &stop = yes %then %let i=4000;

 

Yoko
Obsidian | Level 7

I tried your method and it worked (I had to add % to 'then' though.). Thank you. Can I ask you one more thing? 

I have a section above hosp_2more_timeframe1.  

I want the program iterate until it finds ed_2more_timeframe1.  

Once ed_2more_timeframe1 is found, it should go to the next section to find hosp_2more_timeframe1. 

I do not think the first %if &stop = yes %then %let i = 3000 works.  

Do you have any suggestions? 

 

Yoko_0-1638558947633.png

 

Tom
Super User Tom
Super User

Change that first %IF to a %IF /%THEN/%DO/%END block around the part the code you want to execute conditionally.

 

Note that posting photographs of text is silly. It makes it hard to provide a detailed response.

Tom
Super User Tom
Super User

A simple way to exit is to use the %GOTO statement.

%macro test(limit);
%local index;
%do index=1 %to 10;
  %put &=index;
  %if &index >= &limit %then %goto quit;
%end;
%put Limit not reached.;
%quit:
%mend test;
%test(limit=5);
%test(limit=15);

It the condition you want to test is based on data then you can use SAS code to generate a macro variable that the macro code can then test.

%do i=1 %to 3000;
...
%let stop=0;
data _null_;
  set ;
  if hosp_2more_timeframe1=1 then call symputx('stop','1');
run;
%if &stop=1 %then %goto quit;
%end;
%quit:
...

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 825 views
  • 0 likes
  • 4 in conversation