There is %SYMEXIST(macro-var-name)
macro function to see if the macro-var exists, but you cannot write %IF
in the open, so you have to enclose your %IF
statement inside some other macro. You may end up writing a macro just to wrap your code in a source file like below. This is not pretty, but you can probably get by with this, if the guard is needed.
%macro wrapper;
%if %symexist(foo_defined) %then %return;
%macro foo;
%global foo_defined;
%let foo_defined = 1;
%put i am foo;
%mend foo;
%mend wrapper;
%*-- tests --*;
options mcompilenote=all;
%symdel foo_defined;
%*-- first time it will define %foo --*;
%wrapper
%foo
/* on log
NOTE: The macro FOO completed compilation without errors.
6 instructions 108 bytes.
i am foo
*/
%*-- second time it will not --*;
%wrapper
%foo
/* on log
(no notes on macro compilation)
i am foo
*/
Upon invocation, SAS makes available a bunch of catalogs, files, and directories for accessing (compiled/not compiled) macros. This makes it cumbersome, but not impossible, to directly find out a macro is already available to this session or not, given the macro's name. Read about the (gory) details in this paper:
http://support.sas.com/resources/papers/proceedings09/076-2009.pdf