I created a macro with iteration of 3000. The first part looks like this:
The last part looks like this (there are more codes between these):
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
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;
Some options:
@Yoko wrote:
I created a macro with iteration of 3000. The first part looks like this:
The last part looks like this (there are more codes between these):
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
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;
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?
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.
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:
...
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.