As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Does the Directory Exist"

From sasCommunity
Jump to: navigation, search
(Created article)
 
(add (archived) link about creating a folder, if it doesn't already exist)
Line 1: Line 1:
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.
+
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]] [[User:Avallee|Adrien VALLEE]] demonstrates a macro that uses the FILENAME and FEXIST functions.
 
In the tip [[:Tips:Check if a directory exists]] [[User:Avallee|Adrien VALLEE]] demonstrates a macro that uses the FILENAME and FEXIST functions.
Line 26: Line 26:
  
 
In this final case the parameter could be a file as well as a directory.
 
In this final case the parameter could be a file as well as a directory.
 +
 +
== External links ==
 +
* [http://web.archive.org/web/20160526040359/http://www.sas.com/offices/europe/uk/support/sas-hints-tips/ht1_mar04.html How to Check if a Directory Already Exists and if not, Create it] from SAS.com
 +
 
[[Category:FILENAME Function]]
 
[[Category:FILENAME Function]]
 
[[Category:%SYSFUNC Function]]
 
[[Category:%SYSFUNC Function]]

Revision as of 17:03, 27 February 2017

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