Does the Directory Exist

From sasCommunity
Jump to: navigation, search

There are several ways to check to see if a directory (or a file) exists. These include the use of functions and often, though not necessarily, the macro language.

In the tip Tips:Check if a directory exists Adrien VALLEE demonstrates a macro that uses the FILENAME and FEXIST functions.

Additional discussion of that tip includes other approaches.

The FILEEXIST function eliminates the need to create the FILEREF first (which should then be cleared)

%if %sysfunc(fileexist(&dir))  %then %let return=1;    
%else %let return=0;

--Art Carpenter 18:02, 7 January 2011 (UTC)


Since the return values are 0/1, we can further simplify as follows:

%macro DirExist(dir) ; 
   %sysfunc(filename(fileref,&dir)) 
%mend DirExist;

Here the use of macro is simply a wrapper to hide the use of %sysfunc. --Don Henderson 01:51, 17 January 2011 (UTC)

In this final case the parameter could be a file as well as a directory.

External links