Currently I have my include statements as below:
%include "M:\Program\Macros\one.sas";
%include "M:\Program\Macros\Macros\two.sas";
%include "M:\Program\Macros\Macros\three.sas";
The program that has these two lines are code is located in "M:\Program."
Is there a way to write these three lines where I do not have to include "M:\Program" and SAS will look in the same folder for example "Macros\one.sas?" I would like this program to run regardless of the Program folder location.
I've tried %include "Macros\one.sas"; but that doesn't seem to work.
Thank you so much!
Just to add, if you only plan on running in Batch then (think its one dot, been a while since I have used it):
%include ".\Macros\one.sas";
%include ".\Macros\Macros\two.sas";
%include ".\Macros\Macros\three.sas";
Then SAS starts with the path of the program you are running and expands the . for you.
I kind of wonder what the problem is with three lines of code?
One approach could be to put those three lines in one program file and then only include that program in the (presumed) main program.
Another approach would be to establish an AUTOCALL macro library and then have the SAS startup add that location.
(Assumes those files are basically macro definitions only).
I apologize for the confusion.
There is another program entitled "main.sas" in folder "M:\Program." Main.sas has the following lines of code:
%include "M:\Program\Macros\one.sas";
%include "M:\Program\Macros\Macros\two.sas";
%include "M:\Program\Macros\Macros\three.sas";
I was wondering if I can make main.sas a bit smart where I do not have to include "M:\Program."
Yes. You can you can run PIPE at your SAS.
Add the following code into Main.sas.
filename x pipe 'dir M:\Program\*.sas /s /b';
data _null_;
infile x length=len;
input x $varying200. len;
call execute( cats('%include " ' , x ,' "; ') );
run;
Xia Keshan
In what environment are you running the program (e.g. Display Manager SAS interactive, batch, Enterprise Guide, etc?) Different environments have different ways of dynamically finding the location of the "current" program.
Sometimes instead of dynamically figuring it out, it's easiest to just create a global macro session variable with the root path for the project.
So if you had (slightly different structure):
M:\Project1\Program\main.sas
M:\Project1\Pogram\one.sas
M:\Project1\Program\Macros\macro1.sas
M:\Project1\Program\Macros\macro2.sas
M:\Project1\Output\
M:\Project1\Data
M:\Project2\Program\main.sas
etc.
In main.sas, you could %let root=M:\Project1. It calls all the other programs as needed. Once you know &root, you know code is in &root\Program, macros are in &root\Program\macros, output goes to &root\Output, etc etc.
Also checkout the macro autocall facility. It takes only a small amount of setup, but has benefits of %including macros. (That said, I %included macros for years....)
--Q.
Thank you for your response.
Ultimately, I would like to run this in a batch in windows.
I would like to share this program with other users. If they were to unpackaged this to their desktop, I don't think this approach would work.
For PC SAS interactive and batch, I use a macro like:
%macro ProgramName (path=0 /*Boolean, include path or not*/ ) ; %local return; %*SYSIN only exists in batch, SAS_EXECFILENAME only exists in interactive; %*When batch submit; %let return = %qsysfunc(getOption(SYSIN)); %if %superq(return) ne %str() %then %do; %if &path ne 1 %then %let return = %qscan(&return,-1,\/); %end; %*When DMS interactive; %if %superq(return)=%str() %then %do; %if &path=1 %then %let return = %qsysfunc(sysget(SAS_EXECFILEPATH)); %else %let return = %qsysfunc(sysget(SAS_EXECFILENAME)); %end; &return %mend;
To return program name with path, call like %put %programname(path=1) ; Something like that could be useful.
Here are a couple of pages with information about your big topic: setting up for running in batch.
http://www.sascommunity.org/wiki/Batch_processing_under_Windows
My own naming conventions for folders differ from yours
I have a SAS-Projects folder
under which are projects, such as Project-A, Project-B, Project-Z
one of the 'project' folders is named SAS-site, which is my tool-box;
and it has a sub-folder named 'macros'
Your Q comes up in every class I have taught about macro usage:
"How can I get SAS to find (i.e. search for) my macros?"
thus the by-guess-and-by-golly solution that you have
%include '...\MyMacroA.sas';
%include '...\MyMacroB.sas';
the key issues are explained on this page
SASautos Companion Reusing Macros - sasCommunity
the option SASAUTOS contains the search path (i.e. library, a set of folders)
for the autocall (auto-search) macro facility.
The default value is the fileref sasautos.
use these statement in your autoexec.sas
to add folders to the autocall search list
filename project '.'; *here: project folder;
filename sitemacr '<???>';*there: tool-box containing macros in files;
options mautosource
sasautos = (Project SiteMacr SASautos);
Now you have the macro autocall facility working for you;
no need to %include your macros.
SAS searches those folders, finds a file with the name of the macro, %includes it,
and the compiled code is saved in catalog work.sasmacro.
... and see the location of the macro with this option
options mautocomploc;
Wow this is quite a bit of great information. Thank you for all your help!
I find it a lot easier just to do something like this:
%let MyProgramFolder = M:\Program;
%include "&MyProgramFolder\Macros\one.sas";
%include "&MyProgramFolder\Macros\Macros\two.sas";
%include "&MyProgramFolder\Macros\Macros\three.sas";
However I think using AUTOCALL macros as already discussed is a better approach.
The SAS AUTOCALL method also permits externalized SASAUTOS environment (concatenation, various OS platform environments considered) change for testing, albeit without making any SAS program code change. The alternative would be to edit the SAS program directly and change the %LET statement that identifies the SAS program source code reference.
Scott Barry
SBBWorks, Inc.
The autocall is for sas-macros not for sas-code.
What you could do is allocating a filename. You can use concatenated locations for that. It a PDS approach sbb will recognize.
Filename mycode "M:\Program\Macros" ;
Than you still can decide to use the fileref "mycode" to be used in a sasautos approach for sas-macro-s or use
%include nycode(main) ;
Assuming mian.sas (default suffix .sas) is your code to be included. When developing working with macro-s also very handy to recompile those.
With a different sufffix enclose the name in quites like "main.sasold"
Thank you for the correction, Jaap -- agreed that SASAUTOS has limited applicability here. I stand corrected.
Scott Barry
SBBWorks, Inc.
Just to add, if you only plan on running in Batch then (think its one dot, been a while since I have used it):
%include ".\Macros\one.sas";
%include ".\Macros\Macros\two.sas";
%include ".\Macros\Macros\three.sas";
Then SAS starts with the path of the program you are running and expands the . for you.
Thank you all for the very helpful input.
This worked; thank you so much RW9.
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 25. Read more here about why you should contribute and what is in it for you!
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.