SAS Tips from the Community

SAS tips from real SAS users like you
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 21882 views
  • 5 likes
  • 3 in conversation