<?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: Generate date based on previous date in do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705524#M216466</link>
    <description>&lt;P&gt;You don't really need the DURATION value if you use a "while (date&amp;lt;end_dt)" condition in the do loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  format START_DT END_DT DATE date9.;
  START_DT = '19APR2018'd;
  EVENT_DAY_OF_MONTH = 6;
  END_DT = '05JAN2019'd;
  /** don't need: DURATION = intck('month',START_DT,END_DT)*2; */

  date=start_dt; output;
  do  while (date&amp;lt;end_dt);
    date=intnx('month',date,0,'E'); if date&amp;lt;end_dt then output;
	date=min(end_dt,date+event_day_of_month); output;
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited note:&amp;nbsp; The other thing this program avoids is the MDY function.&amp;nbsp; Since underlying date values are just the number of days after 01jan1960 (or before in the case of negative values), you merely need to add EVENT_DAY_OF_MONTH to the date value representing the end of the previous month.&amp;nbsp; &amp;nbsp; The only exception would be if EVENT_DAY_OF_MONTH is greater than the day-of-month in END_DT.&amp;nbsp; Hence the MIN function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second editted note:&amp;nbsp; I ran a test with END_DT=15JAN2019 and EVENT_DAY_OF_MONTH=6.&amp;nbsp; It generated '06jan2019','31'jan2019', followed by the '15JAN2019'.&amp;nbsp; Assuming the op would want to drop the 31jan2019, I corrected the code.&amp;nbsp; You can see the new "if date&amp;lt;end_dt then" preceding one of the output statements.&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/359808"&gt;@_Zack_&lt;/a&gt;&amp;nbsp;:&amp;nbsp; If I misinterpreted your expectation for END_DT=15JAN2019 and EVENT_DAY_OF_MONTH=6, please state what you would want.&lt;/P&gt;</description>
    <pubDate>Sun, 13 Dec 2020 19:29:03 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-12-13T19:29:03Z</dc:date>
    <item>
      <title>Generate date based on previous date in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705507#M216455</link>
      <description>&lt;P&gt;Hi, I'm kind of stuck when try to generate a series of date within do loop. For iteration 0 and 1 the DATE is set with fixed formula, however starting from iteration &amp;gt; 1, two DATE should be generated within same month (EVENT_DAY_OF_MONTH, follow by end of month), and repeated this pattern until iteration &amp;lt; DURATION.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&lt;BR /&gt;format START_DT END_DT DATE DATE_LAG date9.;&lt;BR /&gt;&lt;BR /&gt;START_DT = '19APR2018'd;&lt;BR /&gt;EVENT_DAY_OF_MONTH = 6;&lt;BR /&gt;END_DT = '05JAN2019'd;&lt;BR /&gt;DURATION = intck('month',START_DT,END_DT)*2;&lt;BR /&gt;&lt;BR /&gt;do i=0 to DURATION by 1;&lt;BR /&gt;if i = 0 then DATE = START_DT;&lt;BR /&gt;if i = 1 then DATE = intnx('month',START_DT,i-1,'E');&lt;BR /&gt;DATE_LAG = LAG(DATE);&lt;BR /&gt;if 1 &amp;lt; i &amp;lt; DURATION and DAY(DATE_LAG) ne EVENT_DAY_OF_MONTH then DATE = mdy(month(intnx('month',START_DT,i-1,'E')),EVENT_DAY_OF_MONTH,year(intnx('month',START_DT,i-1,'E')));&lt;BR /&gt;if 1 &amp;lt; i &amp;lt; DURATION and DAY(DATE_LAG) = EVENT_DAY_OF_MONTH then DATE = intnx('month',START_DT,i-1,'E');&lt;BR /&gt;if i = DURATION then DATE = END_DT;&lt;/P&gt;&lt;P&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Sample expected result i'm looking for:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;2018-04-19&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-04-30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-05-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-05-31&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-06-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-06-30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-07-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-07-31&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-08-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-08-31&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-09-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-09-30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-10-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-10-31&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-11-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-11-30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-12-06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018-12-31&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2019-01-05&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 16:08:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705507#M216455</guid>
      <dc:creator>_Zack_</dc:creator>
      <dc:date>2020-12-13T16:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: Generate date based on previous date in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705510#M216456</link>
      <description>&lt;P&gt;Just use two calls to INTNX and two output statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  START_DT = '19APR2018'd;
  EVENT_DAY_OF_MONTH = 6;
  END_DT = '05JAN2019'd;
  DURATION = intck('month',START_DT,END_DT)*2;
  do offset = 0 to intck('month',start_dt,end_dt);
    date=intnx('month',start_dt,offset) + event_day_of_month -1 ;
    output;
    date=intnx('month',date,0,'e');
    output;
  end;
  format START_DT END_DT DATE date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 16:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705510#M216456</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-13T16:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Generate date based on previous date in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705512#M216457</link>
      <description>&lt;P&gt;I don't see a SET or INFILE statement, nor OUTPUT prior to using the LAG()&amp;nbsp; function.&lt;/P&gt;
&lt;P&gt;In a loop you should save the previous value by assigning it to a separate variable. TRY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

format START_DT END_DT DATE DATE_LAG date9.;

START_DT = '19APR2018'd;
EVENT_DAY_OF_MONTH = 6;
END_DT = '05JAN2019'd;
DURATION = intck('month',START_DT,END_DT)*2;

do i=0 to DURATION by 1;
	if i = 0 then DATE = START_DT;
	DATE_LAG = date; /* line inserted */
	if i = 1 then DATE = intnx('month',START_DT,i-1,'E');
	/* DATE_LAG = LAG(DATE); line canceled */
	if 1 &amp;lt; i &amp;lt; DURATION and DAY(DATE_LAG) ne EVENT_DAY_OF_MONTH then DATE = mdy(month(intnx('month',START_DT,i-1,'E')),EVENT_DAY_OF_MONTH,year(intnx('month',START_DT,i-1,'E')));
	if 1 &amp;lt; i &amp;lt; DURATION and DAY(DATE_LAG) = EVENT_DAY_OF_MONTH then DATE = intnx('month',START_DT,i-1,'E');
	if i = DURATION then DATE = END_DT;

	output;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 16:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705512#M216457</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-12-13T16:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Generate date based on previous date in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705513#M216458</link>
      <description>&lt;P&gt;First attempt, not sure wheter this will always give the expected result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=date);
	start_date = '19APR2018'd;
	event_day_of_month = 6;
	end_date = '05JAN2019'd;
	
	format date yymmddd10.;
	date = start_date;
	
	do while (date &amp;lt;= end_date);
		output;
		if day(date) &amp;lt; 28 then do;
			date = intnx('month', date, 0, 'end');
		end;
		else do;
			date = intnx('month', date, 1, 'beginning');
			date = intnx('day', date, 5);
		end;
	end;
	
	date = end_date;
	output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 16:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705513#M216458</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-13T16:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Generate date based on previous date in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705524#M216466</link>
      <description>&lt;P&gt;You don't really need the DURATION value if you use a "while (date&amp;lt;end_dt)" condition in the do loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  format START_DT END_DT DATE date9.;
  START_DT = '19APR2018'd;
  EVENT_DAY_OF_MONTH = 6;
  END_DT = '05JAN2019'd;
  /** don't need: DURATION = intck('month',START_DT,END_DT)*2; */

  date=start_dt; output;
  do  while (date&amp;lt;end_dt);
    date=intnx('month',date,0,'E'); if date&amp;lt;end_dt then output;
	date=min(end_dt,date+event_day_of_month); output;
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited note:&amp;nbsp; The other thing this program avoids is the MDY function.&amp;nbsp; Since underlying date values are just the number of days after 01jan1960 (or before in the case of negative values), you merely need to add EVENT_DAY_OF_MONTH to the date value representing the end of the previous month.&amp;nbsp; &amp;nbsp; The only exception would be if EVENT_DAY_OF_MONTH is greater than the day-of-month in END_DT.&amp;nbsp; Hence the MIN function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second editted note:&amp;nbsp; I ran a test with END_DT=15JAN2019 and EVENT_DAY_OF_MONTH=6.&amp;nbsp; It generated '06jan2019','31'jan2019', followed by the '15JAN2019'.&amp;nbsp; Assuming the op would want to drop the 31jan2019, I corrected the code.&amp;nbsp; You can see the new "if date&amp;lt;end_dt then" preceding one of the output statements.&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/359808"&gt;@_Zack_&lt;/a&gt;&amp;nbsp;:&amp;nbsp; If I misinterpreted your expectation for END_DT=15JAN2019 and EVENT_DAY_OF_MONTH=6, please state what you would want.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 19:29:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-date-based-on-previous-date-in-do-loop/m-p/705524#M216466</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-12-13T19:29:03Z</dc:date>
    </item>
  </channel>
</rss>

