SAS Macro Features from 9.3 Still Worth Using: %SYSMACEXIST and More

By Mohan Peddineni · · 1 min read
SAS 9.3 added several small macro tools that are still useful in SAS 9.4 and Viya: %SYSMACEXIST checks whether a macro is compiled in the session, %SYSMACDELETE removes it, %SYSMEXECDEPTH and %SYSMEXECNAME tell a macro who called it, the SYSNOBS macro variable holds the number of observations the last step read, and the MAUTOCOMPLOC option shows which file an autocall macro was compiled from.

These features are not new any more, but many clinical macro libraries were written before them and still use older workarounds such as PROC CATALOG to delete a compiled macro. This post shows where each one helps in day-to-day work on a study macro library, with code you can paste and the log lines to expect.

Check your version first

%put &=sysvlong;

The log shows a string such as SYSVLONG=9.04.01M7P.... Anything starting with 9.03 or later, or a Viya release, supports everything below.

%SYSMACEXIST and %SYSMACDELETE: refreshing an autocall macro

Here is a common situation. Your study uses an autocall library, and you are fixing the macro %derive_teae. You edit the file, run the program again, and nothing changes. The reason: the first call compiled the macro into the WORK macro catalog, and SAS reuses that compiled copy for the rest of the session. It does not look at the autocall file again.

Before 9.3 the usual fix was to restart SAS, or to delete the catalog entry with PROC CATALOG. Now there is a statement for it:

%macro refresh(name);
  %if %sysmacexist(&name) %then %do;
    %sysmacdelete &name;
    %put NOTE: Compiled macro &name removed. The next call recompiles it.;
  %end;
  %else %put NOTE: &name is not compiled in this session.;
%mend refresh;

%derive_teae(inds=adae)      /* first call compiles it */
%refresh(derive_teae)
%derive_teae(inds=adae)      /* recompiled from the edited file */

Expected log from %refresh:

NOTE: Compiled macro derive_teae removed. The next call recompiles it.

Two details:

  • %SYSMACEXIST returns 1 only if the macro is compiled in the WORK catalog. An autocall macro that has not been called yet returns 0, even though calling it would work. It answers "is it compiled?", not "can I call it?".
  • You cannot delete a macro while it is running, so call %refresh from open code or from a different macro.

%SYSMEXECDEPTH and %SYSMEXECNAME: who called me?

In a library where macros call macros, log messages are much easier to follow when they say which macro wrote them. %SYSMEXECDEPTH returns the current nesting level (0 in open code, 1 inside a macro called from open code). %SYSMEXECNAME(n) returns the name of the macro running at level n.

A small logging helper uses both:

%macro logmsg(msg);
  %put NOTE: [%sysmexecname(%eval(%sysmexecdepth - 1))] &msg;
%mend logmsg;

%macro derive_aoccfl;
  %logmsg(Sorting ADAE by subject and start date);
%mend derive_aoccfl;

%macro build_adae;
  %logmsg(Starting ADAE build);
  %derive_aoccfl
%mend build_adae;

%build_adae
%logmsg(Called from open code)

Inside %logmsg, the depth is one more than the caller's, so %sysmexecdepth - 1 points at the caller. Expected log:

NOTE: [BUILD_ADAE] Starting ADAE build
NOTE: [DERIVE_AOCCFL] Sorting ADAE by subject and start date
NOTE: [OPEN CODE] Called from open code

At level 0, %SYSMEXECNAME returns OPEN CODE. The names come back in upper case. This is lighter than turning on MPRINT and MLOGIC for the whole program when you only want to know where a message came from.

SYSNOBS: how many observations did the last step read?

SYSNOBS is an automatic macro variable holding the number of observations read from the last data set closed by the previous step. It saves a separate count step for simple checks:

data adsl;
  input usubjid :$10. saffl :$1.;
  datalines;
CSP-001 Y
CSP-002 Y
CSP-003 N
;
run;

data _null_;
  set adsl;
run;
%put &=sysnobs;
SYSNOBS=3

Keep it simple. In a step that reads one data set and writes another, or reads several, "the last data set closed" may not be the one you have in mind. Test the value in your own environment before you build a check on it, and when the count matters for a table, compute it explicitly.

MAUTOCOMPLOC: which file was compiled?

Study programs often have two or three autocall folders in SASAUTOS: a study folder, a standard library and SAS's own macros. When two folders contain a file with the same name, the first one found wins, and that is not always the one you expect.

options mautocomploc;
%derive_teae(inds=adae)

With MAUTOCOMPLOC on, the log shows the path of the autocall source file when the macro is compiled. If it points at the standard library when you expected the study copy, check the order of folders in your SASAUTOS option. A related option, MAUTOLOCDISPLAY, writes the source location every time an autocall macro is called rather than only when it is compiled.

Both options are cheap to leave on in development. They add a line or two per macro to the log, which most teams accept in exchange for knowing exactly which code ran.

Common mistakes

  • Reading %SYSMACEXIST = 0 as "macro not available". It only checks compiled macros, not the autocall path.
  • Editing an autocall file and rerunning without recompiling. The old compiled copy keeps running until you delete it or restart.
  • Calling %SYSMACDELETE on the macro that is running. It cannot delete itself.
  • Trusting SYSNOBS after complex steps without testing which data set it refers to.
  • Duplicate macro names across autocall folders without MAUTOCOMPLOC to show which one was used.
Try it yourself: predict the log

What does this program write to the log?

%macro inner;
  %put depth=%sysmexecdepth name=%sysmexecname(%sysmexecdepth);
%mend inner;

%macro outer;
  %put depth=%sysmexecdepth name=%sysmexecname(%sysmexecdepth);
  %inner
%mend outer;

%outer
%put depth=%sysmexecdepth name=%sysmexecname(%sysmexecdepth);

Answer:

depth=1 name=OUTER
depth=2 name=INNER
depth=0 name=OPEN CODE

Each macro reports its own level and name. In open code the depth is 0 and the name is OPEN CODE.

Frequently asked questions

How do I check if a macro exists in SAS?

%sysmacexist(name) returns 1 if the macro is compiled in the current session's WORK catalog. It returns 0 for autocall macros that have not been called yet.

How do I delete a compiled macro in SAS?

Use %sysmacdelete name;. It removes the compiled definition, so the next call recompiles from the autocall file.

How do I get the name of the current macro?

%sysmexecname(%sysmexecdepth) returns the name of the macro that is running. Use %sysmexecdepth - 1 inside %eval to get the caller.

How do I see which autocall file was used?

Turn on options mautocomploc;. The log then shows the source file path when each autocall macro is compiled.

Key takeaways
  • Use %SYSMACEXIST with %SYSMACDELETE to force an edited autocall macro to recompile.
  • %SYSMEXECNAME(%SYSMEXECDEPTH) gives you the current macro name for clearer log messages.
  • SYSNOBS is handy for quick counts, but test it before relying on it.
  • MAUTOCOMPLOC shows which autocall file was compiled when folders contain duplicate names.

Related: Debugging nested SAS macros · Seeing %INCLUDE code in the SAS log · More SAS articles

SAS
MP
Written by Mohan Peddineni

Manager, Statistical Programming. Writes about SAS, R, CDISC and regulatory submissions. More about me → · LinkedIn

↑