DATA Step, Macro, Functions and more

If Then Run PROC

Accepted Solution Solved
Reply
Regular Contributor
Posts: 184
Accepted Solution

If Then Run PROC

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

Accepted Solutions
Solution
‎05-28-2015 04:54 PM
Super User
Posts: 11,343

Re: If Then Run PROC

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


All Replies
Solution
‎05-28-2015 04:54 PM
Super User
Posts: 11,343

Re: If Then Run PROC

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;

Regular Contributor
Posts: 184

Re: If Then Run PROC

Thanks a ton. Very Helpful!

Regular Contributor
Posts: 227

Re: If Then Run PROC

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)Smiley Wink

         ,%nrstr(%put not exist catalog(&fmt_catalog)Smiley Wink,))

yes, you have to be good at counting parentheses!

Ron Fehd  there is an easier way

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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