基本上能完成版上经常有人问到的大部分基础任务
我不像各位版大那样勤劳,可能隔三差五来一发吧
这个宏是用来检查指定目标文件夹或文件是否存在的,若存在则删除,不存在则创建,用于解决在dos中和SAS中无法创建文件夹的问题
例如可以在运行proc export前ChkFile下
- %macro ChkFile(OutFilePath);
- /**********************************************************************/
- /* 此宏的作用是检查指定路径的文件夹或文件是否存在。若指定路径包含文件 */
- /* 名,则运行此宏后路径中包含的文件夹存在但文件不存在,方便以后操作; */
- /* 若指定路径不包含文件名,则运行此宏后路径中包含的文件夹存在。其中, */
- /* OutFilePath是指定的文件夹或文件路径。注意如果路径中包含文件名,则 */
- /* 一定要写全文件名和其扩展名。 */
- /* */
- /* Created on 2011.9.29 */
- /* Modified on 2011.9.29 */
- /**********************************************************************/
- /* 情形1:当输入参数OutFilePath包含文件名时,注意文件名必须带后缀 */
- %if %SYSFUNC(FIND(&OutFilePath,%Str(.))) NE 0 %then %do;
- /* 得到OutFilePath中包含的文件名File和文件夹路径Dir */
- %let File=%SYSFUNC(SCAN(&OutFilePath,-1,\));
- %let Dir=%SYSFUNC(SUBSTR(&OutFilePath,1,%EVAL(%SYSFUNC(LENGTH(&OutFilePath))-%SYSFUNC(LENGTH(&File)))));
- options noxwait;
- %local rc1 fileref1;
- %local rc2 fileref2;
- %let rc1=%SYSFUNC(FILENAME(fileref1,&Dir));
- %let rc2=%SYSFUNC(FILENAME(fileref2,&OutFilePath));
- %if %SYSFUNC(FEXIST(&fileref1)) %then %do;
- %put NOTE: The directory "&Dir" exists.;
- %if %SYSFUNC(FEXIST(&fileref2)) %then %do; /* 文件夹存在且文件也存在的情形 */
- %SYSEXEC del &OutFilePath;
- %put NOTE: The file "&File" also exists, and has been deleted.;
- %end;
- %else %put NOTE: But the file "&File" does not exist.; /* 文件夹存在且文件不存在的情形 */
- %end;
- %else %do; /* 文件夹不存在的情形 */
- %SYSEXEC md &Dir;
- %put %SYSFUNC(SYSMSG()) The directory has been created.;
- %end;
- %let rc1=%SYSFUNC(FILENAME(fileref1));
- %let rc2=%SYSFUNC(FILENAME(fileref2));
- %end;
- /* 情形2:当输入参数OutFilePath不包含文件名时 */
- %else %do;
- options noxwait;
- %local rc fileref;
- %let rc=%SYSFUNC(FILENAME(fileref,&OutFilePath));
- %if %SYSFUNC(FEXIST(&fileref)) %then
- %put NOTE: The directory "&OutFilePath" exists.;
- %else %do;
- %SYSEXEC md &OutFilePath;
- %put %SYSFUNC(SYSMSG()) The directory has been created.;
- %end;
- %let rc=%SYSFUNC(FILENAME(fileref));
- %end;
- %mend;
- %macro Demo();
- %ChkFile(d:\temp\data.xls);
- %mend;