<?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 listing all files within a directory and subdirectories in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332432#M74822</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am studying the supprt SAS example&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n0js70lrkxo6uvn1fl4a5aafnlgt.htm" target="_self"&gt;List All Files within a Directory Including Subdirectories&lt;/A&gt;&amp;nbsp;and this is a macro that can not only list all files of a certain type within a directory, but also within the subdirectories of that directory.&lt;/P&gt;
&lt;P&gt;I was trying to create a table that would include all these fies, but couldn't - I tried placing data; ... run; in different places but was getting an error all the time. I wll be very grateful for your help, especially that I feel that I might have missed some importnat part of which I am not even aware.&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Feb 2017 00:03:39 GMT</pubDate>
    <dc:creator>ilikesas</dc:creator>
    <dc:date>2017-02-14T00:03:39Z</dc:date>
    <item>
      <title>listing all files within a directory and subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332432#M74822</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am studying the supprt SAS example&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n0js70lrkxo6uvn1fl4a5aafnlgt.htm" target="_self"&gt;List All Files within a Directory Including Subdirectories&lt;/A&gt;&amp;nbsp;and this is a macro that can not only list all files of a certain type within a directory, but also within the subdirectories of that directory.&lt;/P&gt;
&lt;P&gt;I was trying to create a table that would include all these fies, but couldn't - I tried placing data; ... run; in different places but was getting an error all the time. I wll be very grateful for your help, especially that I feel that I might have missed some importnat part of which I am not even aware.&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 00:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332432#M74822</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-02-14T00:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: listing all files within a directory and subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332433#M74823</link>
      <description>&lt;P&gt;The code below should give you a start.&lt;/P&gt;
&lt;PRE&gt;%macro list_files(dir,ext);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
  %let did=%sysfunc(dopen(&amp;amp;filrf));      

   %if &amp;amp;did eq 0 %then %do; 
    %put Directory &amp;amp;dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));   

   %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));

      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
        %put &amp;amp;dir\&amp;amp;name;

&lt;FONT color="#800000"&gt;        data _tmp;
          length dir $512 name $100;
          dir=symget("dir");
          name=symget("name");
        run;
        proc append base=want data=_tmp;
        run;quit;&lt;/FONT&gt;

      %end;
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;        
        %list_files(&amp;amp;dir\&amp;amp;name,&amp;amp;ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&amp;amp;did));
   %let rc=%sysfunc(filename(filrf));     

%mend list_files;
%list_files(c:\temp,sas)&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2017 00:15:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332433#M74823</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-02-14T00:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: listing all files within a directory and subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332434#M74824</link>
      <description>&lt;P&gt;You just need to replace one line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro list_files(dir,ext);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
  %let did=%sysfunc(dopen(&amp;amp;filrf));      

   %if &amp;amp;did eq 0 %then %do; 
    %put Directory &amp;amp;dir cannot be open or does not exist;
    %return;
  %end;

   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));   

   %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));

      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
        &lt;FONT color="#FF6600"&gt;FILE="&amp;amp;dir\&amp;amp;name";  output;&lt;/FONT&gt;
      %end;
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;        
        %list_files(&amp;amp;dir\&amp;amp;name,&amp;amp;ext)
      %end;

   %end;
   %let rc=%sysfunc(dclose(&amp;amp;did));
   %let rc=%sysfunc(filename(filrf));     

%mend list_files;

data WANT;
  length FILE $200;
  %list_files(c:\temp,sas)
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 08:36:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332434#M74824</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-03-26T08:36:52Z</dc:date>
    </item>
    <item>
      <title>Re: listing all files within a directory and subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332616#M74887</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* T008390 Ingenious code to recursively list all directories, sub-directories and files into a SAS datasets

Here is what is going on

  1. Build a template to hold the root, path and dir(depth)
  2. Modifily that datsets as new objects are found
  3. Use SAS dir commands to traverse directories

data template;
   length root path $200 dir 8;
   call missing(path,dir);
   input root;
 cards4;
D:\haven
D:\tym
;;;;
run;quit;


 data template;
   modify template;
   rc=filename('tmp',catx('/',root,path));
   dir=dopen('tmp');
   replace;
   if dir;
   path0=path;
   do _N_=1 to dnum(dir);
     path=catx('/',path0,dread(dir,_N_));
     output;
     end;
   rc=dclose(dir);
 run;

proc print data=template;
run;quit;

Obs      ROOT      PATH                             DIR

  1    D:\haven                                      1
  2    D:\tym                                        1
  3    D:\haven    old                               1
  4    D:\haven    pdf                               1
  5    D:\haven    r.sas7bdat                        0
  6    D:\haven    wps.sas7bdat                      0
  7    D:\tym      mta_incidence.sas7bdat            0
  8    D:\tym      pdf                               1
  9    D:\tym      tym_addavg.sas7bdat               0
 10    D:\tym      tym_asma_nit.sas7bdat             0
 11    D:\tym      tym_asma_wek.sas7bdat             0
 12    D:\tym      tym_estimate.sas7bdat             0
 13    D:\tym      tym_percent.sas7bdat              0
 14    D:\tym      tym_series_addmod.sas7bdat        0
 15    D:\tym      tym_series_addmon.sas7bdat        0
 16    D:\tym      tym_series_season.sas7bdat        0
 17    D:\tym      tym_series_series.sas7bdat        0
 18    D:\tym      tym_series_twoser.sas7bdat        0
 19    D:\tym      tym_series_twoser001.sas7bdat     0
 20    D:\tym      tym_series_twoser001.zip          0
 21    D:\haven    old/r.sas7bdat                    0
 22    D:\haven    old/testdta - Copy.sas7bdat       0
 23    D:\haven    old/testdta.dta                   0
 24    D:\haven    old/testr.sas7bdatx               0
 25    D:\haven    old/testwps.sas7bdat              0
 26    D:\haven    old/txzips.sas7bdatx              0
 27    D:\haven    old/wps.sas7bdat                  0
 28    D:\haven    old/wps.sas7bdatz                 0
 29    D:\haven    old/wpsx.sas7bdat                 0
 30    D:\haven    pdf/old                           1
 31    D:\haven    pdf/sas7bdat.pdf                  0
 32    D:\tym      pdf/tym_series_slyser.pdf         0
 33    D:\haven    pdf/old/testdircode.txt           0




&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2017 14:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332616#M74887</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-14T14:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: listing all files within a directory and subdirectories</title>
      <link>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/460665#M117116</link>
      <description>&lt;P&gt;Hi Patrick,&lt;/P&gt;&lt;P&gt;This is a great macro. I am working within an environment where I don't have access to any Unix/DOS shell commands so I am totally reliant on SAS code for everything.&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 11:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/460665#M117116</guid>
      <dc:creator>bravo433</dc:creator>
      <dc:date>2018-05-08T11:28:16Z</dc:date>
    </item>
  </channel>
</rss>

