To retrieve the nesting
level of the currently executing macro, use the %SYSMEXECDEPTH. This
function returns a number indicating the depth of the macro in nested
macro calls. The following are the %SYSMEXECDEPTH return value descriptions:
0 |
open code |
>0 |
nesting level |
See the following example and explanations
that follow it.
8 %macro A;
9 %put %sysmexecdepth;
10 %mend A; /* The macro execution depth
of a macro called from open code */
11 %A; /* is one */
1
12
13 %macro B;
14 %put %nrstr(%%)sysmexecdepth=%sysmexecdepth;
15 %put %nrstr(%%)sysmexecname(1)=%sysmexecname(1);
16 %put %nrstr(%%)sysmexecname(2)=%sysmexecname(2);
17 %put %nrstr(%%)sysmexecname(0)=%sysmexecname(0);
18 %put %nrstr(%%)sysmexecname(%nrstr(%%)sysmexecdepth-1)=
%sysmexecname(%sysmexecdepth-1);
19 %mend B;
20
21 %macro C;
22 %B;
23 %mend;
24 %C;
%sysmexecdepth=2
%sysmexecname(1)=C
%sysmexecname(2)=B
%sysmexecname(0)=OPEN CODE
%sysmexecname(%sysmexecdepth-1)=C
25
26 %macro level1;
27 %level2;
28 %mend;
29 %macro level2;
30 %level3;
31 %mend;
32 %macro level3;
33 %level4;
34 %mend;
35 %macro level4;
36 %do i = %sysmexecdepth+1 %to -1 %by -1;
37 %put %nrstr(%%)sysmexecname(&i)=%sysmexecname(&i);
38 %end;
39 %mend;
40
41 %level1;
WARNING: Argument 1 to %SYSMEXECNAME function is out of range.
%sysmexecname(5)=
%sysmexecname(4)=LEVEL4
%sysmexecname(3)=LEVEL3
%sysmexecname(2)=LEVEL2
%sysmexecname(1)=LEVEL1
%sysmexecname(0)=OPEN CODE
WARNING: Argument 1 to %SYSMEXECNAME function is out of range.
%sysmexecname(-1)=
42
-
Macro
A
calls macro
B
. Macro
C
calls macro
B
. A
call to %SYSMEXECDEPTH placed in macro
C
would return the value
2 for macro
B
.
-
If the macro
C
wanted to know the name of the macro that had called it, it could
call %SYSMEXECNAME with
%SYSMEXECNAME(%SYSMEXECDEPTH-1
(the value of the
n argument
being %SYSMEXECDEPTH, its own nesting level, minus one). That call
to %SYSMEXECNAME would return the value B.