This tip covers the recurring issue of cleanly canceling SAS processes when conditions are met. SAS provides the ability to stop a single datastep via the stop or abort statement, or to cancel the whole sas session by generating errors if the errorabend
option is set. There is no middle ground to automatically and gently stop the currently running process, or to cancel some of the subsequent statements when conditions are met. In interactive mode, one can also try his or her chance by clicking the (!) icon, but this is not totally reliable.
Here is a macro that will cancel whatever statements one wishes to cancel when a condition is met. A similar result could be achieved by enclosing the whole program in a huge macro, and running %if tests. However, this would have 2 drawbacks:
Typical dependency test
%if not test1 %then %do; code 1 %if not test1 %then %do; code2 %if not test1 %then %do; code3 %end; %end; %end;-- more code -- |
Using this tip%NOEXECTEST (test1); code1 %NOEXECTEST (test2); code2 %NOEXECTEST (test3); code3 &noexecmacroend %mend;-- more code -- |
This tip works around these drawbacks by dynamically enclosing the statements to be discarded in a dummy macro.
The %NOEXECTEST macro, when called and if conditions are met, will start another macro and uncomment the %mend statement for that macro. Since this dummy macro will not be called, all the statements inside it will not be run.
%macro noexectest(condition=, /* condition assessed */ msg=); /* message to display in log if condition met */ %global noexecmacroend; %if %unquote(&condition) %then %do; %* check user condition, if true; %let noexecmacroend=; %* uncomment mend statement; %put %sysfunc(repeat(*,50)); %* display message; %put %upcase(&msg); %put %sysfunc(repeat(*,50)); %unquote(%nrstr(%macro noexecdummy;)); %* and start dummy macro; %end; %* [This dummy macro includes all statements until mend]; %* [These statements will not be run]; %else %let noexecmacroend=* ; %* else keep mend stmt commented; %mend; |
Example of Usage
*start of cancelable block; %noexectest( condition= %nrstr(%sysfunc(weekday(%sysfunc(today()))) = 1) , msg= No runs on Sundays. Processing cancelled.); data not_on_sunday; x=1; /* comments can be cancelled too */ title '1'; proc print; run; *subsequent test if first one passed; %noexectest( condition=%str(&syserr ne 0), msg= Could not print data. Processing aborted.); %macro print2; title '2'; proc print; run; %mend; /* macros can be cancelled as well */ %print2; *end of cancelable block (i.e. end of program most of the time); &noexecmacroend %mend; |
If you add the %NOEXECTEST macro into your program editor, color coding disappears in the SAS editor due to the
%unquote(%nrstr(%macro noexecdummy;));statement. SAS sees the %macro string and stops color-coding afterwards.
Improved color-coding logic from SAS is required here. A work-around is to fool the color parser a second time and add
%let a=%nrstr(%mend);anywhere after the "offending" above statement, for example just before the %end; statement.
Better yet, you can add this macro in the sasauto path and not in the program using it.
About the Author
Christian Graffeuille is a programmer and consultant with over 13 years of SAS software experience. He has worked for SAS Institute in France for 10 years, and is now living in New Zealand. His SAS expertise includes data warehouse architecture, application design and development, training, and programming using base SAS, SQL, SAS/FSP, SAS/AF, SAS/GRAPH and SAS/IntrNet software. He is also practised in web design and web development.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Macro Non SAS Authors ==> Christian Graffeuille |
Date Modified: | 2005-12-13 03:03:18 |
Date Created: | 2004-10-11 15:15:32 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | All | n/a | n/a |