Hello,
I have a macro call at the start of my program. This macro is stored in my study folder:
C:Project\Study\Test\grabpath.sas
%macro grabpath;
%global gpath;
%let gpth= %sysget(SAS_EXECFILEPATH);
%mend grabpath;
At the start of my program, the macro is called:
%grabpath;
it does not work because the macro is not compiled.
Is there a way to compile the macro when it is called so that with &gpath return the directory path so that the path is not required to be entered manually?
so thta is I wnat to call the setup.sas file I only write
%include "&gpath/setup.sas" instead of %include "C:Project\Study\Test\setup.sas"
I have directories like:
C:\Projects\Project1\Code
C:\Projects\Project1\Code\Macros
C:\Projects\Project2\Code
C:\Projects\Project2\Code\Macros
...
C:\Projects\Common\Code
C:\Projects\Common\Code\Macros
I put my shared autocall macros in to C:\Projects\Common\Code\Macros. That's where I would put grabpath.sas, because it's used across projects.
On windows, I edit my config file to point to the folder. Even though the config file says don't edit below this line. Because I'm a rebel.
So my config file has a line like:
-SET SASAUTOS (
"C:\Projects\Common\Code\Macros"
"!SASROOT\core\sasmacro"
"!SASROOT\graph\sasmacro"
"!SASROOT\iml\sasmacro"
"!SASROOT\qc\sasmacro"
"!SASROOT\stat\sasmacro"
)
That way every SAS session I start with this config file has my utility autocall library available.
Then in a program where the code is stored in a projectN \Code directory, you could call your grabpath macro, and use it to %include your setup code:
%grabpath;
%include "&gpath/setup.sas" ;
And assuming setup.sas is just setup-code, it would include a line like below to add the project-specific macro library to the search path:
*add project-specific macro library to front of search path;
options insert=sasautos=("&gpath\Macros") ;
You could add this statement to the beginning of your program:
options sasautos = ('C:Project\Study\Test' sasautos);
That would make all macros from that folder (including %grabpath) available to the current program.
As you have already done, you would need to name the entries properly. So grabpath.sas would need to hold the definition of %grabpath.
Yes, that's true. SAS doesn't have a function that says, "I moved my macros somewhere else, so find out where I moved them to." However, you could just identify a bunch of folders in all your programs if that would work for you:
options sasautos=('C:\Project\Study\Test' 'C:\Project\Study\TestA' 'C:\Project\Study\TestB' sasautos);
One alternative is to store general utility macros like this, which will be used across projects, outside of a project-specific directory. And your code or config or autoexec or start icon or whatever you want can add that fixed location to the autocall search path. I typically have an autocall search path that starts with a project-specific autocall directory, then the shared utility autocall directory, then SAS-provided locations.
I have directories like:
C:\Projects\Project1\Code
C:\Projects\Project1\Code\Macros
C:\Projects\Project2\Code
C:\Projects\Project2\Code\Macros
...
C:\Projects\Common\Code
C:\Projects\Common\Code\Macros
I put my shared autocall macros in to C:\Projects\Common\Code\Macros. That's where I would put grabpath.sas, because it's used across projects.
On windows, I edit my config file to point to the folder. Even though the config file says don't edit below this line. Because I'm a rebel.
So my config file has a line like:
-SET SASAUTOS (
"C:\Projects\Common\Code\Macros"
"!SASROOT\core\sasmacro"
"!SASROOT\graph\sasmacro"
"!SASROOT\iml\sasmacro"
"!SASROOT\qc\sasmacro"
"!SASROOT\stat\sasmacro"
)
That way every SAS session I start with this config file has my utility autocall library available.
Then in a program where the code is stored in a projectN \Code directory, you could call your grabpath macro, and use it to %include your setup code:
%grabpath;
%include "&gpath/setup.sas" ;
And assuming setup.sas is just setup-code, it would include a line like below to add the project-specific macro library to the search path:
*add project-specific macro library to front of search path;
options insert=sasautos=("&gpath\Macros") ;
thank you this worked.
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.