Search code examples
sassas-macro

SAS: how to know which Macro called current Macro?


Imagine that a certain macro M1 was called by a different macro and is being executed. Is there a way to get access to the name of the macro that called M1 in that specific instance without the calling macro having been explicitly programmed to pass on that information?

I have looked into the List of SAS Automatic Macrovariables and haven't found what I wanted.


Solution

  • Use the new %sysmexecdepth and %sysmexecname() functions.

    Example:

    %macro mymac;
      %put My name is : &sysmacroname;
      %put My depth is : %sysmexecdepth;
      %put My name is : %sysmexecname(%sysmexecdepth);
      %put My parent is named : %sysmexecname(%sysmexecdepth-1);
    %mend mymac;
    %mymac;
    

    Note: Be careful if you are trying to use those functions in output strings. They have a nasty habit of "eating" the spaces in front of them. Notice the difference between the first line and the other lines the macro generates. The first one preserves the space after the colon and the others do not.

    My name is : MYMAC
    My depth is :1
    My name is :MYMAC
    My parent is named :OPEN CODE