<?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: Compare current month to previous month in a do loop macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643728#M192163</link>
    <description>&lt;P&gt;When I run your macro, it loops 13 times through the first loop (&amp;amp;I going 0 to 12) and another 13 times through the second loop (&amp;amp;J going 0 to 12). So I'm not able to say why when you run it takes 4 hours.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;To help you further debug your own situation, you can print information to the log about each iteration, for example (partial code)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=0 %to %sysfunc(intck(month,&amp;amp;start,%sysfunc(inputn(&amp;amp;end,yymmn6))));
%put &amp;amp;=i;
%do j=0 %to %sysfunc(intck(month,&amp;amp;start1,%sysfunc(inputn(&amp;amp;end_1,yymmn6))));
%put &amp;amp;=j;
%let prvmth=%sysfunc(intnx(month,&amp;amp;start1,&amp;amp;j),yymmn6);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Lastly, you don't have to constantly format and unformat and format and unformat your variables. Make your life and your code simple: leave the macro variables as unformatted all the way through the code.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Apr 2020 21:31:08 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-04-28T21:31:08Z</dc:date>
    <item>
      <title>Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643699#M192152</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to compare accounts in the current month to those of the previous month (in order to determine, new accounts, increases and decreases) and I would like to do this for the last 13 months.&lt;/P&gt;&lt;P&gt;I attempted the following, although no errors came up in the log, it appears to have gotten stuck in the loop (code was running for about 4 hours).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas as to where I am going wrong?&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=" language-sas"&gt;%MACRO DATA(beg, end, beg_1, end_1);
%LOCAL start start1 i j prvmth dt;
%let start=%sysfunc(inputn(&amp;amp;beg,yymmn6));
%let start1=%sysfunc(inputn(&amp;amp;beg_1,yymmn6));
%do i=0 %to %sysfunc(intck(month,&amp;amp;start,%sysfunc(inputn(&amp;amp;end,yymmn6))));
%do j=0 %to %sysfunc(intck(month,&amp;amp;start1,%sysfunc(inputn(&amp;amp;end_1,yymmn6))));
%let prvmth=%sysfunc(intnx(month,&amp;amp;start1,&amp;amp;j),yymmn6);
%let dt=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i),yymmn6);

%END;
%END;
%MEND;


%let dt = %sysfunc( intnx( month, &amp;amp;todaysdate, -1, e), yymmn6. ); *TO CALC END OF 13MTH REPORTING PERIOD;
%put dt is &amp;amp;dt;

%let prevdt = %sysfunc( intnx( month, &amp;amp;todaysdate, -13, e), yymmn6. ); *TO CALC START OF 13MTH REPORTING PERIOD;
%put prevdt is &amp;amp;prevdt;

%let prvmth = %sysfunc( intnx( month, &amp;amp;todaysdate, -2, e), yymmn6. ); *TO CALC MTH PRIOR TO CURRENT MTH OF CURRENT YR;
%put prvmth is &amp;amp;prvmth;

%let prevyrmth = %sysfunc( intnx( month, &amp;amp;todaysdate, -14, e), yymmn6. ); *TO CALC MTH PRIOR TO START OF 13 MTH REPORTING;
%put prevyrmth is &amp;amp;prevyrmth;



%DATA(&amp;amp;PREVDT,&amp;amp;DT,&amp;amp;PREVYRMTH,&amp;amp;PRVMTH);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 20:38:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643699#M192152</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-28T20:38:11Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643709#M192154</link>
      <description>&lt;P&gt;It looks like the third argument of your call to the DATA macro should be &amp;amp;PREVYRMTH (but you have &amp;amp;PRVYRMTH).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 19:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643709#M192154</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-04-28T19:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643711#M192155</link>
      <description>&lt;P&gt;Let's start at the beginning ... what is the value of &amp;amp;todaysdate?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;=todaysdate;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Apr 2020 19:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643711#M192155</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-28T19:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643719#M192160</link>
      <description>Thanks! That was a typo, I've made the correction to the post.</description>
      <pubDate>Tue, 28 Apr 2020 20:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643719#M192160</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-28T20:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643720#M192161</link>
      <description>Hi. Today's date:&lt;BR /&gt;&lt;BR /&gt;%let todaysdate = %sysfunc( today() );&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I will add a %put line as I didn't do that.</description>
      <pubDate>Tue, 28 Apr 2020 20:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643720#M192161</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-28T20:41:12Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643728#M192163</link>
      <description>&lt;P&gt;When I run your macro, it loops 13 times through the first loop (&amp;amp;I going 0 to 12) and another 13 times through the second loop (&amp;amp;J going 0 to 12). So I'm not able to say why when you run it takes 4 hours.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;To help you further debug your own situation, you can print information to the log about each iteration, for example (partial code)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=0 %to %sysfunc(intck(month,&amp;amp;start,%sysfunc(inputn(&amp;amp;end,yymmn6))));
%put &amp;amp;=i;
%do j=0 %to %sysfunc(intck(month,&amp;amp;start1,%sysfunc(inputn(&amp;amp;end_1,yymmn6))));
%put &amp;amp;=j;
%let prvmth=%sysfunc(intnx(month,&amp;amp;start1,&amp;amp;j),yymmn6);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Lastly, you don't have to constantly format and unformat and format and unformat your variables. Make your life and your code simple: leave the macro variables as unformatted all the way through the code.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 21:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643728#M192163</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-28T21:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643729#M192164</link>
      <description>&lt;P&gt;If you want the %DO loop to count backwards you have to tell it that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %do i=0 %to %sysfunc(intck(month,&amp;amp;start,%sysfunc(inputn(&amp;amp;end,yymmn6)))) %by -1;
    %let dt=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i),yymmn6);
    %do j=0 %to %sysfunc(intck(month,&amp;amp;start1,%sysfunc(inputn(&amp;amp;end_1,yymmn6)))) %by -1;
    %let prvmth=%sysfunc(intnx(month,&amp;amp;start1,&amp;amp;j),yymmn6);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Apr 2020 21:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643729#M192164</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-28T21:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643759#M192174</link>
      <description>Thanks! I will make this change.</description>
      <pubDate>Wed, 29 Apr 2020 04:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643759#M192174</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-29T04:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643764#M192176</link>
      <description>Thanks, this produces an error :' The data set list ... does not contain any members'</description>
      <pubDate>Wed, 29 Apr 2020 05:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643764#M192176</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-29T05:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643767#M192179</link>
      <description>From the log, it appears that it produced what I wanted and then restarted the loop. I assume this is because of the 2 do statements. What I was trying to do is get the code to run once but with 2 different tables in the code having 2 different start and end dates. For instance, for March if I am trying to find the number of new accounts I want the program to look at the accounts in March and compare them to those in February, then do this for 13 months.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 29 Apr 2020 05:54:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643767#M192179</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-29T05:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643780#M192183</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324692"&gt;@Len18&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks, this produces an error :' The data set list ... does not contain any members'&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your macro as posted CAN NOT create this ERROR, as it does not contain any non-macro code necessary for this.&lt;/P&gt;
&lt;P&gt;If you want us to diagnose your code, you need to show it (and the log from it) as is.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 06:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643780#M192183</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-29T06:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643787#M192186</link>
      <description>I know posting the code will help but I am a little nervous about doing so. I did however, remove most of the code and used just 2 tables when calling the macro and you are definitely correct. The macro is not the issue. I will review the code bit by bit to better determine what the cause of the problem is and create a new post accordingly. Thanks a lot though, now I know I am looking at the wrong thing</description>
      <pubDate>Wed, 29 Apr 2020 07:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643787#M192186</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-29T07:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643812#M192190</link>
      <description>&lt;P&gt;Don't jump right into the middle and write a big macro with lots of steps and lots of macro code.&lt;/P&gt;
&lt;P&gt;Start with the Base SAS code that does what you want for a single, defined instance. Then replace the elements that define the instance with macro variables and retest. Only &lt;U&gt;then&lt;/U&gt; start to define those elements dynamically through macro code. That way you make sure that you don't have a mistake in the Base SAS code that is masked by all the macro processing around it. By working in small steps with tests you also catch a mistake as soon as it is introduced.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 07:53:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/643812#M192190</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-29T07:53:52Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644186#M192381</link>
      <description>Thanks. Just to give some background. There were several pieces of code written by different individuals providing output on various products. I was asked to take all of these pieces of code and consolidate into one program so that all of the product data can be viewed together. I was able to do so, the program runs perfectly when run for one month. I was also asked to automate and provide the output for the last 13 months, so no dates or other variables would have to be inputted. Just click run and get the results. So this is where I started having problems. For this particular category of products I broke up the code and ran the 13 month macros. Comparing the results along the way to the results for individual months (without macro). For some reason a portion of code producing correct results with no macro is somehow producing different results within the macro. I was further able to narrow it down to a case when statement. Case when var1 (current month) in (select var2 from tbl2 (previous month)) then 'old' else 'new'. I have been stuck here trying to answer the why.</description>
      <pubDate>Thu, 30 Apr 2020 10:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644186#M192381</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-30T10:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644189#M192382</link>
      <description>I will post the macro with this portion of code shortly.</description>
      <pubDate>Thu, 30 Apr 2020 10:09:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644189#M192382</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-30T10:09:40Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644211#M192392</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO DATA(beg, end, beg_1, end_1);
%LOCAL start start1 i j prvmth dt;
%let start=%sysfunc(inputn(&amp;amp;beg,yymmn6));
%let start1=%sysfunc(inputn(&amp;amp;beg_1,yymmn6));
%do i=0 %to %sysfunc(intck(month,&amp;amp;start,%sysfunc(inputn(&amp;amp;end,yymmn6))));
%do j=0 %to %sysfunc(intck(month,&amp;amp;start1,%sysfunc(inputn(&amp;amp;end_1,yymmn6))));
%let prvmth=%sysfunc(intnx(month,&amp;amp;start1,&amp;amp;j),yymmn6);
%let dt=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i),yymmn6);



PROC SQL;
	CREATE TABLE TEST_&amp;amp;DT AS 
		SELECT 
            PROD,
			CASE WHEN PROD IN (SELECT PROD FROM TEST2_&amp;amp;PRVMTH.)
				THEN 'OLD' 
				ELSE 'NEW' 
			END AS NEW 
		FROM IND_&amp;amp;DT							 
QUIT;

%END;
%END;
%MEND;


%let dt = %sysfunc( intnx( month, &amp;amp;todaysdate, -1, e), yymmn6. ); *TO CALC END OF 13MTH REPORTING PERIOD;
%put dt is &amp;amp;dt;

%let prevdt = %sysfunc( intnx( month, &amp;amp;todaysdate, -13, e), yymmn6. ); *TO CALC START OF 13MTH REPORTING PERIOD;
%put prevdt is &amp;amp;prevdt;

%let prvmth = %sysfunc( intnx( month, &amp;amp;todaysdate, -2, e), yymmn6. ); *TO CALC MTH PRIOR TO CURRENT MTH OF CURRENT YR;
%put prvmth is &amp;amp;prvmth;

%let prevyrmth = %sysfunc( intnx( month, &amp;amp;todaysdate, -14, e), yymmn6. ); *TO CALC MTH PRIOR TO START OF 13 MTH REPORTING;
%put prevyrmth is &amp;amp;prevyrmth;



%DATA(&amp;amp;PREVDT,&amp;amp;DT,&amp;amp;PREVYRMTH,&amp;amp;PRVMTH);


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Apr 2020 11:37:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644211#M192392</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-30T11:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644218#M192396</link>
      <description>&lt;P&gt;And what does the log from the macro say?&lt;/P&gt;</description>
      <pubDate>Thu, 30 Apr 2020 11:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644218#M192396</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-30T11:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current month to previous month in a do loop macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644219#M192397</link>
      <description>Actually, I just changed the name of the field created by the case when statement and that fixed it. Phew! Thanks!</description>
      <pubDate>Thu, 30 Apr 2020 11:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-month-to-previous-month-in-a-do-loop-macro/m-p/644219#M192397</guid>
      <dc:creator>Len18</dc:creator>
      <dc:date>2020-04-30T11:55:57Z</dc:date>
    </item>
  </channel>
</rss>

