Return to Archive

SAS Tip of the Month
October 2004

Want to find out if a macro variable exists or not in your program? The following macro will let you know:

    %macro mpresent (_var=,        /*Macro variable to search for*/
                     _xists=_xists /*Macro variable returning the result - default _XISTS*/
        %global &_Xists;
        %let xdsid=%sysfunc(open(sashelp.vmacro (where=(name="%upcase(&_var)"))));
        %let &_Xists=%SYSFUNC(ATTRN(&xdsid,any));
        %let xrc=%sysfunc(close(&xdsid));
    %mend mpresent;

The result of the find will be in the macro variable defined in the _XISTS parameter, 1 if it is present, any other value if it is not found. An example of the call is given in the following code fragment:

    %if %upcase(&dsetname)=DEMO %then %do;
        data demoage;
            set demo;
            %if (&_xsort=1) %then by &sortlist;

In the example the dataset DEMOAGE is created from the dataset DEMO by variables defined in the macro variable SORTLIST but only if the macro variable SORTLIST exists.

This macro is one of my old favorites. Enjoy.

Updated October 6, 2004