BookmarkSubscribeRSS Feed
KBACHU
Fluorite | Level 6

Hi All - I am looking for a global macro variable which should be able to detect my current program path. I would like to add this to my autoexec file and call the macro in the SAS program. Can someone please help me?

14 REPLIES 14
PBsas
Obsidian | Level 7

%global variable_name;

%let variable_name = path;

save this in the autoexec file. You would be able to use the macro variable in the sas code that will resolve to the path.

 

I hope it helps.

KBACHU
Fluorite | Level 6

Thanks for your response, but this way I have to define many macro variables in the autoexec file. I am looking for something which should be able to detect the program path and I want to use this in many other SAS programs. Is there any option in SAS which will be able to detect the current program path?

KBACHU
Fluorite | Level 6

The reason I am looking for this option is because I can use relative path to call other SAS programs.

 

%include '&currentpath\..\..test.sas';

ballardw
Super User

You may be looking for the SAS environment variable SAS_EXECFILEPATH if you are looking for the location of the sas code file.

You would get to that using %sysget(SAS_EXECFILEPATH) this returns the path AND the program file name. It assumes the program file has been saved before the %sysget() is executed.

 

This could be in any code file it need not be in the autoexec.sas (in fact you wouldn't want it as it there would have the location of the autoexec!)

ballardw
Super User

Something like this perhaps:

 

%let workpath=%sysfunc(pathname(work));

 

At startup Base SAS usually defaults to the work library location.

 

 

jklaverstijn
Rhodochrosite | Level 12

You may use SYSPROCESSNAME.

 

Hope this helps,

- Jan.

jklaverstijn
Rhodochrosite | Level 12
You will have to parse out the value.

If I run

sas /home/jan/test.sas

then I get

SYSPROCESSNAME=Program /home/jan/test.sas
Tom
Super User Tom
Super User

What do you mean by "current program"? How did you submit the program.

If you issued started SAS from the command line then the SYSIN option should have the name of the program you are running.

 

Otherwise is is very difficult for SAS to know how you submitted the program.  

 

Some have suggested using the environment variable (SAS_EXECFILEPATH) that the enhanced program editor sets.  That might work some times IF you are using PC-SAS and IF you are using the enhanced program editor (instead of the many other ways to edit and/or submit SAS code in PC-SAS) and IF you happened to save the program before submitting it.

KBACHU
Fluorite | Level 6

Current program refers to the program which I am executing in SAS EG interface.

 

I am submitting the program from SAS EG.

 

I am using the below code to get the sas program path, but this works only from DMS SAS, and does not work from SAS EG.

 

 

%macro pathfind;
%sysget(SAS_EXECFILEPATH)
%mend pathfind;
%put %pathfind;

 

From SAS EG, I get the below warning message,

 

WARNING: The argument to macro function %SYSGET is not defined as a system variable.

 

Tom
Super User Tom
Super User

SAS_EXECFILEPATH is an environment variable set on your PC by the Enhanced Editor in PC-SAS.  Not surprisingly it will not be set if you submit your program from Enterprise Guide.

 

You could submit a simple program to look at all macro variables.

%put _ALL_;

And environment variables (if you are allowed to use PIPEs).

data _null_;
  set 'env' pipe;
  input;
  put _infile_;
run;

And looks around and see if Enterprise Guide has left any bread crumbs you can follow home.

 

But since your Enterprise Guide projects live on your PC and not on the server where SAS is running.  Also also since the SAS programs live inside the project and not as .sas files on the file system I really doubt you will find any crumbs that the birds have not already eaten.

KBACHU
Fluorite | Level 6

Thank You Tom. Looks like, there is no way I can get the program path name from SAS EG. I will look for other options.

KBACHU
Fluorite | Level 6

Hi All - I found a sasnote which says that I can use _SASPROGRAMFILE from SAS EG/Studio to retrieve the filename/path. I am using the below statmenet

 

%put %sysget(_SASPROGRAMFILE);

 

but getting the message saying that as below.

 

 WARNING: The argument to macro function %SYSGET is not defined as a system variable.

 

Do I need to define %sysget as a system variable? I am connecting to a SAS server running on windows OS.

 

http://support.sas.com/kb/24/301.html

GreyJoy
Obsidian | Level 7

Late reply but here we go. Hopefully this helps someone in the future :

SAS Windows environment maintains the SAS OPTION of SAS_EXECFILEPATH as the location of the SAS file that you are running. 

SAS EG keeps the file path and program name logged in its systems macro variables. The path would be housed in &_SASPROGRAMFILE

 

To assign these values in a macro variable you could do :

1. %let New_path=%sysfunc(dequote(&_SASPROGRAMFILE)); /*----Enterprise Guide-----*/

2. %let New_path=%sysget(SAS_EXECFILEPATH); /*----SAS Windows Environment-----*/

 

Bonus: If you run SAS is batch mode using a BAT. file to open SAS and call the program you want,  the input changes to SYSIN when you use the window CMD language "sysin" to call the file  :

3. %let New_path=%sysfunc(getoption(SYSIN));

 

Tom
Super User Tom
Super User

Note that the SAS_EXECFILEPATH environment variable is not really very reliable.

It only works in the "enhanced" editor, not the normal Display Manager program editor window that works in all versions of SAS.  And then it only knows what name you last used for the file.  You might not have ever saved the file.  You might have modified that file and not saved the changes.

7     * Regular program editor ;
8     %put SAS_EXECFILE=%sysget(SAS_EXECFILEPATH);
SAS_EXECFILE=
9     * Enhanced editor, file saved ;
10    %put SAS_EXECFILEPATH=%sysget(SAS_EXECFILEPATH);
SAS_EXECFILEPATH=C:\downloads\test1.sas
11    * Enhanced editor, file not save yet ;
12    %put SAS_EXECFILEPATH=%sysget(SAS_EXECFILEPATH);
SAS_EXECFILEPATH=

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 14 replies
  • 4573 views
  • 6 likes
  • 6 in conversation