<?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: SAS Macro Keeps on Running in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571105#M161085</link>
    <description>&lt;P&gt;Move the DT1 increment after the test block.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jul 2019 21:45:08 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-07-03T21:45:08Z</dc:date>
    <item>
      <title>SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571098#M161079</link>
      <description>&lt;P&gt;Hi.. my following macro keeps on running and won't stop.. Can anyone spot any mistake there?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my macro, I am first checking if the file&amp;nbsp;lib.dataset_&amp;amp;dt2. exists or not. If it does then append the file name to a list. If not, then skip the date dt2, and move on to the next date and next file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At the end, my macro variable &lt;CODE class=" language-sas"&gt;&amp;amp;fileList1 should have all the existing files. For example,&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;%put&amp;nbsp;&amp;amp;fileList1.;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;(lib.dataset_20190501&amp;nbsp;lib.dataset_20190524 lib.dataset_20190602&amp;nbsp;lib.dataset_20190603 .....)&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rsubmit;
%macro code(d1=, d2=);
%let start_date = &amp;amp;d1.;
%let end_date = &amp;amp;d2.;
%let dt1 = &amp;amp;start_date.;
%do %while (%sysevalf("&amp;amp;dt1."d &amp;lt;= "&amp;amp;end_date."d));
	%let dt1 = %sysfunc(putn("&amp;amp;dt1."d, date9.));
	%let dt2 = &amp;amp;dt1.;
	%let dt2 = %sysfunc(putn("&amp;amp;dt1."d, yymmddn8.));
	%if %sysfunc(exist(lib.dataset_&amp;amp;dt2.)) = 1 %then %do;
		%let fileList1 = %str();
		%let thefile1 = lib.dataset_&amp;amp;dt2.; 
		%let fileList1 = %str(&amp;amp;fileList1 &amp;amp;thefile1); 
		%let dt1 = %sysfunc(intnx(day, "&amp;amp;dt1."d, 1, s), date9.);
	%end;
%end;
%put &amp;amp;fileList1.;

%mend;
endrsubmit;

rsubmit;
%code(d1 = '01MAY2019', d2 = '02JUL2019');
endrsubmit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jul 2019 21:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571098#M161079</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-03T21:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571099#M161080</link>
      <description>What are you planning to use this for? &lt;BR /&gt;If the data doesn't exist you never increment the dates so it never exits.</description>
      <pubDate>Wed, 03 Jul 2019 21:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571099#M161080</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T21:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571101#M161081</link>
      <description>&lt;P&gt;if the dataset does not exist, won't the code skips it and move on to the next date? since I have the if then do statement..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to compile all the existing file name onto a macro variable, so later on I can call it in a data-step to append all those files together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 21:33:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571101#M161081</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-03T21:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571102#M161082</link>
      <description>No, because you have a DO UNTIL loop which doesn't have any default increment. You know that shortcut lists exist then?Many of these work for data set names as well. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;So you can have hte first and last date and it will include everything in between:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set lib.mydata201801:; *all for January 2018;&lt;BR /&gt;set lib.myData201801 - lib.myData201812;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 03 Jul 2019 21:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571102#M161082</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T21:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571103#M161083</link>
      <description>&lt;P&gt;The way your macro is written you should not have quotes on your macro parameters otherwise your dates wont resolve correctly. Also it would be good programming practice to set an upper limit on the number of DO loops so you never end up in this situation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%code(d1 = '01MAY2019', d2 = '02JUL2019'); * You have this - Dont need quotes;
%code(d1 = 01MAY2019, d2 = 02JUL2019); * Try this;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jul 2019 21:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571103#M161083</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-07-03T21:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571104#M161084</link>
      <description>And you can just pull the names from SASHELP.VTABLE instead of manually compiling them as well.</description>
      <pubDate>Wed, 03 Jul 2019 21:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571104#M161084</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T21:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571105#M161085</link>
      <description>&lt;P&gt;Move the DT1 increment after the test block.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 21:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571105#M161085</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-07-03T21:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571114#M161087</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211631"&gt;@newboy1218&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi.. my following macro keeps on running and won't stop.. Can anyone spot any mistake there?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my macro, I am first checking if the file&amp;nbsp;lib.dataset_&amp;amp;dt2. exists or not. If it does then append the file name to a list. If not, then skip the date dt2, and move on to the next date and next file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end, my macro variable &lt;CODE class=" language-sas"&gt;&amp;amp;fileList1 should have all the existing files. For example,&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;%put&amp;nbsp;&amp;amp;fileList1.;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;(lib.dataset_20190501&amp;nbsp;lib.dataset_20190524 lib.dataset_20190602&amp;nbsp;lib.dataset_20190603 .....)&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rsubmit;
%macro code(d1=, d2=);
%let start_date = &amp;amp;d1.;
%let end_date = &amp;amp;d2.;
%let dt1 = &amp;amp;start_date.;
%do %while (%sysevalf("&amp;amp;dt1."d &amp;lt;= "&amp;amp;end_date."d));
	%let dt1 = %sysfunc(putn("&amp;amp;dt1."d, date9.));
	%let dt2 = &amp;amp;dt1.;
	%let dt2 = %sysfunc(putn("&amp;amp;dt1."d, yymmddn8.));
	%if %sysfunc(exist(lib.dataset_&amp;amp;dt2.)) = 1 %then %do;
		%let fileList1 = %str();
		%let thefile1 = lib.dataset_&amp;amp;dt2.; 
		%let fileList1 = %str(&amp;amp;fileList1 &amp;amp;thefile1); 
		%let dt1 = %sysfunc(intnx(day, "&amp;amp;dt1."d, 1, s), date9.);
	%end;
%end;
%put &amp;amp;fileList1.;

%mend;
endrsubmit;

rsubmit;
%code(d1 = '01MAY2019', d2 = '02JUL2019');
endrsubmit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macros and macro variables are just text.&amp;nbsp; Yes, we have the %eval and %sysevalf functions, but this line "worries" me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;%do %while (%sysevalf("&amp;amp;dt1."d &amp;lt;= "&amp;amp;end_date."d));&lt;/CODE&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Why make this more complicated than it needs to be?&amp;nbsp; If it were me, I'd code it this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_dates(start,end);
	data _null_;
		length buffer $32767;  %* adjust as required, but the max length does not really hurt performance ; ;
		do date="&amp;amp;start"d to "&amp;amp;end"d;
			buffer=catx(" ",buffer,put(date,yymmddn8.));
		end;
		call symputx("dates",buffer,"G");
	run;
%mend;
%get_dates(01MAY2019,02JUL2019);
%put &amp;amp;=dates;
&lt;BR /&gt;%* this is the block of code you need to repeat for each token in the list ;
%macro code;
	%let dataset=lib.dataset_&amp;amp;word;
	%put &amp;amp;=dataset;
%mend;	

%loop(&amp;amp;dates);

* if you want to wrap this in one uber-macro: ;

%macro my_uber_macro(start,end);
	%* create a list of dates ;
	data _null_;
		length buffer $32767;  %* adjust as required, but the max length does not really hurt performance ; ;
		do date="&amp;amp;start"d to "&amp;amp;end"d;
			buffer=catx(" ",buffer,put(date,yymmddn8.));
		end;
		call symputx("dates",buffer,"G");
	run;

	%* run a block of code over those dates ;
	%let i=1;
	%let word=%scan(&amp;amp;dates,&amp;amp;i,%str( ));
	%do %while (&amp;amp;word ne );

		%* this is the bit of code you need to repeat for each token ;
		%let dataset=lib.dataset_&amp;amp;word;
		%put &amp;amp;=dataset;
		%* end of the bit of code you need to repeat ;

		%let i=%eval(&amp;amp;i+1);
		%let word=%scan(&amp;amp;dates,&amp;amp;i,%str( ));
	%end;
%mend;

%my_uber_macro(01MAY2019,02JUL2019);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See&amp;nbsp;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop.sas" target="_blank" rel="noopener"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;for the loop macro.&amp;nbsp; Save it to your autocall macro library.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't want to use that macro, then just use the concepts in that macro to parse each token in the &amp;amp;dates list, and call a child macro or block of code for each word in the list.&amp;nbsp; Put the block of code you need to repeat in that child macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll have to use a different approach if your list of dates won't fit into a macro variable (64K).&amp;nbsp; Well, actually, the 32K limit of the buffer variable in the data step.&amp;nbsp; However, I rarely encounter this limit in my day-to-day work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(if you did encounter that limit, see&amp;nbsp;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 23:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571114#M161087</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-07-03T23:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571119#M161089</link>
      <description>&lt;P&gt;You are making this way too complicated. Date are just INTEGERS, in particular the number of days since 1960. Just use a normal %DO loop.&amp;nbsp; You will need to use %SYSEVALF() to enable the macro processor to understand how to handle date literals because the implied call to %EVAL() doesn't understand them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro code(d1=, d2=);
%local date dsn;
%if not %symexist(filelist1) %then %global filelist1;
%do date=%sysevalf(&amp;amp;d1) %to %sysevalf(&amp;amp;d2) ;
  %let dsn=lib.dataset_%sysfunc(putn(&amp;amp;date,yymmddn8));
  %if %sysfunc(exist(&amp;amp;dsn)) %then %let filelist1=&amp;amp;filelist1 &amp;amp;dsn;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call it with actual date values, not strings. Either data literals&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%code(d1 = '01MAY2019'd, d2 = '02JUL2019'd);
%put &amp;amp;=filelist1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or just the raw number of days.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%code(d1=21670,d2=21732);
%put &amp;amp;=filelist1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2019 00:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571119#M161089</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-04T00:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571136#M161098</link>
      <description>&lt;P&gt;Are you working on a submission to the Obfuscated SAS Code Contest? With this, you might get an honourable mention.&lt;/P&gt;
&lt;P&gt;You are using lots of data step functions, so you should let them loose in their natural habitat, which is the data step, and only wrap the macro definition around it for easy reuse:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_filelist(lib=,prefix=,d1=,d2=);
data _null_;
length filelist $32767;
do dt2 = &amp;amp;d1. to &amp;amp;d2.;
  dsname = "&amp;amp;lib..&amp;amp;prefix." !! put(dt2,yymmddn8.);
  if exist(dsname)
  then filelist = catx(' ',filelist,dsname);
end;
call symputx('filelist',filelist,'g');
run;
%mend;
%make_filelist(lib=lib,prefix=dataset_,d1='01may2019'd,d2='02jul2019'd)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can, as already mentioned, use raw date values when calling the macro.&lt;/P&gt;
&lt;P&gt;Always strive for the most simple and straightforward solution. Which usually means macro code is not needed.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2019 05:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571136#M161098</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-04T05:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Keeps on Running</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571138#M161099</link>
      <description>&lt;P&gt;PS and if you need to repeat a certain action for those datasets, change the data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data control;
do dt2 = &amp;amp;d1. to &amp;amp;d2.;
  dsname = "&amp;amp;lib..&amp;amp;prefix." !! put(dt2,yymmddn8.);
  if exist(dsname) then output;
end;
keep dsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you have a dataset from which you can dynamically run this action with call execute. And you can't run into any limits for character or macro variables.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Jul 2019 05:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Keeps-on-Running/m-p/571138#M161099</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-04T05:33:07Z</dc:date>
    </item>
  </channel>
</rss>

