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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 875 views
  • 0 likes
  • 3 in conversation