BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AllanBowe
Barite | Level 11

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?

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

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.

View solution in original post

7 REPLIES 7
japelin
Rhodochrosite | Level 12

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?

AllanBowe
Barite | Level 11
I'm talking about runtime (during execution).

Our error handling macro needs to act differently when called inside a %include, due to differences in the way SAS processes macros in this context.
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
japelin
Rhodochrosite | Level 12

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.

AllanBowe
Barite | Level 11

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        
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
AllanBowe
Barite | Level 11

So in the end this did NOT fix my use case - as those macro variables are NOT available inside of macros!

 

  • SYSINCLUDEFILEDEVICE
  • SYSINCLUDEFILEDIR
  • SYSINCLUDEFILEFILEREF
  • SYSINCLUDEFILENAME

 

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

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
japelin
Rhodochrosite | Level 12

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.

AllanBowe
Barite | Level 11
Your input led to the solution! No need to apologise. It's a strange limitation indeed, or at least, not intuitive. The best thing is, we finally have a way to allow end users to send custom abort messages from the user provided validation programs in our main product - Data Controller for SAS

https://datacontroller.io
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 939 views
  • 6 likes
  • 2 in conversation