<?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: Check if folder is empty in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792411#M253879</link>
    <description>&lt;P&gt;&lt;SPAN class="VIiyi"&gt;&lt;SPAN class="JLqJ4b ChMk0b" data-language-for-alternatives="en" data-language-to-translate-into="de" data-phrase-index="0" data-number-of-phrases="1"&gt;&lt;SPAN&gt;Assuming you want to export to the directory, which must be empty, you could just check if the file exists. Depending on the SAS version you are using the following code needs to be wrapped in a macro.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if not %sysfunc(fileexist(c:/list.csv)) %then %do;
  &amp;lt; insert proc export here &amp;gt;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Jan 2022 10:40:30 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-01-26T10:40:30Z</dc:date>
    <item>
      <title>Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792404#M253873</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I check if the folder is empty and if it is empty it must create a file?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got the part to create a new file. I'm just stuck at the part where I need to create this file if the folder directory is completely empty&lt;/P&gt;&lt;PRE&gt;proc export 
	data=files
    outfile="C:/List.csv" 
    dbms=tab;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 09:55:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792404#M253873</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T09:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792405#M253874</link>
      <description>&lt;P&gt;List all files in the folder and if 0 number of files is returned, the folder is empty :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to: List all the files in a folder&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-List-all-the-files-in-a-folder/m-p/674065#M202918" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-List-all-the-files-in-a-folder/m-p/674065#M202918&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 10:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792405#M253874</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-01-26T10:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792406#M253875</link>
      <description>&lt;P&gt;I think you can use the DOPEN and DNUM functions for that, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename myfolder "&amp;lt;path to folder&amp;gt;";

data _null_;
  dir_id=dopen("myfolder");
  if dnum(dir_id)=0 then do;
       /* do what is necessary if folder is empty */
    end;
  rc=dclose(dir_id);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 10:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792406#M253875</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-01-26T10:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792408#M253876</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename d 'c:\temp'; /* specify directory for checking */
data _null_;
  did=dopen('d');
  if did&amp;gt;0 then do;
    filecnt=dnum(did);
    call symputx('filecnt',filecnt);
    rc=dclose(did);
  end;
run;

%if &amp;amp;filecnt=0 %then %do;
  /* Describe what to do if the number of files is zero here. */
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 10:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792408#M253876</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-26T10:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792411#M253879</link>
      <description>&lt;P&gt;&lt;SPAN class="VIiyi"&gt;&lt;SPAN class="JLqJ4b ChMk0b" data-language-for-alternatives="en" data-language-to-translate-into="de" data-phrase-index="0" data-number-of-phrases="1"&gt;&lt;SPAN&gt;Assuming you want to export to the directory, which must be empty, you could just check if the file exists. Depending on the SAS version you are using the following code needs to be wrapped in a macro.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if not %sysfunc(fileexist(c:/list.csv)) %then %do;
  &amp;lt; insert proc export here &amp;gt;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 10:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792411#M253879</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-01-26T10:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792413#M253880</link>
      <description>&lt;BR /&gt;/*file paths*/&lt;BR /&gt;%let path_in= C:/Files;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data test;&lt;BR /&gt;dir_id=dopen("&amp;amp;path_in");&lt;BR /&gt;%if dnum(dir_id)=0 %then %do;&lt;BR /&gt;/* do what is necessary if folder is empty */&lt;BR /&gt;proc export&lt;BR /&gt;data=tables&lt;BR /&gt;outfile="C:/Files/Log.csv"&lt;BR /&gt;dbms=tab;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;rc=dclose(dir_id);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Tried the above but I get an error:&lt;BR /&gt;ERROR: Required operator not found in expression: dnum(dir_id)=0&lt;BR /&gt;ERROR: Skipping to next %END statement.</description>
      <pubDate>Wed, 26 Jan 2022 10:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792413#M253880</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T10:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792418#M253884</link>
      <description>&lt;P&gt;You are mixing data step and macro code, which won't work (macro triggers are resolved before the code is handed off to the data step compiler).&lt;/P&gt;
&lt;P&gt;Furthermore, as stated in your other thread, the PROC statement terminates the DATA step anyway.&lt;/P&gt;
&lt;P&gt;Next, the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0hpa8p9kacbran1ndqiw3krwohq.htm" target="_blank" rel="noopener"&gt;DOPEN Function&lt;/A&gt; needs a file &lt;EM&gt;reference&lt;/EM&gt; as argument, not a file &lt;EM&gt;name&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;To make code conditional on the non-existence of files in a directory, you can do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path_in= C:/Files;

filename dref "&amp;amp;path_in";

%let did = %sysfunc(dopen(&amp;amp;dref.));

%if %sysfunc(dnum(&amp;amp;dref.)) = 0
%then %do;
  /* further code */
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Jan 2022 11:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792418#M253884</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-01-26T11:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Check if folder is empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792438#M253896</link>
      <description>&lt;P&gt;This macro will export the folder contents to a dataset:&amp;nbsp;&amp;nbsp;&lt;A href="https://core.sasjs.io/mp__dirlist_8sas.html" target="_blank"&gt;https://core.sasjs.io/mp__dirlist_8sas.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;If the dataset is empty, the folder is empty&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 12:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-folder-is-empty/m-p/792438#M253896</guid>
      <dc:creator>AllanBowe</dc:creator>
      <dc:date>2022-01-26T12:30:06Z</dc:date>
    </item>
  </channel>
</rss>

