HomeContact
How to Change Your SAS Working Directory with DLGCDIR (Tutorial)
February 21, 2024
1 min

Table Of Contents

01
Find the current directory in SAS
02
What is the DLGCDIR function?
03
How to Use the DLGCDIR Function
04
Automating with the Autoexec File

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;`
%mend;
%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_;<br/> rc = dlgcdir(""C":\MySASProjects");<br/> put rc=; <br/>run;<br/> Example

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=;
run;

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. $1

  2. $1

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

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!


Share


© 2025 9to5sas
AboutContactPrivacyTerms