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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
jcromwell77
Fluorite | Level 6
You're correct, and I fat-fingered the include statement. I meant %INCLUDE not $INCLUDE, and it is correct in my code, just not in my question above.
Tom
Super User Tom
Super User

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.

 

jcromwell77
Fluorite | Level 6
Thank you! I did not know about filename, and I used the syntax you gave me and was able to execute the code properly.
ChrisHemedinger
Community Manager

+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.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
Reeza
Super User
Just as an FYI you can also wildcard your %includes using an *

%include "&path/Program0*.sas" / source2;
jcromwell77
Fluorite | Level 6
Thank you, this worked as well. I wish I could add this as a solution too.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 7708 views
  • 8 likes
  • 6 in conversation