BookmarkSubscribeRSS Feed
🔒 This topic is locked. We are no longer accepting replies to this topic. Need further help? Please sign in and ask a new question.
SAS_Tipster
Moderator

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. 

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Thats a lot of faff for what is:
Datastep:

want=fexist("c:\temp");

 

Macro:

%if %sysfunc(fexist("c:\temp")) %then %do;
Tom
Super User Tom
Super User

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;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Visit a random SAS tip This SAS Tips board is not open for replies or comments, but we welcome your feedback and questions. Have a question or comment about this tip? Start a new topic in one of our discussion boards, and reference this tip topic.
Discussion stats
  • 2 replies
  • 20355 views
  • 5 likes
  • 3 in conversation