Does the Directory Exist
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.
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.
- How to Check if a Directory Already Exists and if not, Create it from SAS.com
- The paperBuilding Intelligent Macros: Using Metadata Functions with the SAS Macro Language contains a description of the %CHECKLOC macro to check for the existence of a directory and create it if it does not already exist.