I want PROC REG to be run if a condition satisfies. For example, I have a table that stores variable names and variance (VIF). If any variable has variance value greater than 5, i want PROC REG to be run on different dataset (input data set), else exit (out of the macro).
Variable | Variance |
A | 3.75 |
B | 4.5 |
C | 10 |
D | 11 |
E | 13 |
Since you mention exit a macro then we assume all of this is within a defined macro.
First get a flag value that says you have a variance in the range you are concerned with:
<previous macro code>
proc sql noprint;
select (max(variance)>5) into : varflag
from have;
quit;
/*The macro variable varflag will be 1 if true. OR you could select the maximum value but you'll need to play some games with %sysevalf in comparisons and this is a tad cleaner;*/
%if &varflag =1 %then %goto leave; /* NOTE there is no % for leave here but is used for the line label to skip to*/
<the code to execute when you aren't going to leave the macro>;
%leave: <any code you might want to execute before leaving the macro could go here such as
cleaning up data sets>
%mend;
Since you mention exit a macro then we assume all of this is within a defined macro.
First get a flag value that says you have a variance in the range you are concerned with:
<previous macro code>
proc sql noprint;
select (max(variance)>5) into : varflag
from have;
quit;
/*The macro variable varflag will be 1 if true. OR you could select the maximum value but you'll need to play some games with %sysevalf in comparisons and this is a tad cleaner;*/
%if &varflag =1 %then %goto leave; /* NOTE there is no % for leave here but is used for the line label to skip to*/
<the code to execute when you aren't going to leave the macro>;
%leave: <any code you might want to execute before leaving the macro could go here such as
cleaning up data sets>
%mend;
Thanks a ton. Very Helpful!
see also:
Using Functions SYSFUNC and IFC to conditionally execute statements in open code - sasCommunity
example:
%let fmt_catalog = library.formats;
%sysfunc(ifc(%sysfunc(cexist(&fmt_catalog))
,%nrstr(%put exist catalog(&fmt_catalog);)
,%nrstr(%put not exist catalog(&fmt_catalog);),))
yes, you have to be good at counting parentheses!
Ron Fehd there is an easier way
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.