This tutorial explains how to make SAS stop macro execution on error. It is one of the most common task in building a macro. For example, you are building a macro for SAS users who are beginners. You need to make sure error handling in the macro. If user forgets to specify either a dataset name or variable name, macro should not execute further steps and it should abort immediately.
In the following program, we are telling SAS to stop sas code if user does not specify parameters and notifying them what they have missed. The %abort cancel; statement tells SAS to abort execution immediately.
Sometimes we make typo while entering dataset or variable name. It is important to handle these kinds of errors as well so we need to check for error(s) after each step of SAS Code (Data Step, PROCs). %if &syserr. ne 0 %then %do; works for it.
Instead of using %length to calculate the length of macro parameter, we can use COUNTW function. It is very useful to count the number of variables in the macro parameter.
%if %sysfunc(countw(&inputdata., %str( ))) = 0 %then %do;
%abort cancel;
%end;
1. Stop Macro Processing on Error
In the following program, we are telling SAS to stop sas code if user does not specify parameters and notifying them what they have missed. The %abort cancel; statement tells SAS to abort execution immediately.
Logic - If the length of string of a macro parameter is 0, it means the macro parameter is blank.%macro explore(inputdata= ,var=);options notes;%if %length(&inputdata) = 0 %then %do;%put ERROR: INPUTDATA= must be specified;%put ERROR: The macro ended abnormally.;%abort cancel;%end;%if %length(&var) = 0 %then %do;%put ERROR: VAR= must be specified;%put ERROR: The macro ended abnormally.;%abort cancel;%end;proc sort data = &inputdata.;by &var.;run;%mend;%explore(inputdata = , var = age );
2. Go to End of Program If Error
In the following program, we are telling SAS to go to end of the code if error comes, The %goto statement is used to jump to end of the program.
%macro explore(inputdata= ,var=);3. Check for Error after each step of SAS Code
options notes;
%if %length(&inputdata) = 0 %then %do;
%put ERROR: INPUTDATA= must be specified;
%put ERROR: The macro ended abnormally.;
%goto exit;
%end;
%if %length(&var) = 0 %then %do;
%put ERROR: VAR= must be specified;
%put ERROR: The macro ended abnormally.;
%goto exit;
%end;
proc sort data = &inputdata.;
by &var.;
run;
%exit:
%mend;
%explore(inputdata = , var = age );
Sometimes we make typo while entering dataset or variable name. It is important to handle these kinds of errors as well so we need to check for error(s) after each step of SAS Code (Data Step, PROCs). %if &syserr. ne 0 %then %do; works for it.
%macro explore(inputdata= ,var=);Tip
options notes;
%if %length(&inputdata) = 0 %then %do;
%put ERROR: INPUTDATA= must be specified;
%put ERROR: The macro ended abnormally.;
%abort cancel;
%end;
%if %length(&var) = 0 %then %do;
%put ERROR: VAR= must be specified;
%put ERROR: The macro ended abnormally.;
%abort cancel;
%end;
proc sort data = &inputdata.;
by &var.;
run;
%if &syserr. ne 0 %then %do;
%abort cancel;
%end;
%mend;
%explore(inputdata = sashelp.clss , var = age );
Instead of using %length to calculate the length of macro parameter, we can use COUNTW function. It is very useful to count the number of variables in the macro parameter.
%if %sysfunc(countw(&inputdata., %str( ))) = 0 %then %do;
%abort cancel;
%end;
Share Share Tweet