<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SAS Tip: Check if a directory exists in SAS Tips from the Community</title>
    <link>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475994#M186</link>
    <description>&lt;P&gt;Thanks.&amp;nbsp; 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:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/direxist.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/direxist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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(&amp;amp;path) = ) %then %goto quit;
%if ^%sysfunc(fileexist(&amp;amp;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,&amp;amp;path))) %then %do;
  %let did=%sysfunc(dopen(&amp;amp;fileref));
  %if (&amp;amp;did) %then %do;
    %let return=1;
    %let rc=%sysfunc(dclose(&amp;amp;did));
  %end;
  %else %let sysrc=1;
  %let rc=%sysfunc(filename(fileref));
%end;

%quit:
%*----------------------------------------------------------------------
Return the value as the result of the macro.
-----------------------------------------------------------------------;
&amp;amp;return
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 06 Jul 2018 14:05:27 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-07-06T14:05:27Z</dc:date>
    <item>
      <title>SAS Tip: Check if a directory exists</title>
      <link>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475988#M183</link>
      <description>&lt;P&gt;This&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;macro&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;returns 1 if the specified directory exists, 0 if not. It takes advantage of the fact that the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;fexist&lt;/I&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;can check for the existence of both directories and files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD valign="top"&gt;
&lt;DIV class="mw-geshi mw-code mw-content-ltr" dir="ltr"&gt;
&lt;DIV class="sas source-sas"&gt;
&lt;PRE class="de1"&gt;&lt;SPAN class="kw2"&gt;%macro&lt;/SPAN&gt; DirExist&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;dir&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;&amp;nbsp;; 
   &lt;SPAN class="kw2"&gt;%LOCAL&lt;/SPAN&gt; rc &lt;SPAN class="kw3"&gt;fileref&lt;/SPAN&gt; &lt;SPAN class="kw4"&gt;return&lt;/SPAN&gt;; 
   &lt;SPAN class="kw2"&gt;%let&lt;/SPAN&gt; rc = &lt;SPAN class="kw2"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&lt;SPAN class="kw3"&gt;filename&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&lt;SPAN class="kw3"&gt;fileref&lt;/SPAN&gt;,&lt;SPAN class="re0"&gt;&amp;amp;dir&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;&amp;nbsp;; 
   &lt;SPAN class="kw2"&gt;%if&lt;/SPAN&gt; &lt;SPAN class="kw2"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&lt;SPAN class="kw3"&gt;fexist&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&amp;amp;&lt;SPAN class="kw3"&gt;fileref&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;  &lt;SPAN class="kw2"&gt;%then&lt;/SPAN&gt; &lt;SPAN class="kw2"&gt;%let&lt;/SPAN&gt; &lt;SPAN class="kw4"&gt;return&lt;/SPAN&gt;=&lt;SPAN class="nu0"&gt;1&lt;/SPAN&gt;;    
   &lt;SPAN class="kw2"&gt;%else&lt;/SPAN&gt; &lt;SPAN class="kw2"&gt;%let&lt;/SPAN&gt; &lt;SPAN class="kw4"&gt;return&lt;/SPAN&gt;=&lt;SPAN class="nu0"&gt;0&lt;/SPAN&gt;;
   &amp;amp;&lt;SPAN class="kw4"&gt;return&lt;/SPAN&gt;
&lt;SPAN class="kw2"&gt;%mend&lt;/SPAN&gt; DirExist;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/TD&gt;
&lt;TD valign="top"&gt;
&lt;DIV class="mw-geshi mw-code mw-content-ltr" dir="ltr"&gt;
&lt;DIV class="sas source-sas"&gt;
&lt;PRE class="de1"&gt;&lt;SPAN class="coMULTI"&gt;/* Usage */&lt;/SPAN&gt;
option noxwait; &lt;SPAN class="coMULTI"&gt;/* SAS 8.2 only */&lt;/SPAN&gt;
&lt;SPAN class="kw2"&gt;%put&lt;/SPAN&gt;&amp;nbsp;%DirExist&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;C:\Documents &lt;SPAN class="kw4"&gt;and&lt;/SPAN&gt; Settings\&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
&lt;SPAN class="kw2"&gt;%put&lt;/SPAN&gt;&amp;nbsp;%DirExist&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;aaa&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;This tip was originally posted by Adrien Vallee on sasCommunity.org.&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jul 2018 13:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475988#M183</guid>
      <dc:creator>SAS_Tipster</dc:creator>
      <dc:date>2018-07-06T13:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Tip: Check if a directory exists</title>
      <link>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475993#M185</link>
      <description>&lt;P&gt;Thats a lot of faff for what is:&lt;BR /&gt;Datastep:&lt;/P&gt;
&lt;PRE&gt;want=fexist("c:\temp");&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro:&lt;/P&gt;
&lt;PRE&gt;%if %sysfunc(fexist("c:\temp")) %then %do;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Jul 2018 13:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475993#M185</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-06T13:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Tip: Check if a directory exists</title>
      <link>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475994#M186</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp; 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:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/direxist.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/direxist.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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(&amp;amp;path) = ) %then %goto quit;
%if ^%sysfunc(fileexist(&amp;amp;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,&amp;amp;path))) %then %do;
  %let did=%sysfunc(dopen(&amp;amp;fileref));
  %if (&amp;amp;did) %then %do;
    %let return=1;
    %let rc=%sysfunc(dclose(&amp;amp;did));
  %end;
  %else %let sysrc=1;
  %let rc=%sysfunc(filename(fileref));
%end;

%quit:
%*----------------------------------------------------------------------
Return the value as the result of the macro.
-----------------------------------------------------------------------;
&amp;amp;return
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jul 2018 14:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-directory-exists/m-p/475994#M186</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-06T14:05:27Z</dc:date>
    </item>
  </channel>
</rss>

