DATA Step, Macro, Functions and more

Use relative path to run SAS programs

Reply
Contributor
Posts: 70

Use relative path to run SAS programs

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.

 


%include "..\..\path.sas";

Super User
Posts: 11,343

Re: Use relative path to run SAS programs

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.

 

%include "&basepath.program1.sas";

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.

Respected Advisor
Posts: 4,932

Re: Use relative path to run SAS programs

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.

PG
Contributor
Posts: 70

Re: Use relative path to run SAS programs

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.

Super User
Super User
Posts: 7,076

Re: Use relative path to run SAS programs

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');
Contributor
Posts: 70

Re: Use relative path to run SAS programs

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 ;
%include "&rundir/../../path.sas";

Super User
Super User
Posts: 7,076

Re: Use relative path to run SAS programs

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

%part1;

Instead of 

%include '../../part1.sas';
Ask a Question
Discussion stats
  • 6 replies
  • 1305 views
  • 2 likes
  • 4 in conversation