We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Find current directory path

by Super User Friday - edited Friday by Community Manager (627 Views)

Looking at some SAS code that was working very hard to figure out what directory to use for default output I was reminded of a macro that Tom Hoffman created for me almost 20 years ago that I still use all of the time.  So I have posted a version of it on github

curdir.sas

 

The idea is simple. Create a fileref pointing to the current working directory and then ask SAS what path that fileref points to. The path is then returned as the content of the macro.  That way you can embed the macro call into other SAS statements.

 

 

%macro curdir;
/*----------------------------------------------------------------------
Returns the current SAS directory physical name.
----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
Originally developed by Tom Hoffman.
Posted in memory of Tom and Fan.
-----------------------------------------------------------------------
Usage:

%put %curdir is the current directory.;
------------------------------------------------------------------------
Notes:

-----------------------------------------------------------------------
History:

11MAR99 TRHoffman Creation - with help from Tom Abernathy.
06DEC00 TRHoffman Used . notation to refernece current directory as
                  suggested by Fan Zhou.
----------------------------------------------------------------------*/
%local fr rc curdir;

%let rc = %sysfunc(filename(fr,.));
%let curdir = %sysfunc(pathname(&fr));
%let rc = %sysfunc(filename(fr));

&curdir

%mend curdir;

 

Comments
by Trusted Advisor
Friday

Big one @Tom :) Thanks to you and your peers

by Super User
Saturday

The following code is the same with your macro ?

 


%let path=%sysfunc(pathname(SASROOT));
%put &path;
by Super User
Saturday

The libref SASROOT points to where SAS is installed, not the current working directory for the process that is running SAS.

 

If you run SAS from the command line the current directory is normally the directory where you issued the command. If you are using a server (via SAS/Studio or EG for example) then it is normally the directory where the application server was launched.

 

But if your SAS code had submitted any cd commands then the current directory could be very different that what is was when the SAS program started.

by Regular Contributor
Saturday

sasroot is the name of an environment variable:

 

%put sasroot: %sysget(sasroot);

 

This is the path your work folder:

 

%put sasuser: %sysfunc(getoption(sasuser));


if you are submitting programs in batch then startup-only option sysin contains the path/filename.ext

 

 %put sysin: %sysfunc(getoption(sysin));

 

I can't remember the session option/macro-variable which has this path+filename.

 

trivial comment: you can code this without the assignment and reference to curdir

 

%let rc = %sysfunc(filename(fr,.));
/* %let curdir =  */ %sysfunc(pathname(&fr));
%let rc = %sysfunc(filename(fr));

/*&curdir */ %mend curdir;

 

by Occasional Contributor NovenberGetRight
Tuesday - last edited Tuesday

Two comments:
1,  it looks only work in SAS DM, in EG it returns an incorrect path "C:\WINDONS\system32" ;

2,  please add a semi-colon in the end of &curdir   in your code just like  "%put  &curdir;"

 

by Super User
Tuesday - last edited Tuesday

Two comments:
1,  it looks only work in SAS DM, in EG it returns an incorrect path "C:\WINDONS\system32" ;

That seems like a likely current directory for the process that is running SAS.  If you want to change it you would need to submit an operating system command at some point in your SAS code.  Such as :

 

x "cd c:\My Shared Data Folder\";

2,  please add a semi-colon in the end of &curdir   in your code just like  "%put  &curdir;"

 

Not sure what you are talking about here. The example line in the program header does include a semi-colon to end the %PUT statement. 

 

%put %curdir is the current directory.;

 

by Occasional Contributor NovenberGetRight
yesterday

Unnamed QQ Screenshot20180815123421.png

by Regular Contributor
yesterday

What would this be useful for?

by Super User
4 hours ago

When you make a "function" style macro the only SAS code it should generate are the characters of the data value that you wan it to generate. If you include a semi-colon in the SAS code that the macro generates then you can no longer use the macro call as if it was a function.  That extra semi-colon would cause havoc with the actual SAS statement that you are trying to use the macro function to generate.

 

If I tried to use the macro call in a single statement like:

%let target=%curdir/logs;

And the current directory was /home/mydir then SAS would see the result as these TWO statements.

%let target=/home/mydir;
/logs;

And SAS would not understand what a command like 

/logs;

was supposed to mean.

by Super User
4 hours ago - last edited 4 hours ago

What would this be useful for?

The same reason that you would call for the current directory in any other programming language.

 

The best use I have put it to is to have SAS automatically detect what project I am working  on based on which directory I was in when I launched SAS.   So if my file system is organized to have tasks nested under projects I could parse the current directory to figure out which task for which project is being worked on.

%let curdir=%curdir;
%let projcode=%scan(&curdir,4,/);
%let taskcode=%scan(&curdir,5,/);
proc sql noprint;
  select title,manager
    into :proj_title trimmed
       , :manager_name trimmed
    from sysdata.projects
    where project="&projcode" and task="&taskcode"
  ;
quit;
TITLE1 "&proj_title";
FOOTNOTE1 "See &manager_name";
...

 

by New User marrygold
2 hours ago

When you make a "capacity" style full scale the main SAS code it ought to produce are the characters of the information esteem that you want it to create. In the event that you incorporate a semi-colon in the SAS code that the large-scale creates then you can never again utilize the full-scale call as though it was a capacity. That additional semi-colon would cause devastation with the genuine SAS proclamation that you are attempting to utilize the large-scale capacity to create 
Do my Assignment for Me

Contributors
Your turn
Sign In!

Want to write an article? Sign in with your profile.


Looking for the Ask the Expert series? Find it in its new home: communities.sas.com/askexpert.