<?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: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624635#M20141</link>
    <description>&lt;P&gt;A couple of comments. You almost never want quotes around the macro variable value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, you want to use SAS date functions and formats to accomplish this, such as INTNX.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Now, I have to admit, I'm not entirely clear on why you have date = '2020-01-10' and then you talk about February. I'm also not clear, as your have a SET statement that include &amp;amp;date, do you really have separate SAS data sets for every single day and you have to create new separate data set for every single day? If so, this isn't a particularly good way to organize the data, in fact I would call it bluntly a terrible blunder to organize this way, but maybe you're stuck with it — I hope not. You'd be better off creating one large data set for the month or for the year, rather than data sets for each and every day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, here goes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Somehow the user provides a date, and we loop over all dates in the month */
%let date=2020-01-10;
%let date1=%sysfunc(inputn(&amp;amp;date,yymmdd10.));

%macro dothis;
    %do day=%sysfunc(intnx(month,&amp;amp;date1,0,b)) %to
         %sysfunc(intnx(month,&amp;amp;date1,0,e)) %by 1;
		%let day1=%sysfunc(putn(&amp;amp;day,date7.));
		data example_&amp;amp;day1;
		    set balance_&amp;amp;day1;
			if prod_bal=. then delete;
		run;
	%end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;&lt;FONT size="4"&gt;But, really, don't do this, take my advice above and put everything into one large data set, your programming thereafter will be much easier.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Feb 2020 20:28:31 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-02-13T20:28:31Z</dc:date>
    <item>
      <title>Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624627#M20137</link>
      <description>&lt;P&gt;Hello Beautiful People!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope all is well.&amp;nbsp; I'm trying to automate a very manual process as follows:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;There is a %LET macro variable that references a date ---&amp;gt; %LET date = '2020-01-10';&lt;/LI&gt;
&lt;LI&gt;There is then a data step that follows that generates basic data for that date.&amp;nbsp;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Once the data step runs, change the date variable for the next day and rerun the process.&amp;nbsp; Repeat this for every day of the month.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;An example of the above process is below, where I would change the macro variable after this process runs for the next day and repeat until I capture all of the days of the month.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date =20200110;
Data example; set balance_&amp;amp;date.;run;
/*Rerun the above step for every day of the month because busy work.*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How would I translate this into a macro that encompasses a %DO loop to capture every day of the month?&amp;nbsp; For example: If it's February 2020, it would capture all 29 days.&amp;nbsp; If it's December, it would capture all 31 days.&amp;nbsp; Etc.&amp;nbsp; Any guidance on this is greatly appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:23:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624627#M20137</guid>
      <dc:creator>davidvalentine</dc:creator>
      <dc:date>2020-02-13T20:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624632#M20139</link>
      <description>&lt;P&gt;That code would not work for starters - the quotes and dashes would cause issues so showing actual code would help. &lt;BR /&gt;&lt;BR /&gt;Second, you can shortcut reference data sets if they actually have a naming structure:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set balance_2020_02: ;
*the colon will use all data sets that start with balance_2020_02;
if prod_bal = . then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage - this has some examples that may be useful to you.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624632#M20139</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-13T20:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624634#M20140</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I fixed the SAS example code to present a near-identical copy.&amp;nbsp; In the meantime, I'm not sure I follow your suggestion...&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624634#M20140</guid>
      <dc:creator>davidvalentine</dc:creator>
      <dc:date>2020-02-13T20:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624635#M20141</link>
      <description>&lt;P&gt;A couple of comments. You almost never want quotes around the macro variable value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, you want to use SAS date functions and formats to accomplish this, such as INTNX.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Now, I have to admit, I'm not entirely clear on why you have date = '2020-01-10' and then you talk about February. I'm also not clear, as your have a SET statement that include &amp;amp;date, do you really have separate SAS data sets for every single day and you have to create new separate data set for every single day? If so, this isn't a particularly good way to organize the data, in fact I would call it bluntly a terrible blunder to organize this way, but maybe you're stuck with it — I hope not. You'd be better off creating one large data set for the month or for the year, rather than data sets for each and every day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, here goes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Somehow the user provides a date, and we loop over all dates in the month */
%let date=2020-01-10;
%let date1=%sysfunc(inputn(&amp;amp;date,yymmdd10.));

%macro dothis;
    %do day=%sysfunc(intnx(month,&amp;amp;date1,0,b)) %to
         %sysfunc(intnx(month,&amp;amp;date1,0,e)) %by 1;
		%let day1=%sysfunc(putn(&amp;amp;day,date7.));
		data example_&amp;amp;day1;
		    set balance_&amp;amp;day1;
			if prod_bal=. then delete;
		run;
	%end;
%mend;
%dothis&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;&lt;FONT size="4"&gt;But, really, don't do this, take my advice above and put everything into one large data set, your programming thereafter will be much easier.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624635#M20141</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-13T20:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624641#M20143</link>
      <description>&lt;P&gt;Hi PaigeMiller,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks so much for your guidance on this.&amp;nbsp; This is one of those situations where I'm stuck with what was given to me from above.&amp;nbsp; And they want it this way.&amp;nbsp; But you gave me plenty of wiggle room with your suggestions and I very much appreciate your help.&amp;nbsp; Thanks again!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Valentine&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624641#M20143</guid>
      <dc:creator>davidvalentine</dc:creator>
      <dc:date>2020-02-13T20:36:48Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624643#M20144</link>
      <description>Did you try it by running the code? It'll pull all data at once into a single data set which is the best approach. &lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set balance_2020_02: ;&lt;BR /&gt;*the colon will use all data sets that start with balance_202002: ;&lt;BR /&gt;if prod_bal = . then delete;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624643#M20144</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-13T20:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624646#M20146</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/295099"&gt;@davidvalentine&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks so much for your guidance on this.&amp;nbsp; This is one of those situations where I'm stuck with what was given to me from above.&amp;nbsp; And they want it this way.&amp;nbsp; But you gave me plenty of wiggle room with your suggestions and I very much appreciate your help.&amp;nbsp; Thanks again!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Tell the people you work with that you will be able to write code much more efficiently if everything is in a single data set, thereby saving time and money. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;shows how you can do that.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624646#M20146</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-13T20:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Programming: How to Use a %Do Loop in a Macro to Create Datasets for Each Day of a Month</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624651#M20148</link>
      <description>&lt;P&gt;Do you need to loop or just read all of the datasets in one step?&lt;/P&gt;
&lt;P&gt;So lets get a string that looks like a date in YYYYMMDD style.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date=20200210 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then to get year and month we just need the first 6 characters. No need to for any knowledge of actual dates.&amp;nbsp; So you can use the colon wildcard to tell SAS to read all datasets that start with BALANCE_202002.&amp;nbsp; You might need include a %UNQUOTE() function call to make sure the parse doesn't treat %substr() function call as breaking the dataset name into three tokens instead of one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example; 
  set %unquote(balance_%scan(&amp;amp;date,1,6):) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Feb 2020 20:57:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-Programming-How-to-Use-a-Do-Loop-in-a-Macro-to-Create/m-p/624651#M20148</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-13T20:57:08Z</dc:date>
    </item>
  </channel>
</rss>

