I am creating a Main program to execute 5 sub programs, and I want to reuse a file path for better code management.
For example:
LIBNAME home 'C:\Users\user\Documents' ;
OR
%LET path = C:\Users\user\Documents ;
And later I have 5 INCLUDE statements as such:
%INCLUDE 'C:\Users\user\Documents\program01.sas' ;
%INCLUDE 'C:\Users\user\Documents\program02.sas' ;
etc...
Is there a way to use a LIBNAME or variable instead of having to use 'C:\Users\user\Documents' over and over? Such as:
$INCLUDE '&path\program01.sas' ;
etc...
So far I've had no luck with using either a variable or LIBNAME, both give me errors. Ultimately, I want to be able to use the LIBNAME, variable or whatever to update the file path for all sub programs. Currently I have 5 subs and likely to have more in the future, so this would make code management a lot easier.
The macro processor will ignore strings that are quoted on the outside with single quote characters. If you want the macro variable to be expanded then use double quote characters instead.
%LET path = C:\Users\user\Documents ;
%INCLUDE "&path\program01.sas" ;
Libnames are for storing SAS datasets (or other binary SAS files like catalog), not for referencing text files. If you want to make a nickname to point to directory for finding text files then use a FILENAME statement to define a fileref you can then use with your %INCLUDE, INFILE or FILE statements.
filename root "C:\Users\user\Documents" ;
%INCLUDE root("program01.sas");
Editor's note: adding other helpful replies. This one from @Kurt_Bremser :
Try this
%LET path = C:\Users\user\Documents ; %INCLUDE "&path.\program01.sas"; %INCLUDE "&path.\program02.sas";
From @Reeza :
Just as an FYI you can also wildcard your %includes using an *
%include "&path/Program0*.sas" / source2;
And @ChrisHemedinger :
+1 for Tom's tip of using a FILENAME statement to indicate a folder. I use this all of the time as I have some programs that use different paths whether I run them on my local machine (Windows) or on a central server (Linux). Example:
%let tgtpath = %sysfunc(ifc(&SYSSCP. = WIN, \\network\root\u\&sysuserid.\.creds, /u/&sysuserid./.creds) ); filename creds "&tgtpath."; %include creds(signon.sas);
Note that the quotes are not required for the "member name" you're including here.
Such as:
$INCLUDE '&path\program01.sas' ;
You type the INCLUDE command with a % sign not a $ sign. It should also use double quotes instead of single quotes.
The macro processor will ignore strings that are quoted on the outside with single quote characters. If you want the macro variable to be expanded then use double quote characters instead.
%LET path = C:\Users\user\Documents ;
%INCLUDE "&path\program01.sas" ;
Libnames are for storing SAS datasets (or other binary SAS files like catalog), not for referencing text files. If you want to make a nickname to point to directory for finding text files then use a FILENAME statement to define a fileref you can then use with your %INCLUDE, INFILE or FILE statements.
filename root "C:\Users\user\Documents" ;
%INCLUDE root("program01.sas");
Editor's note: adding other helpful replies. This one from @Kurt_Bremser :
Try this
%LET path = C:\Users\user\Documents ; %INCLUDE "&path.\program01.sas"; %INCLUDE "&path.\program02.sas";
From @Reeza :
Just as an FYI you can also wildcard your %includes using an *
%include "&path/Program0*.sas" / source2;
And @ChrisHemedinger :
+1 for Tom's tip of using a FILENAME statement to indicate a folder. I use this all of the time as I have some programs that use different paths whether I run them on my local machine (Windows) or on a central server (Linux). Example:
%let tgtpath = %sysfunc(ifc(&SYSSCP. = WIN, \\network\root\u\&sysuserid.\.creds, /u/&sysuserid./.creds) ); filename creds "&tgtpath."; %include creds(signon.sas);
Note that the quotes are not required for the "member name" you're including here.
+1 for Tom's tip of using a FILENAME statement to indicate a folder. I use this all of the time as I have some programs that use different paths whether I run them on my local machine (Windows) or on a central server (Linux). Example:
%let tgtpath = %sysfunc(ifc(&SYSSCP. = WIN,
\\network\root\u\&sysuserid.\.creds,
/u/&sysuserid./.creds)
);
filename creds "&tgtpath.";
%include creds(signon.sas);
Note that the quotes are not required for the "member name" you're including here.
Try this
%LET path = C:\Users\user\Documents ;
%INCLUDE "&path.\program01.sas";
%INCLUDE "&path.\program02.sas";
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.