To stop the sas code if the condition fail

Reply
Contributor
Posts: 30

To stop the sas code if the condition fail

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

Contributor
Posts: 45

Re: To stop the sas code if the condition fail

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;

Contributor
Posts: 30

Re: To stop the sas code if the condition fail

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?

Super User
Posts: 5,256

Re: To stop the sas code if the condition fail

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
Contributor
Posts: 45

Re: To stop the sas code if the condition fail

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

Contributor
Posts: 30

Re: To stop the sas code if the condition fail

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;

Ask a Question
Discussion stats
  • 5 replies
  • 1558 views
  • 0 likes
  • 3 in conversation