This macro returns 1 if the specified directory exists, 0 if not. It takes advantage of the fact that the fexist can check for the existence of both directories and files.
%macro DirExist(dir) ; %LOCAL rc fileref return; %let rc = %sysfunc(filename(fileref,&dir)) ; %if %sysfunc(fexist(&fileref)) %then %let return=1; %else %let return=0; &return %mend DirExist; |
/* Usage */ option noxwait; /* SAS 8.2 only */ %put %DirExist(C:\Documents and Settings\); %put %DirExist(aaa); |
This tip was originally posted by Adrien Vallee on sasCommunity.org.
Thats a lot of faff for what is:
Datastep:
want=fexist("c:\temp");
Macro:
%if %sysfunc(fexist("c:\temp")) %then %do;
Thanks. But that just checks if the path exists, but not whether it is really a directory or a normal file. To really test if a path is a directory I like to use this macro:
https://github.com/sasutils/macros/blob/master/direxist.sas
In addition to returning 1 (true) or 0 (false) it will also set SYsrc=1 to let you know that the path is a normal file instead of a directory.
%macro direxist
/*----------------------------------------------------------------------
Test if directory exists
----------------------------------------------------------------------*/
(path /* Name of directory to test for existance */
);
/*----------------------------------------------------------------------
Test if directory exists. Returns value 1 or 0.
1 - Directory exists
0 - Directory does not exist
Global macro variable SYSRC will be set to 1 when a file is found
instead of a directory.
----------------------------------------------------------------------*/
%local return rc did fileref;
%*----------------------------------------------------------------------
Set up return values as normal failure to find path.
-----------------------------------------------------------------------;
%let return=0;
%let sysrc=0;
%*----------------------------------------------------------------------
If path is not specified or does not exist then return normal failure.
-----------------------------------------------------------------------;
%if (%bquote(&path) = ) %then %goto quit;
%if ^%sysfunc(fileexist(&path)) %then %goto quit;
%*----------------------------------------------------------------------
Try to open it using DOPEN function.
Return 1 if it can be opened.
Otherwise set SYsrc=1 to mean that PATH exists but is not a directory.
-----------------------------------------------------------------------;
%if (0=%sysfunc(filename(fileref,&path))) %then %do;
%let did=%sysfunc(dopen(&fileref));
%if (&did) %then %do;
%let return=1;
%let rc=%sysfunc(dclose(&did));
%end;
%else %let sysrc=1;
%let rc=%sysfunc(filename(fileref));
%end;
%quit:
%*----------------------------------------------------------------------
Return the value as the result of the macro.
-----------------------------------------------------------------------;
&return
%mend;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!