<?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: choose all file name for macro variable by using loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873143#M344988</link>
    <description>&lt;P&gt;Getting an error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: File WORK.LIST.DATA does not exist.&lt;/P&gt;</description>
    <pubDate>Mon, 01 May 2023 11:55:02 GMT</pubDate>
    <dc:creator>dht115</dc:creator>
    <dc:date>2023-05-01T11:55:02Z</dc:date>
    <item>
      <title>choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/872685#M344768</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am running a SAS macro to sort my dataset. I do have multiple folders and each folder has different number of datasets. Is there a way for us to modify SAS macro which can go to specific folder and bring all sas file name as macro parameter. So I just need to change library name and run the macro instead of changing sas dataset name for macro variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;For&amp;nbsp;scenario 1, I do have five (5) sas files in folder so I need to run my macro five times.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;Whereas, in scenario 2, I do have three (3) sas files in folder - and I do need to run my macro only three times.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Scenario 1:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dht115_0-1682628118181.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83349i1340F0C6F17C75E8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dht115_0-1682628118181.png" alt="dht115_0-1682628118181.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dht115_2-1682628299593.png" style="width: 279px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83351i381D58D1F24F1901/image-dimensions/279x146?v=v2" width="279" height="146" role="button" title="dht115_2-1682628299593.png" alt="dht115_2-1682628299593.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Scenario 2:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dht115_1-1682628153333.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83350i1425B2971A4B4B6F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dht115_1-1682628153333.png" alt="dht115_1-1682628153333.png" /&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dht115_3-1682628315963.png" style="width: 316px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83352i7F46ABEBC9CE4281/image-dimensions/316x124?v=v2" width="316" height="124" role="button" title="dht115_3-1682628315963.png" alt="dht115_3-1682628315963.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;libname abc 'C:\Users\XXX\Desktop\platform\one'; 
%macro test (var);
proc sort data=&amp;amp;var. out=new_sort_&amp;amp;var.;
by item;
run;
%mend;

%test(test1);
%test(test2);
%test(test3);
%test(test4);
%test(test5);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 20:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/872685#M344768</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2023-04-27T20:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/872689#M344771</link>
      <description>&lt;P&gt;Use DICTIONARY.TABLES to import all dataset names from the directory, then load them into a macro variable. In the macro definition use loop to sort all datasets dynamically without physically referring to a dataset name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Eg:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sort(dir=);
	proc sql noprint;
		select distinct memname
			into: list separated by ' ' 
				from dictionary.tables
			where libname eq "%upcase(&amp;amp;dir)";
		%put &amp;amp;=list;
	quit;
	%do i=1 %to %sysfunc(countw(&amp;amp;list));
		%let var= %scan(&amp;amp;list, &amp;amp;i);
		proc sort data=&amp;amp;var. out=new_sort_&amp;amp;var.;
			by item;
		run;
	%end;
%mend;

%sort(dir= abc);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Apr 2023 21:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/872689#M344771</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-04-27T21:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873143#M344988</link>
      <description>&lt;P&gt;Getting an error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: File WORK.LIST.DATA does not exist.&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 11:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873143#M344988</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2023-05-01T11:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873146#M344989</link>
      <description>&lt;P&gt;Whenever you get an error in the log, show us the ENTIRE log for this code. Do not show us partial logs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since this involves macros, please turn on the macro debugging option by running the code below and then run your macro again and show us the ENTIRE log. Do not show us partial logs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 May 2023 11:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873146#M344989</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-01T11:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873148#M344991</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Getting an error:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: File WORK.LIST.DATA does not exist.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The most likely way that could happen would be if you forgot an &amp;amp; in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var= %scan(&amp;amp;list, &amp;amp;i);  %* &amp;lt;-- check there is an &amp;amp; in front of list ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another possibility is that&amp;nbsp; you didn't create the libref ABC prior to running the macro.&amp;nbsp; In that case you will see in the log that the select statement returns 0 obs and it does not create the macro variable LIST.&amp;nbsp; So you would have unresolved macro variable reference message in the log as well.&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 12:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873148#M344991</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-05-01T12:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873151#M344993</link>
      <description>&lt;P&gt;Try like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options dlcreatedir;
libname t "R:\test1";

data t.a t.b t.c t.d t.e;
  set sashelp.class;
run;

libname t "R:\test2";

data t.a t.b t.c t.d t.e;
  set sashelp.class;
run;

libname t "R:\test3";

data t.a t.b t.c t.d t.e;
  set sashelp.class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sorting for multiple directories:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sort(lib,by);
  libname t "&amp;amp;lib.";
  proc contents data=t._ALL_ out=list noprint;
  run;
  proc sort data=list(keep=libname memname) nodupkeys;
  by libname memname;
  run;
 
  data _null_;
    set list;
    call execute(cats("proc sort data=",libname,".",memname,"; by &amp;amp;by.; run;"));
  run;
  libname t clear;
%mend sort;



data _null_;
input;
call execute('%sort(' !! _INFILE_ !! ',age)'); /* &amp;lt;----------- you can modify sorting variables here */
cards4;
R:\test1
R:\test2
R:\test3
;;;;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;different directories are fed through the CARDS4 to a data step which call macro execution for datasets in that directories.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 12:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873151#M344993</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-05-01T12:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: choose all file name for macro variable by using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873167#M344999</link>
      <description>&lt;P&gt;Log says: "WORK.LIST data does not exist".&amp;nbsp; Macro will read data from ABC library. You need to declare LIBNAME first, then use this lib as macro parameter.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Libname abc "C/myfolder/myfiles";&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 13:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/choose-all-file-name-for-macro-variable-by-using-loop/m-p/873167#M344999</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-05-01T13:36:41Z</dc:date>
    </item>
  </channel>
</rss>

