How to change your working directory for SAS® with the DLGCDIR DATA step function

7

How to change your working directory for SASRegardless of the environment in which you run SAS (whether it is SAS® Foundation, SAS® Studio, or SAS® Enterprise Guide®), SAS uses a default location on your host system as a working directory. When you do not specify the use of a different directory within your code, the default location is where SAS stores output.

Beginning with SAS® 9.4 TS1M4, you can use a new DATA step function, DLGCDIR, to change the location for your working directory. You can use this function in Microsoft Windows or UNIX/Linux environments.

Make sure that any directory that you specify with the DLGCDIR function is an existing directory that you have Write or Update access to.

Finding Out What Your Current Directory Is

To determine what your current working directory in SAS is, submit the following code:

   data _null_;
      rc=dlgcdir();
      put rc=;
   run;

Changing Your Windows Directory

The following sample code for Windows sets the working directory in SAS as the TEMP folder on your C: drive:

   data _null_; 
      rc=dlgcdir("c:\temp");
      put rc=;
   run;

Changing Your Linux Directory

This sample code (for a Linux environment) changes the working directory in SAS to /u/your/linux/directory:

   data _null_;
      rc=dlgcdir("/u/your/linux/directory");
      put rc=;
   run;

Changing Your Directory: Other Tips

The DLGCDIR function temporarily changes the working directory for the current SAS or client session. However, you can create an autoexec file that contains the DATA step code that uses the DLGCDIR function. The autoexec file then executes the code each time you invoke SAS.

In most situations, it is still recommended that you specify the intended target directory for the Output Delivery System (ODS) and in other SAS statements. For example, when you use the ODS HTML statement, you should specify the target directory with the PATH option, as shown here:

   ods html path="c:\temp" (url=none) file="sasoutput.html";

Similarly, with the ODS PDF statement, you should specify the target directory with the FILE option, as shown here:

   ods pdf file="c:\temp\sasoutput.pdf";

I hope you've found this post helpful.

Share

About Author

Martin Mincey

Senior Technical Support Analyst, SAS Technical Support

Martin Mincey is a Senior Principal Technical Support Analyst in the Foundation SAS group in Technical Support. He has been in SAS Technical Support since 1984. His main areas of expertise are SAS/GRAPH, ODS, and ODS Graphics.

Related Posts

7 Comments

  1. Leonid Batkhan

    Martin, thank you for this blog post. It's a very useful new SAS function that allows dynamically (programmatically) change your working directory. Could you please let us know what command in the SAS configuration file (sasv9.cfg) or elsewhere controls its default location? I would also emphasize that "working" directory is NOT a location of the WORK library.

    • Martin Mincey
      Martin Mincey on

      Hi Leonid, With an install of SAS on Windows, you can use the SASINITIALFOLDER setting in your SAS configuration file (SASV9.CFG file) to set the default "working" directory at SAS invocation. For example, you can set this in your SASV9.CFG file on Windows by adding a line to the top of the config file that looks like this:

      -SASINITIALFOLDER "C:\your\working\directory"

  2. Peter Lancashire on

    Great to see that SAS 9.4 M4 has made it possible to discover and change the current working directory. The need for this has been obvious for so long that the bodged workaround with X was documented. Unix has been able to do this from the beginning (pre-1986) and MS-DOS from very early. What took so long? And what is the DLG part of the name? I would have found something like CHGWDIR easier to remember, if you must limit to 7 characters. This kind of obscurity will not convert new users to SAS.

    • Peter Crawford on

      No running of system commands through PIPE or X was required
      Filename here '.' ;
      %let here= %sysfunc( pathname( here ));
      should have provided what you wanted since EG became available

  3. Very useful. It would be even more useful if your first example:

    data _null_;
    rc=dlgcdir();
    put rc=;
    run;

    stored the value into a data step variable rather than writing it to the log as a NOTE:. I could see using some logic to make decision based on the working directory folder. Still, quite useful.

    • Martin Mincey
      Martin Mincey on

      Hi Al, After researching this a bit more here, it turns out that the DLGCDIR function is not designed to return the current working directory in a DATA step variable or macro variable. But the following sample code when run on Windows creates both a DATA step variable and a macro variable that contains the current working directory:

      filename curdir pipe 'echo %CD%';

      data sasdata;
      infile curdir truncover;
      length currentdir $ 100;
      input currentdir $char100.;
      put currentdir=;
      call symput('curwdir',currentdir);
      run;

      %put Current working directory is &curwdir;

  4. Peter Crawford on

    That DLG prefix was previously a hint that the function operated to open a dialog box in interactive SAS - hence the lack of returned value.
    Sometimes I assume too much
    Does the dlgcdir() function work within %SYSFUNC()?

Leave A Reply

Back to Top