3

How can I determine the name and path of the program I am currently running? (In Windows, sas 9.1.3 - but techniques for other versions / environments would also be good to know..)

flag

5 Answers

3

You can use the Environment variables SAS_EXECFILEPATH and SAS_EXECFILENAME.

like so...

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

%macro xfile;
  %sysget(SAS_EXECFILENAME)
%mend xfile;
%put %xfile;

On Unix/Linux systems (but only in batch mode - not dms) you could probably use the dictionary.extfiles table (or sashelp.vextfl) since SAS_EXECFILEPATH and SAS_EXECFILENAME are Windows only environment variables:

proc sql noprint;
select 
  scan(xpath,-1,'/') into :xfile
  from dictionary.extfiles
  where upcase(xpath) like '%.SAS';
select 
  xpath into :xfilepath
  from dictionary.extfiles
  where upcase(xpath) like '%.SAS';
quit;
%put &xfile.;
%put &xfilepath.;
link|flag
The second approach is likely more widely applicable since I'm fairly certain that the macro variables are not populated in batch (even on Windows) – jay.l.stevens Apr 9 at 4:50
3

The following paper expands on the above answer by resolving windows filepaths into UNC paths, which should make code reliant on access to the location, rather than potentially transient drive mappings: The Path, The Whole Path, And Nothing But the Path, So Help Me Windows

link|flag
two excellent answers - so I chose the one that arrived first! – NextLevel-IS Nov 7 at 12:00
2

To get the last opened filename:

data _null_;
set sashelp.vextfl(where=(upcase(xpath) like '%.SAS'));
call symput('program', scan(xpath,-1,'\'));
run;

%put &program;

**result: filename.sas**

To get filepath, use the following code;

data _null_;
set sashelp.vextfl
(where=(upcase(xpath) like '%.SAS'));
call symput('program', xpath)); 
run;

%put &program;

**Result:**
H:\Company\Client\Study\ABC304\Programs\filename.sas

Sarath Annapareddy StudySAS Blog

link|flag
This is essentially the same solution as the second example in the Accepted answer. Although I do think that this is possibly slightly more stable. – jay.l.stevens Jul 4 at 16:53
2

In batch mode you can use:

%put %sysfunc(getoption(sysin));

link|flag
This is much more elegant, but won't work when in interactive debugging mode; unless, I guess you could specify the -sysin at session startup and then it would still work properly. – jay.l.stevens Jul 12 at 12:16
1

beware the Environment variables SAS_EXECFILEPATH and SAS_EXECFILENAME
These are filled through the "file-open" dialog of enhanced editor (hence windows-only). Since there can be more than one such editor window open, which do you think the (single) environment variable SAS_EXECFILEPATH should show?

link|flag
I'm pretty sure this is correct and shouldn't have been downvoted since when running in batch (even on Windows) SAS_EXECFILEPATH and SAS_EXECFILENAME don't appear to be populated. – jay.l.stevens Apr 9 at 4:49

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.