2 ways to Find the Current Working Directory in SAS

2 ways to Find the Current Working Directory in SAS

Current directory is the default location where SAS will look for input files and write output files unless you specify otherwise.

There are two effective methods to find your current working directory in SAS:

Method 1: Using SAS Macro

%macro cwd;
   %let rc=%sysfunc(filename(cwd,.)); 
   %let cwd=%sysfunc(pathname(&cwd));
   %put Current Working Directory: &cwd; 
%mend;
%cwd

Explanation:

  • %macro cwd; : Defines a macro named “cwd”.
  • %let rc=%sysfunc(filename(cwd, .)); : Assigns a fileref named “cwd” to the current directory (represented by “.”). The filename function is used to get the assigned fileref’s path.
  • %let cwd=%sysfunc(pathname(cwd)); : Retrieves the full path of the “cwd” fileref using the pathname function.
  • %put Current Working Directory: &cwd; : Prints the retrieved path to the log

Method 2: Using the PATHNAME Function directly

The PATHNAME function is a useful tool in SAS that allows you to retrieve the physical name of an external file or library.

By using the PATHNAME function, you can easily obtain the file’s or library’s physical name, which can be used in various SAS procedures and data steps.

data _null_;
   rc = filename('cwd',.);
   cwd = pathname('cwd');
   put cwd=;
run;

These methods provide the current working directory at the time of execution. If you change your working directory using DLGCDIR or other methods later, you’ll need to rerun these code snippets to get the updated path.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.