I am looking for a way to detect (at runtime) if my SAS code is being executed inside of (as part of / within) a %INCLUDE statement.
Is this possible?
Why not use the SYSINCLUDEFILEDIR and SYSINCLUDEFILENAME Automatic Macro Variables?
Those will contain the directory and file name of the called program when it is submitted in %include.
I guess it depends on whether you want to detect it during execution, or whether you want to detect it after the submitted steps are all completed.
If it is the latter, you can use %include with the source2 option to output to the log, so you can check the log saved in an external file.
Also, what action do you expect to take when it is detected?
Why not use the SYSINCLUDEFILEDIR and SYSINCLUDEFILENAME Automatic Macro Variables?
Those will contain the directory and file name of the called program when it is submitted in %include.
perfect! thankyou!! I didn't see those...
For reference, these are the results I got:
filename _test temp;
data _null_;
file _test;
put '%put &=SYSINCLUDEFILEDIR;';
put '%put &=SYSINCLUDEFILEDEVICE;';
put '%put &=SYSINCLUDEFILEFILEREF;';
put '%put &=SYSINCLUDEFILENAME;';
run;
%inc _test;
%macro x();
%let loc="%sysfunc(pathname(work))/test";
data _null_;
file &loc;
put '%put &=SYSINCLUDEFILEDIR;';
put '%put &=SYSINCLUDEFILEDEVICE;';
put '%put &=SYSINCLUDEFILEFILEREF;';
put '%put &=SYSINCLUDEFILENAME;';
run;
%inc &loc;
%mend;
%x()
%put &=SYSINCLUDEFILEDIR;
%put &=SYSINCLUDEFILEDEVICE;
%put &=SYSINCLUDEFILEFILEREF;
%put &=SYSINCLUDEFILENAME;
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73
74
75 filename _test temp;
76 data _null_;
77 file _test;
78 put '%put &=SYSINCLUDEFILEDIR;';
79 put '%put &=SYSINCLUDEFILEDEVICE;';
80 put '%put &=SYSINCLUDEFILEFILEREF;';
81 put '%put &=SYSINCLUDEFILENAME;';
82 run;
NOTE: The file _TEST is:
Filename=/tmp/SAS_work55E900003774_sas.analytium.co.uk/#LN00187,
Owner Name=allbow,Group Name=allbow,
Access Permission=-rw-r--r--,
Last Modified=02 September 2021 11:46:51
NOTE: 4 records were written to the file _TEST.
The minimum record length was 25.
The maximum record length was 29.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 473.00k
OS Memory 26276.00k
Timestamp 02/09/2021 08:46:51 AM
Step Count 72 Switch Count 0
Page Faults 0
Page Reclaims 62
Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 8
83
84 %inc _test;
SYSINCLUDEFILEDIR=/tmp/SAS_work55E900003774_sas.analytium.co.uk
SYSINCLUDEFILEDEVICE=TEMP
SYSINCLUDEFILEFILEREF=_TEST
SYSINCLUDEFILENAME=#LN00187
89
90 %macro x();
91 %let loc="%sysfunc(pathname(work))/test";
92 data _null_;
93 file &loc;
94 put '%put &=SYSINCLUDEFILEDIR;';
95 put '%put &=SYSINCLUDEFILEDEVICE;';
96 put '%put &=SYSINCLUDEFILEFILEREF;';
97 put '%put &=SYSINCLUDEFILENAME;';
98 run;
99
100 %inc &loc;
101 %mend;
102
103 %x()
NOTE: The file "/tmp/SAS_work55E900003774_sas.analytium.co.uk/SAS_work810D00003774_sas.analytium.co.uk/test" is:
Filename=/tmp/SAS_work55E900003774_sas.analytium.co.uk/SAS_work810D00003774_sas.analytium.co.uk/test,
Owner Name=allbow,Group Name=allbow,
Access Permission=-rw-r--r--,
Last Modified=02 September 2021 11:46:51
NOTE: 4 records were written to the file
"/tmp/SAS_work55E900003774_sas.analytium.co.uk/SAS_work810D00003774_sas.analytium.co.uk/test".
The minimum record length was 25.
The maximum record length was 29.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 469.37k
OS Memory 26276.00k
Timestamp 02/09/2021 08:46:51 AM
Step Count 73 Switch Count 0
Page Faults 0
Page Reclaims 25
Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 8
SYSINCLUDEFILEDIR=/tmp/SAS_work55E900003774_sas.analytium.co.uk/SAS_work810D00003774_sas.analytium.co.uk
SYSINCLUDEFILEDEVICE=DISK
SYSINCLUDEFILEFILEREF=
SYSINCLUDEFILENAME=test
108
109 %put &=SYSINCLUDEFILEDIR;
SYSINCLUDEFILEDIR=
110 %put &=SYSINCLUDEFILEDEVICE;
SYSINCLUDEFILEDEVICE=
111 %put &=SYSINCLUDEFILEFILEREF;
SYSINCLUDEFILEFILEREF=�|��#
112 %put &=SYSINCLUDEFILENAME;
SYSINCLUDEFILENAME=
113
114 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
126
So in the end this did NOT fix my use case - as those macro variables are NOT available inside of macros!
To deal with this scenario, I made a macro wrapper for the %include statement, that inserts these variables as precode - it's documented here: https://core.sasjs.io/mp__include_8sas.html
Oh, I see. I'm sorry.
The SAS Programming Documentation I referred to did not mention that limitation, even though it is the same version.
I should have referred to the original, English version.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.