如何检查目录是否已存在,如果没有,请创建它?

时间:2014-09-04 07:27:18

标签: windows-7 sas enterprise-guide

如何检查目录是否已存在,如果没有,请创建它吗?

我在Windows 7下使用SAS 9.3服务器和SAS EG 5.1。

%macro chk_dir(dir=) ; 
   options noxwait; 
   %local rc fileref ; 
   %let rc = %sysfunc(filename(fileref,&dir)) ; 
   %if %sysfunc(fexist(&fileref))  %then 
      %put NOTE: The directory "&dir" exists ; 
   %else 
     %do ; 
         %sysexec md   &dir ; 
         %put %sysfunc(sysmsg()) The directory has been created. ; 
   %end ; 
   %let rc=%sysfunc(filename(fileref)) ; 
%mend chk_dir ; 

此代码来自SAS website

但是当我尝试使用此宏%chk_dir(dir=E:\foo\20140904_test);创建文件夹时,它无法创建文件夹,我收到以下日志消息:

MLOGIC(CHK_DIR):  %SYSEXEC  md   &dir
SYMBOLGEN:  Macro variable DIR resolves to E:\foo\20140904_test
ERROR: Shell escape is not valid in this SAS session. 
MLOGIC(CHK_DIR):  %PUT %sysfunc(sysmsg()) The directory has been created.

目录E:\foo'确实存在,并且调用%chk_dir(dir=E:\foo);按预期提供日志输出:NOTE: The directory "E:\foo" exists

1 个答案:

答案 0 :(得分:1)

你可以添加

Options DLCREATEDIR;

到你的代码 - 如果它不存在,这将自动创建文件夹。否则,如果您尝试将结果保存到不存在的文件夹,则会收到错误。

但这只能解决保存问题,如果您只是想检查,它不会提供任何解决方案。