Hi all,
I am looking for a way to dynamically retrieve the name/path of the SAS programs that are being executed (i.e. the information would remain correct if a program would be moved to a different folder and/or would be renamed), but with the specificity that the different programs are called in a sequence making use of %INCLUDE statements.
I have found many macros doing that perfectly for the main program (i.e. the one in which the %INCLUDE statements are coded), but it does not work for the programs which are included (returned path and program names remain the names of the main program).
I guess this is due to the fact that the %include statement only append the code of a SAS program into the program making the calling?
I am working on SAS 9.4 on Windows 10.
Would there be anyone who already had this need and found a solution? Here below, a representation of what I would like to get:
Main program: C:\Users\&sysuserid.\Desktop\Program1.sas
path value = C:\Users\&sysuserid.\Desktop
program name = Program1.sas
First include statement: %include "C:\Users\&sysuserid.\Desktop\Program2.sas";
path value = C:\Users\&sysuserid.\Desktop
program name = Program2.sas
Second include statement: %include "D:\Program3.sas";
path value = D:\
program name = Program3.sas
Many thanks in advance for your help.
Kr,
Florent
SAS now sets that information into macro variables.
%let path=%sysfunc(pathname(work));
data _null_;
file "&path/test.sas" ;
put '%put &=SYSINCLUDEFILEDEVICE;'
/ '%put &=SYSINCLUDEFILEDIR;'
/ '%put &=SYSINCLUDEFILEFILEREF;'
/ '%put &=SYSINCLUDEFILENAME;'
;
run;
%include "&path/test.sas" ;
filename test "&path/test.sas";
%include test ;
Results
390 %include "&path/test.sas" ; SYSINCLUDEFILEDEVICE=DISK SYSINCLUDEFILEDIR=C:\... SYSINCLUDEFILEFILEREF= SYSINCLUDEFILENAME=test.sas 395 396 filename test "&path/test.sas"; 397 %include test ; SYSINCLUDEFILEDEVICE=DISK SYSINCLUDEFILEDIR=C:\... SYSINCLUDEFILEFILEREF=TEST SYSINCLUDEFILENAME=test.sas
It might help to include examples of the macros or code you have attempted that don't work so you don't get those or similar suggestions.
What to want for output to this? An entry in a the LOG, a macro variable, write to a data set? Or is this supposed to parse a code file?
I would tend to take any of my %include statements and add the appropriate code in the main program. Such as if I wanted something in the LOG:
%put "Calling Program: Program2.SAS in C:\Users\&sysuserid.\Desktop"; %include "C:\Users\&sysuserid.\Desktop\Program2.sas";
Hi ballardw,
An entry in the LOG was indeed the idea.
Unfortunately, I wanted to avoid typing that kind of hard-coded %put statements in the programs because that would remain static (I am looking for a dynamic way to trace programs names and locations (in case a program would be renamed and/or moved to a different location).
The reply from Tom gives exactly what I was looking for. I was just not aware that the referred automatic macro-variables were introduced in SAS 9.4 but they are pretty much handy!
Kind regards,
Florent
If you just want to see the filename in the LOG you don't have to do anything. SAS already writes a NOTE with that information.
746 %include code /source2 ; NOTE: %INCLUDE (level 1) file CODE is file C:\...\AppData\Local\Temp\1\SAS Temporary Files\_TD8300_..._\#LN00084. 747 +* Temporary file; NOTE: %INCLUDE (level 1) ending.
SAS now sets that information into macro variables.
%let path=%sysfunc(pathname(work));
data _null_;
file "&path/test.sas" ;
put '%put &=SYSINCLUDEFILEDEVICE;'
/ '%put &=SYSINCLUDEFILEDIR;'
/ '%put &=SYSINCLUDEFILEFILEREF;'
/ '%put &=SYSINCLUDEFILENAME;'
;
run;
%include "&path/test.sas" ;
filename test "&path/test.sas";
%include test ;
Results
390 %include "&path/test.sas" ; SYSINCLUDEFILEDEVICE=DISK SYSINCLUDEFILEDIR=C:\... SYSINCLUDEFILEFILEREF= SYSINCLUDEFILENAME=test.sas 395 396 filename test "&path/test.sas"; 397 %include test ; SYSINCLUDEFILEDEVICE=DISK SYSINCLUDEFILEDIR=C:\... SYSINCLUDEFILEFILEREF=TEST SYSINCLUDEFILENAME=test.sas
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.