3

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.

Marco
  • 313
  • 1
  • 2
  • 11

1 Answers1

5

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
Tom
  • 47,574
  • 2
  • 16
  • 29
  • Works like a charm! Also thanks for the heads up concerning "eaten" blanks. This seems to be enough to solve that problem: %let depth = %sysmexecdepth; %put My depth is : &depth; – Marco Mar 29 '20 at 05:09
  • 1
    @Tom have you reported the eating disorder to SAS support ? It's something they should fix rather than let fester. – Richard Mar 29 '20 at 12:01
  • I reported it. Not sure if they are going to fix. – Tom Mar 29 '20 at 13:21