BookmarkSubscribeRSS Feed
HashimBasheer
Fluorite | Level 6

I have a sas code which check for the value(true/false). If true continue running the code and if fails exit the sas code without running the steps after.

I would like to create a macro so that i can include this as %include in all the codes.

macro:

check if the value is true/false

if true continue the code

fail exit the program

5 REPLIES 5
Murray_Court
Quartz | Level 8

I recommend the %goto function to skip the rest of the statements in the event of certain logic.

This program is straight out of the sas help library: SAS(R) 9.2 Macro Language: Reference

%macro check;

  %local status;

  %if /***Insert logic here ***/ %then %do;

  %put NOTE: /* your warning message */;

  %goto exit;

  %end;

   more macro statements that test for error conditions

  %put Check completed successfully.;

%exit:

%mend check;

HashimBasheer
Fluorite | Level 6

Thanks Murray Smiley Happy

I have this option. But the issue is I have data steps after the macro. I dont want to run those data steps if the condition fail. Is there a way I can exit them as well?

LinusH
Tourmaline | Level 20

You can issue the ENDSAS statement conditionally in a macro, if you need to issue a RC, take a look at ABORT ABEND.

Data never sleeps
Murray_Court
Quartz | Level 8

Hi Linus,


You need to put everything that you want to execute conditionally inside the macro.


/*compile macro */

%macro conditional;

  %if /***Insert logic here ***/ %then %do;

  %put NOTE: /* your warning message */;

  %goto exit;

  %end;

  %put NOTE: Logic determines that data steps will run normally.;


   put your further data steps here


%exit:

%mend conditional;

/*execute macro*/

%conditional

Although probably a better idea would be to put your data stepc etc. in a macro and then execute the macro conditionally.


%macro conditional2;


/* data steps and everything that you want to execute conditionally */


%mend conditional2;

%macro execute;

%if 1=0 %then %do;
%conditonal2      /*this calls our previously compiled (but until not not exsecuted) macro */
%end;
%else %put NOTE: Logic indicates that data steps will not be executed;


%mend execute;

%execute

HashimBasheer
Fluorite | Level 6

I feel like we have 2 options.

1. Bring all the steps into the macro and then use goto/getout to exit the code.

%macro validation;

%if condition %then %do;

%goto getout;

%end;

% else %do;

steps1l

steps2;

%end;

2. Two macros. one for checking the conditions and then triggering the datasteps which can be saved in another macro ;

%macro validation;

%if condition %then %do;

%goto getout;

%end;

% else %do;

call all the steps as new macro

%end;

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