BookmarkSubscribeRSS Feed
KBACHU
Fluorite | Level 6

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";

6 REPLIES 6
ballardw
Super User

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\;  /* note that this has to be valid per your operating 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.

PGStats
Opal | Level 21

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
KBACHU
Fluorite | Level 6

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.

Tom
Super User Tom
Super User

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');
KBACHU
Fluorite | Level 6

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";

Tom
Super User Tom
Super User

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';

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 17506 views
  • 5 likes
  • 4 in conversation