How to Change Your SAS Working Directory with DLGCDIR (Tutorial)

In SAS, the working directory is the default location where SAS looks for input files and writes output.

The DLGCDIR data step function provides a way to change the working directory during your SAS session.

Find the current directory in SAS

Here’s how you can find the current working directory in SAS. Read the article for an explanation of the code.

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

This will print the current working directory to your SAS log.

Current Working Directory: /pbr/biconfig/940/Lev1/SASApp

What is the DLGCDIR function?

Introduced in SAS 9.4, the DLGCDIR function allows you to temporarily change the working directory for your SAS session. Key points to remember:

  • Temporary Change: The change the working directory is only in effect for the duration of your current SAS session.
  • Permissions: Ensure you have the necessary write or update permissions for the directory you want to specify.
  • Operating Systems: This function works in Windows and UNIX/Linux environments.

How to Use the DLGCDIR Function

The syntax is straightforward:

data _null_;
   rc = dlgcdir("C:\MySASProjects");
   put rc=;


Let’s say you want to change your working directory to “/home/usr/Datasets” on a Unix system:

data _null_;
   rc = dlgcdir("/home/usr/Datasets");
   put rc=; 

Now, any SAS statements that reference files without a full path will assume those files are located in the “/home/usr/Datasets” directory.

Automating with the Autoexec File

If you frequently work with a specific directory, you can automate the process of setting the working directory using SAS’s autoexec file.

  1. Create an autoexec file: Create a simple text file named “” in a location SAS will recognize (ask your SAS administrator if unsure).
  2. Add the code: Put the DLGCDIR code into your autoexec file. For example:
data _null_;
   rc = dlgcdir("/home/usr/Datasets");
   put rc=; 

After executing the code, a confirmation note will appear in the SAS log indicating that the working directory has been updated.

NOTE: The current working directory is now "/home/subhroster20070/Datasets".

Now each time you start an SAS session, the code in your autoexec file will run, setting your working directory.

In Conclusion

The DLGCDIR function offers a simple and effective way to manage your working directory during your SAS sessions. Understanding how to use it will help you organize your projects better and make your SAS coding more efficient.

Let me know if you have any specific scenarios or other SAS tasks you’d like to explore in a blog post. I’m happy to help!

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



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.