- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I would like to obtain the path of the file to be run.
I currently have a python program that submits calls to the command prompt to run several programs including SAS programs.
When I run the current SAS program, I need to be able to grab the file path to run a %includes statement. Currently, it errors out because the %includes uses the working directory and not the path in which the file is located.
In python I know it's the following:
__file__
This returns the path to that specific file.
I saw some code in the forums, but failed to see that this gives the working directory not the file path:
%let rc = %sysfunc(filename(fr,.)); %let curdir = %sysfunc(pathname(&fr));
Is there a way to obtain the path to the file in SAS?
For example, I am running program.sas.
I would like to be able grab the location this file is being run from & use that in the %includes statement.
Specifically, if I have the file located here:
C:\Users\Person\program.sas
It would return the following:
C:\Users\Person
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Are you saying your program is doing something like:
sas myfile.sas
If so then the option SYSIN will have the string myfile.sas and the path to that file is the current working directory.
If you included a path on the command line like this:
sas /home/user1/myfile.sas
Then the path to the file will be part of the value of the SYSIN option.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You might be skipping a bit of your description. File path to specifically what file???
Your example with the Filename function is misleading. That function creates a file reference for SAS to use given the path to and the file name.
If you have a Fileref previously created the SAS metadata such as stored in the SASHELP.VEXTFL has the Fileref and the full path/filename of associated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, I added some clarification to the question above. I would not have any metadata at this point in the program.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Are you saying your program is doing something like:
sas myfile.sas
If so then the option SYSIN will have the string myfile.sas and the path to that file is the current working directory.
If you included a path on the command line like this:
sas /home/user1/myfile.sas
Then the path to the file will be part of the value of the SYSIN option.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Tom!
Yes, I was submitting a string to the command prompt with the sysin option.
I was able to find the following article:
Using the SAS Macro Language to Create Portable Programs - SAS Learning Post
I modified the macro to just grab the end.
This will work during testing locally with a batch submit or if it's being launched from a different working directly through the command prompt.
%macro ExecPrg(); %GLOBAL curdir; %if %sysfunc(getoption(sysin)) ne %str() %then %do; /* Batch Execution */ %LET curfile = %sysfunc(getoption(sysin)); %end; %else %do; /* Interactive Execution */ %LET curfile = %sysget(SAS_EXECFILEPATH); %end; %LET count = %sysfunc(countw(&curfile,%str(\), mq)); %LET curdir = ; %DO i=0 %TO %eval(&count-1); %LET part_name = %scan(&curfile, &i, "\"); %IF &i = 0 or &i = 1 %THEN %DO; %LET curdir = &part_name; %END; %ELSE %DO; %LET curdir = &curdir\&part_name; %END; %END; %mend execprg; %ExecPrg();
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The environment variable SAS_EXECFILEPATH is something that is only set if you are using Display Manager under Windows operating system AND you are using the "enhanced" editor instead of the original program editor AND you have actually saved the code before submitting it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
SYSIN should have the fully qualified filename, even if you call it with a relative filename.
%let sysin=%qsysfunc(getoption(sysin));
%let file=%qscan(&sysin,-1,/\);
%let path=%qsubstr(&sysin,1,%length(&filename)-%length(&file));
%put &=sysin;
%put &=file;
%put &=path;
At least that is how it works on Unix in my shop.
If you want to know the current working directory make a fileref and use the PATHNAME() function.
filename here '.';
%let curdir=%qsysfunc(pathname(here));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
First off you need to know the OS your SAS server runs on as the file path will be defined differently based on the OS.
Second, the R reference __file__ is obviously a pointer to a folder and we need to find out where this is. There must be some function in R that will resolve this so this is not a SAS question but an R one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, the OS is windows.
In python, not R, but it may be the same I'm not sure, running the following:
__file__
would return a string of the file location.
So if I wrote a python program located here:
C:\my_folder\Desktop\test.py
And within that "test.py" program wrote:
print(__file__)
I would get:
"C:\my_folder\Desktop\test.py"
I needed a similar function to run within the SAS program.
Sorry for the confusion.