<?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: create consecutive intervals in months between 2 dates for all ids in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354636#M82981</link>
    <description>&lt;P&gt;Thanks for your reply Reeza and the correction on the datastep. But there is more to this. I probablly didn't do a good job explaining. The final_date keeps diffreing for each id. I need to know of a way where I can stop creating intervals for each id until I reach the final_date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a possibility that the final_date could be in the 4th interval or the 5th interval. How can I automatically create 4 month intervals until the final_date is reached or in other words in within the last inerval period of that id?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My intention is to calculate the avearge event_value for each id in each of its intervals by event_number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this explains my concern well. I porbably may not need the last if, then else statements too.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know your ideas on this.&lt;/P&gt;</description>
    <pubDate>Fri, 28 Apr 2017 23:42:02 GMT</pubDate>
    <dc:creator>dr2014</dc:creator>
    <dc:date>2017-04-28T23:42:02Z</dc:date>
    <item>
      <title>create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354583#M82961</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I need your advice asap&amp;nbsp;to create intervals of&amp;nbsp;3 months between 2 dates : begin_date and final_date. For the example provided, the 2 dates are within&amp;nbsp;3 months of each other, but I could have the final_date in the analysis anywhere from 1 to 12 or more months apart.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The example provided is for an&amp;nbsp;id=604. It has&amp;nbsp; begin_date, final_date, event_number, event_date.&amp;nbsp;All the dates&amp;nbsp;had a format of yymmdd10. I first converted the&amp;nbsp; 'begin_date' into a sas date callled 'begin_date2'&amp;nbsp;to&amp;nbsp;use the intnx function. Below is my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I created 2 start and end dates ..int1start, int1end, int2start, int2end and 2 intervals p_int1 and&amp;nbsp;p_int2 . I gave&amp;nbsp;the interval&amp;nbsp;a value of '1' or '0'&amp;nbsp;: '1'&amp;nbsp;if the final_date was&amp;nbsp;between the start and end dates for the interval and '0' if the final_date was absent between the start and end dates for the interval.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to know if there is faster way to do this. How can I ask for the final_date to determine the 'last end date' for the 'last interval? Basically, how can stop creating the interval dates just after I reach the final_date? The final_date has to be within the last interval period. Also, can I create these interval start and end dates using macro and also the intervals p_int1 etc.? Please let me know. I attached&amp;nbsp;the end result of the code below.&amp;nbsp; I would like to know about a shorter way to accomplish the same.Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;begin_date2=begin_date; /* converted into a sas date*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;int1start=begin_date2 ;&lt;/P&gt;
&lt;P&gt;int1end=intnx('month',begin_date2,&lt;STRONG&gt;3&lt;/STRONG&gt;,'same')-&lt;STRONG&gt;1&lt;/STRONG&gt; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;int2start=intnx('month',int1end,&lt;STRONG&gt;0&lt;/STRONG&gt;,'same')+&lt;STRONG&gt;1&lt;/STRONG&gt; ;&lt;/P&gt;
&lt;P&gt;int2end=intnx('month',int2start,&lt;STRONG&gt;3&lt;/STRONG&gt;,'same')-&lt;STRONG&gt;1&lt;/STRONG&gt; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;format int1start int1end int2start int2end yymmdd10.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if int1start le final_date_new lt int1end then p_Int1=&lt;STRONG&gt;1&lt;/STRONG&gt; ;&lt;/P&gt;
&lt;P&gt;else if int2start le final_date_new lt int2end then p_Int2=&lt;STRONG&gt;1&lt;/STRONG&gt; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;format int1start int1end int2start int2end yymmdd10.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also have determine these intervals for many others ids in the original&amp;nbsp;dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look forward to your replies. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2017 19:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354583#M82961</guid>
      <dc:creator>dr2014</dc:creator>
      <dc:date>2017-04-28T19:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354632#M82978</link>
      <description>&lt;P&gt;Your basic approach - use of INTNX - is fine.&amp;nbsp; I simplified one of the statements (INT2START) a bit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One concern with the use of the 'SAME' alignment operator is if a date is on the end of the month.&amp;nbsp; The start date in the second row is at the end of November; watch what it does to the February date.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input (begin_date final_date_new)(date9.);
datalines;
11jun201023aug2010
30nov200930jan2010
run;

data want;
set have;
int1start=begin_date ;
int1end=intnx('month',int1start,3,'same')-1 ;
int2start=intnx('month',int1start,3,'same');
/*int2start=intnx('month',int1end,0,'same')+1 ;*/
int2end=intnx('month',int2start,3,'same')-1 ;
if int1start le final_date_new lt int1end then p_Int1=1 ;
else if int2start le final_date_new lt int2end then p_Int2=1 ;
format begin_date final_date_new int1start int1end int2start int2end yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also only one FORMAT statement is needed.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2017 22:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354632#M82978</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-04-28T22:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354636#M82981</link>
      <description>&lt;P&gt;Thanks for your reply Reeza and the correction on the datastep. But there is more to this. I probablly didn't do a good job explaining. The final_date keeps diffreing for each id. I need to know of a way where I can stop creating intervals for each id until I reach the final_date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a possibility that the final_date could be in the 4th interval or the 5th interval. How can I automatically create 4 month intervals until the final_date is reached or in other words in within the last inerval period of that id?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My intention is to calculate the avearge event_value for each id in each of its intervals by event_number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this explains my concern well. I porbably may not need the last if, then else statements too.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know your ideas on this.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2017 23:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354636#M82981</guid>
      <dc:creator>dr2014</dc:creator>
      <dc:date>2017-04-28T23:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354637#M82982</link>
      <description>&lt;P&gt;Thanks also for the tip on the 'same' operator. What would be an alternative way of doing it then to aviod that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2017 23:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354637#M82982</guid>
      <dc:creator>dr2014</dc:creator>
      <dc:date>2017-04-28T23:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354654#M82987</link>
      <description>The real question is the definition of the interval.  Perhaps this behavior is fine.  We do not know at this point because we do not really know your interval definitions.</description>
      <pubDate>Sat, 29 Apr 2017 01:18:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354654#M82987</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-04-29T01:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354655#M82988</link>
      <description>Are you saying that there may be up to 5 intervals not just the two that you coded for and that the FINAL_DATE determines the termination of the intervals?</description>
      <pubDate>Sat, 29 Apr 2017 01:20:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354655#M82988</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-04-29T01:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354840#M83071</link>
      <description>&lt;P&gt;Yes, thats right. The intervals can range from 1 to 8 and the final_date determines the last interval. . I have to define these intervals for each id in the dataset.&amp;nbsp;The interval periods are for 4 months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so for example here are the dates for id 1 and 2:&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="208"&gt;
&lt;P&gt;id&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="208"&gt;
&lt;P&gt;Begin_date&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="208"&gt;
&lt;P&gt;Final_date&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="208"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="208"&gt;
&lt;P&gt;2010-10-04&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="208"&gt;
&lt;P&gt;2011-04-04&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="208"&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="208"&gt;
&lt;P&gt;2010-03-01&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="208"&gt;
&lt;P&gt;2011-11-02&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I need to create 4&amp;nbsp; month intervals between these 2 date variables and stop creating them when I reach the final_date, where the final_date is within the last interval&amp;nbsp;i.e&amp;nbsp; final_date is less than the last interval 'end_date' or it could be the 'end-date' of the last interval.&lt;/P&gt;
&lt;P&gt;I need to do this for each id. Can this be done automatically using arrays or other date options?&lt;/P&gt;</description>
      <pubDate>Sun, 30 Apr 2017 22:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/354840#M83071</guid>
      <dc:creator>dr2014</dc:creator>
      <dc:date>2017-04-30T22:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/355489#M83256</link>
      <description>&lt;P&gt;Here is an array solution with 4 month intervals.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id @3 begin_date date9.
         @13 final_date_new date9.;
datalines;
1 11jun2010 23aug2011
2 30nov2009 30nov2012
3 30nov2009 30nov2016
run;

data want(drop=int);
set have;
array strtdate {8} intstart1 - intstart8;
array stopdate {8} intend1 - intend8;

int=1;
strtdate{int}=begin_date ;
stopdate{int}=min(intnx('month',strtdate{int},4,'same')-1 ,final_date_new);
do while(stopdate{int} lt final_date_new and int lt 8);
   int+1;
   strtdate{int}=intnx('month',strtdate{int-1},4,'same') ;
   stopdate{int}=min(intnx('month',strtdate{int},4,'same')-1 ,final_date_new);
end;

format begin_date final_date_new intstart: intend: yymmdd10.;
run;
title want;
proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The interval start and stop variables have been renamed for ease of coding.&amp;nbsp; The DO WHILE is evaluated at the top of the loop so it can be exited as soon as the criteria (exceed the end date or 8 intervals) is met.&lt;/P&gt;</description>
      <pubDate>Wed, 03 May 2017 06:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/355489#M83256</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-05-03T06:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: create consecutive intervals in months between 2 dates for all ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/358206#M84152</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13683"&gt;@ArtC&lt;/a&gt; for the solution.</description>
      <pubDate>Fri, 12 May 2017 11:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-consecutive-intervals-in-months-between-2-dates-for-all/m-p/358206#M84152</guid>
      <dc:creator>dr2014</dc:creator>
      <dc:date>2017-05-12T11:56:16Z</dc:date>
    </item>
  </channel>
</rss>

