8

I'd like to specify the current folder. I can find the current folder:

libname _dummy_ ".";
%let folder = %NRBQUOTE(%SYSFUNC(PATHNAME(_DUMMY_)));
%put &folder;

and change it manually by double clicking the current folder status bar, but I'd prefer to code it. Is this possible?

Murray
  • 660
  • 1
  • 8
  • 20

2 Answers2

10

Like this:

x 'cd <full path>';

for example

x 'cd C:\Users\foo';

SAS recognizes that a change directory command was issued to the OS and changes it's current working directory.

fscmj
  • 441
  • 4
  • 5
0

Be aware that the timing of an X statement is like that of other global statements (title, footnote, options, etc). If it is placed within a DATA step, the X statement will be issued prior to the data step execution.

For example, supposing your current working directory is c:\temp. The following writes HelloWorld.txt to c:\temp2 rather than c:\temp. At compile time, SAS runs the X statement and then performs the data step. Note that in SAS, a period (.) is the reference to the current working directory.

data _null_;
  file '.\HelloWorld.txt';
  put 'Hello, world!';
  x 'cd C:\temp2';
run;

To change directories after the data step has executed, you would want to use CALL SYSTEM. CALL statements execute conditionally by being called after a data step.

data _null_;
  file '.\HelloWorld.txt';
  put 'Hello, world!';
  command = 'cd "C:\temp2"';
  call system(command);
run;

More information about these kinds of details for Windows systems can be found in the Running Windows or MS-DOS Commands from within SAS

Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
  • 1
    I think the timing of the X statement is the same as other global statements (title, options, etc.) Shouldn't be a difference between timing of X and %SYSCALL or %SYSEXEC. Can you show an example of a difference? I recognize if you put a global statement such as X inside of a data step, the global statement will be executed before the data step has even finished compiling, but that is the same with %syscall and %sysexec. – Quentin May 05 '17 at 18:58
  • Windows Example would be great. Understand that X statement is executed immediately. I would expect same for %SYSCALL and %SYSEXEC and TITLE and OPTIONS and.... – Quentin May 05 '17 at 19:20
  • I believe you are correct. I cannot produce an example which behaves otherwise. Is that then what the X Statement documentation is referring to when it says, "SAS executes the X statement immediately"? I will update my answer. http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#win-stmt-x.htm – Lorem Ipsum May 05 '17 at 19:27
  • Here is an extremely esoteric example, but one which does illustrate a difference between the `x` statement and using `%syscall system()`. The current working directory must be a UNC path on a network. This happens to me when working from a network file which is not currently mapped (i.e. it begins with a \\ ). In such a case, `%Test()` shows the UNC error `CMD.EXE was started with the above path as the current directory. UNC paths are not supported. Defaulting to Windows directory.` The error seems to appear be because the `x` statement executes immediately (before `temp` is assigned). – Lorem Ipsum May 05 '17 at 20:21
  • `%macro Test(); %let temp = %sysfunc(pathname(WORK)); data _null_; file "&temp.\HelloWorld.txt"; put 'Hello, world!'; run; option xwait; x 'cd "&temp."'; %sysexec(type "&temp.\HelloWorld.txt"); %mend; %Test(); %macro Test2(); %let temp = %sysfunc(pathname(WORK)); data _null_; file "&temp.\HelloWorld.txt"; put 'Hello, world!'; run; option xwait; %let cmd = cd "&temp"; %syscall system(cmd); %sysexec(type "&temp.\HelloWorld.txt"); %mend; %Test2();` – Lorem Ipsum May 05 '17 at 20:21
  • Hmm.. I don't see now the X could be executed before the %LET statement that comes before it. Will have to play with this a bit and see if I can replicate. Thx. – Quentin May 05 '17 at 20:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143542/discussion-between-lorem-ipsum-and-quentin). – Lorem Ipsum May 05 '17 at 20:43