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

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).

VariableVariance
A3.75
B4.5
C10
D11
E13
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

3 REPLIES 3
ballardw
Super User

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;

Ujjawal
Quartz | Level 8

Thanks a ton. Very Helpful!

Ron_MacroMaven
Lapis Lazuli | Level 10

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1410 views
  • 0 likes
  • 3 in conversation