07-07-2016 01:12 PM
Is there a way I can use relative path in SAS program? Something like, if I have a SAS code in test > test1> test2 directory calles test.sas and the content of test.sas is as below. I want to execute the program path.sas which is in test folder.
07-07-2016 01:31 PM
Can you? Yes. But considering the the default run location is a bit variable as a temporary folder that gets deleted when SAS terminates then it is a poor idea.
If you do not want to retype a long path, or make it easy to change you could use a macro variable in an autoexec.sas to set the long part and then use as needed.
%let basepath= c:\data\folder1\test1\test2\test3\; /* not this has to be valid per your operation system.
would bring the code from program1.sas file into the current program from the folder indicated by the BASEPATH macro variable.
To change location just modify the %let statement for Basepath.
07-07-2016 02:37 PM
Relative file paths in SAS are relative to the 'current directory'. If you want a path that is relative to the path of the current program, you have to build it yourself.
To get the path of the current program, use SYSGET("SAS_EXECFILEPATH") in a data step or %qsysfunc(sysget(SAS_EXECFILEPATH)) in macro code.
07-10-2016 08:04 PM
Thanks for your respone. Can you please give me an example? The relative path doesnt work from EG. We have a procedure in which we move the SAS code to different enviornments. Using relative path will make our life easier as we dont have to modify the program path everytime we migrate the code. I even tried using X in front of the %include statement, but for some reasons SAS coesnt recognize the file.
07-10-2016 08:26 PM
You can easily use relative paths if you run SAS from the command line. For example in Unix environment. But if you are launching SAS in other ways then you will need to have some way to tell SAS what directory you want to use for your base path.
If you store the path into a macro variable then you can use that to generate the file names.
%let rundir = /root/test/test1/test2 ; %include "&rundir/../../path.sas";
You can even use SAS filerefs instead of macro variables.
filename rundir '/root/test/test1/test2' ; %include rundir('../../path.sas');
07-10-2016 08:31 PM
Thanks Tom for your reply. I would like to use this feature from SAS EG. I will not be able to use the below variable as the top level directory will change when I move the code from one environemnt to an other. I am looking for some macro which autodetects the current folder and able to execute the code which is two levels below the current directory using the include statment. I was able to acheive this by running SAS on compute server directly by using %include '..\..\..', but this feature doesnt work from EG.
%let rundir = /root/test/test1/test2 ;
07-10-2016 09:03 PM
I am not sure if Enterprise Guide sends anything to the SAS server to let it know what piece of SAS code it is currently running. Someone with more EG experience might be able to comment on that.
You might want to look into using a code management or version control tools that will let you automatically embed the path INTO the file when you move it. For example if you use RCS then you can use the $SOURCE keyword to have RCS store the location of the file into the file when new versions are checked into the version control system.
You could also change how you are calling the program. In stead of pointing and clicking at a file just submit a small program the calls the program you want.
%let path=...... ; %include "&path/test.sas";
Then the program "test.sas" that is called can assume that the macro variable PATH has already been set and use that to find the other programs.
Another way is to change how your code is organized so that you can use AUTOCALL macros instead of %INCLUDING files with code in them. Then you can just adjust the path used in the SASAUTOS option and SAS will find and automatically include the macro definition when you call your system macros. Then your programsn look more like