BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
narnia649
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

8 REPLIES 8
ballardw
Super User

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.

narnia649
Obsidian | Level 7

Thanks, I added some clarification to the question above. I would not have any metadata at this point in the program.

Tom
Super User Tom
Super User

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.

narnia649
Obsidian | Level 7

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();
Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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

 

SASKiwi
PROC Star

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.   

narnia649
Obsidian | Level 7

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 6027 views
  • 1 like
  • 4 in conversation