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
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;
Thanks Murray
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?
You can issue the ENDSAS statement conditionally in a macro, if you need to issue a RC, take a look at ABORT ABEND.
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
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.