<?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: Recursive find files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857261#M338734</link>
    <description>&lt;P&gt;If your subtree is too large to load into a hash object you can use the MODIFY statement to affect a recursive search.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/dirtree.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/dirtree.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 05 Feb 2023 17:41:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-02-05T17:41:42Z</dc:date>
    <item>
      <title>Recursive find files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857260#M338733</link>
      <description>&lt;P&gt;Here is a new twist on an old trope.&amp;nbsp; Recursive find files will create a data set listing all files below a root path to a certain depth.&amp;nbsp; Recursion in DATA Step made possible by maintaining state data (i.e. stack) in a hash object.&amp;nbsp; The only downside is that sub-folder detection in SAS is done via MOPEN returning a 0, which means every file detected has an attempted open and it takes a lot longer to get the results than say a bash find command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let root = G:;
%let maxdepth = 2;

data find_files (keep=folder filename);
  length folder $2000 fref $8 filename $2000 index dnum 8 ;

  call missing (depth, did, folder, index, dnum);

  declare hash state ();
  state.defineKey ('depth');
  state.defineData ('fref', 'did', 'folder', 'index', 'dnum');
  state.defineDone();

  folder = "&amp;amp;root";

  depth = 0;
  maxdepth = &amp;amp;maxdepth;

  link push;
  goto iterate;

push:
  depth = depth + 1;

  link fref;
  did = dopen (fref);

  if did = 0 then stop;

  filename = '';
  output;

  dnum = dnum (did);
  index = 0;

  state.add();

  if depth &amp;gt; maxdepth then do;
    link pop;
    goto iterate;
  end;
return;

%*-------------------------------------------------------;
iterate:
%*-------------------------------------------------------;
  if index &amp;gt;= dnum then do;
    link pop;
    goto iterate;
  end;

  index + 1;
  state.replace();

  filename = dread(did, index);

  mid = mopen(did, filename);

  if mid = 0 then do;
    folder = catx ('/', folder, filename);

    link push;
    goto iterate;
  end;

* add FINFO() calls here to obtain file size and mod date;

  mid = fclose(mid);
  output;

  goto iterate;

%*-------------------------------------------------------;
pop:
%*-------------------------------------------------------;
  did = dclose(did);
  rc = filename (fref);

  state.remove();

  depth = depth - 1;
  if depth = 0 then stop;

  state.find();  

  return;

%*-------------------------------------------------------;
fref:
%*-------------------------------------------------------;
  call missing (fref);
  rc = filename (fref, folder);

  if rc ne 0 then do;
    msg = sysmsg();
    put msg;
    stop;
  end;
  return;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Feb 2023 17:30:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857260#M338733</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2023-02-05T17:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Recursive find files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857261#M338734</link>
      <description>&lt;P&gt;If your subtree is too large to load into a hash object you can use the MODIFY statement to affect a recursive search.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/dirtree.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/dirtree.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Feb 2023 17:41:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857261#M338734</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-05T17:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: Recursive find files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857277#M338748</link>
      <description>The only issue I see with the recursion is if the number of allowed open&lt;BR /&gt;filerefs is exceeded. There is 1 fileref open per depth reached.  No way&lt;BR /&gt;the hash will run out of resources.&lt;BR /&gt;</description>
      <pubDate>Sun, 05 Feb 2023 19:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-find-files/m-p/857277#M338748</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2023-02-05T19:04:58Z</dc:date>
    </item>
  </channel>
</rss>

