<?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: Saving the files found using the list_files macro into a global macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681679#M206213</link>
    <description>&lt;P&gt;I have a somewhat different macro that saves the results in a SAS data set. From there, you can do whatever you want with the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro list_files(dir=,outdsn=files,all_subdirs=N);
data _null_;
	if upcase("&amp;amp;all_subdirs")='N' then call symputx('str',cat("'dir ",'"',"%trim(&amp;amp;dir)"" /b'"));
	else if upcase("&amp;amp;all_subdirs")='Y' then call symputx('str',cat("'dir ",'"',"%trim(&amp;amp;dir)"" /b /s'"));
run;
filename tmp pipe &amp;amp;str;
data &amp;amp;outdsn;
  infile tmp dlm="¬" missover;
  input filenames :$200.;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;(Works only on Windows)&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Sep 2020 16:10:59 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-09-04T16:10:59Z</dc:date>
    <item>
      <title>Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681677#M206212</link>
      <description>&lt;P&gt;I need to find all the SAS files in all the sub-directories in a drive and save them into a macro variable that I can use outside the macro. I found this example below that prints the names of all the files of said directory into the log. I know that it saves them locally into &amp;amp;name, but I need to use name outside this macro. I tried changing %local to %global and the macro ran forever. I also tried saving name into another variable within the macro but also failed. I'd much rather save these file names into a table, but a macro variable will also work. Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using the following macro that I found here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is it below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&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;
%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;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Sep 2020 16:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681677#M206212</guid>
      <dc:creator>serrld113</dc:creator>
      <dc:date>2020-09-04T16:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681679#M206213</link>
      <description>&lt;P&gt;I have a somewhat different macro that saves the results in a SAS data set. From there, you can do whatever you want with the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro list_files(dir=,outdsn=files,all_subdirs=N);
data _null_;
	if upcase("&amp;amp;all_subdirs")='N' then call symputx('str',cat("'dir ",'"',"%trim(&amp;amp;dir)"" /b'"));
	else if upcase("&amp;amp;all_subdirs")='Y' then call symputx('str',cat("'dir ",'"',"%trim(&amp;amp;dir)"" /b /s'"));
run;
filename tmp pipe &amp;amp;str;
data &amp;amp;outdsn;
  infile tmp dlm="¬" missover;
  input filenames :$200.;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;(Works only on Windows)&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 16:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681679#M206213</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-04T16:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681687#M206218</link>
      <description>&lt;P&gt;I don't know about your system but I have some project that if the paths are kept in the values with the filename then the length of single macro variable would be exceeded.&lt;/P&gt;
&lt;P&gt;Plus are your paths and file names really clean, like no spaces or odd characters in the folder or file names?&lt;/P&gt;
&lt;P&gt;If not, then you might have some fun using such an ugly variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anything that can be done with macro variable and much more can be done with values in a data set. I would, for any purpose I can envision using such, go with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s suggestion.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 16:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681687#M206218</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-04T16:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681689#M206219</link>
      <description>I added that I prefer a data set over a macro variable.</description>
      <pubDate>Fri, 04 Sep 2020 16:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681689#M206219</guid>
      <dc:creator>serrld113</dc:creator>
      <dc:date>2020-09-04T16:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681691#M206220</link>
      <description>I really appreciate this, but it didn't output a dataset for me. I added my directory path and changed all_subdirs = "Y"</description>
      <pubDate>Fri, 04 Sep 2020 16:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681691#M206220</guid>
      <dc:creator>serrld113</dc:creator>
      <dc:date>2020-09-04T16:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681699#M206225</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157069"&gt;@serrld113&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I really appreciate this, but it didn't output a dataset for me. I added my directory path and changed all_subdirs = "Y"&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Works for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: for your own benefit, as well as the benefit of others, it is never sufficient to say something didn't work&amp;nbsp;&lt;EM&gt;and provide no additional information&lt;/EM&gt;. If it didn't work, give us details about what you did and what happened. Otherwise, the conversation goes "didn't work" "works for me" "doesn't work for me" "works fine for me"&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 17:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681699#M206225</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-04T17:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681703#M206229</link>
      <description>I apologize. In my reply I included that I've added my directory as well as changed the option for all_subdirs to Y. I've also tried my directory between quotation marks and without. Thanks again.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 04 Sep 2020 17:32:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681703#M206229</guid>
      <dc:creator>serrld113</dc:creator>
      <dc:date>2020-09-04T17:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681704#M206230</link>
      <description>&lt;P&gt;The one thing that kinda works is adding a data step after the %put statement in my original post. However, it will only output the last SAS file the loop finds. I was wondering if there was a dynamic way to keep adding to the variable as the loop runs.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;
			%put &amp;amp;dir\&amp;amp;name;
&lt;BR /&gt;/* this is the part I add */
				data files;
			  		filenames = "&amp;amp;dir\&amp;amp;name";
				run;

		%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Sep 2020 17:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681704#M206230</guid>
      <dc:creator>serrld113</dc:creator>
      <dc:date>2020-09-04T17:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681707#M206233</link>
      <description>&lt;P&gt;We need to see your code, or the log, or both. That's the only way to figure out what happens when code doesn't work.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 17:38:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681707#M206233</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-04T17:38:46Z</dc:date>
    </item>
    <item>
      <title>Re: Saving the files found using the list_files macro into a global macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681781#M206273</link>
      <description>&lt;P&gt;Don't put the file names into macro variables. Put them into a dataset.&amp;nbsp; A macro variable has a maxium length of 64K.&amp;nbsp; Even a moderate number of names could easily overflow that limit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a great idea for doing the recursion into the subdirectories by using the MODIFY statement in a data step posted in this thread:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332616/highlight/true#M74887" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332616/highlight/true#M74887&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example.&amp;nbsp; First create a dataset with the structure you want and one observation for each top level directory you want to read.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist;
  length root filename $256 dir level 8;
  input root;
  retain filename ' ' level 0 dir 1;
cards4;
c:\temp
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now run this data step to scan all of the files in the directory(s).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist;
  modify filelist;
  rc1=filename('tmp',catx('\',root,filename));
  rc2=dopen('tmp');
  dir = not not rc2;
  replace;
  if dir;
  level=level+1;
  filename0=filename;
  do i=1 to dnum(rc2);
    filename=catx('\',filename0,dread(rc2,i));
    output;
  end;
  rc3=dclose(rc2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to limit to files with a particular extension it is probably easier after you have the dataset already.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=filelist;
  where filename like '%.sas7bdat';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs     root      filename               dir    level

 4     c:\temp    one.sas7bdat            0       1
 6     c:\temp    two.sas7bdat            0       1
 7     c:\temp    sub1\three.sas7bdat     0       2
&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Sep 2020 03:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-the-files-found-using-the-list-files-macro-into-a-global/m-p/681781#M206273</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-05T03:59:57Z</dc:date>
    </item>
  </channel>
</rss>

