Using SAS and mkdir to create a directory structure in windows

Robert Penridge picture Robert Penridge · Sep 21, 2012 · Viewed 18k times · Source

I want to create a directory structure in Windows from within SAS. Preferably using a method that will allow me to specify a UNC naming convention such as:

\\computername\downloads\x\y\z

I have seen many examples for SAS on the web using the DOS mkdir command called via %sysexec() or the xcommand. The nice thing about the mkdir command is that it will create any intermediate folders if they also don't exist. I successfully tested the below commands from the prompt and it behaved as expected (quoting does not seem to matter as I have no spaces in my path names):

mkdir \\computername\downloads\x\y\z
mkdir d:\y
mkdir d:\y\y
mkdir "d:\z"
mkdir "d:\z\z"
mkdir \\computername\downloads\z\z\z
mkdir "\\computername\downloads\z\z\z"

The following run fine from SAS:

x mkdir d:\x;
x 'mkdir d:\y';
x 'mkdir "d:\z"';
x mkdir \\computername\downloads\x;
x 'mkdir \\computername\downloads\y';

But these do not work when run from SAS,eg:

x mkdir d:\x\x;
x 'mkdir d:\y\y';
x 'mkdir "d:\z\z"';
x mkdir \\computername\downloads\x\y\z ;
x 'mkdir "\\computername\downloads\z"';

** OR **;

%sysexec mkdir "\\computername\downloads\x\y\z ";

** OR **;

filename mkdir pipe "mkdir \\computername\downloads\x\y\z";
data _null_;
  input mkdir;
  put infile;
run;

It does not work. Not only this but the window closes immediately even though I have options xwait specified so there is no opportunity to see any ERROR messages. I have tried all methods with both the UNC path and a drive letter path, ie. D:\downloads\x\y\z.

If I look at the error messages being returned by the OS:

%put %sysfunc(sysrc()) %sysfunc(sysmsg());

I get the following:

-20006 WARNING: Physical file does not exist, d:\downloads\x\x\x.

Looking at the documentation for the mkdir command it appears that it only supports creating intermediate folders when 'command extensions' are enabled. This can be achieved with adding the /E:ON to cmd.exe. I've tried changing my code to use:

cmd.exe /c /E:ON mkdir "\\computername\downloads\x\y\z"

And still no luck!

Can anyone tell me why everyone else on the internet seems to be able to get this working from within SAS except for me? Again, it works fine from a DOS prompt - just not from within SAS.

I'd prefer an answer that specifically address this issue (I know there are other solutions that use multiple steps or dcreate()).

I'm running WinXP 32Bit, SAS 9.3 TS1M2. Thanks.

Answer

Andrew picture Andrew · Sep 14, 2018

Here is a trick that uses the LIBNAME statement to make a directory

options dlcreatedir; libname newdir "/u/sascrh/brand_new_folder";

I believe this is more reliable than an X statement.

Source: SAS trick: get the LIBNAME statement to create folders for you