<?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: flagging a start date when difference between two consecutive dates is greater than certain valu in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/flagging-a-start-date-when-difference-between-two-consecutive/m-p/408429#M12460</link>
    <description>&lt;P&gt;A slight variation on what you ask turns a difficult problem into an easy one.&amp;nbsp; Consider this approach and whether it would suit your needs.&amp;nbsp; The set-up is up to you:&amp;nbsp; your data set must be sorted by ID and DATE1, and DATE1 must actually be a SAS date and not a character string.&amp;nbsp; From that point:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;prior_date = lag(date);&lt;/P&gt;
&lt;P&gt;interval = date - prior_date;&lt;/P&gt;
&lt;P&gt;if first.id then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;interval = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;prior_date = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;interval_count = 0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if interval &amp;gt;= 46 and interval_count=0 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;interval_count + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;flag = 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;format prior_date date11.;&lt;/P&gt;
&lt;P&gt;drop interval_count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The difference between what you ask and what the program does:&amp;nbsp; the flag appears on the SECOND date of the interval you are seeking.&amp;nbsp; However, the observation contains the variable PRIOR_DATE, which holds the first date of the interval.&amp;nbsp; So it should support what you need to do.&amp;nbsp; For your second bullet point, SAS has many ways to count.&amp;nbsp; This would be one of them:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=want;&lt;/P&gt;
&lt;P&gt;tables ID;&lt;/P&gt;
&lt;P&gt;where flag = 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Sun, 29 Oct 2017 23:23:48 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-10-29T23:23:48Z</dc:date>
    <item>
      <title>flagging a start date when difference between two consecutive dates is greater than certain value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/flagging-a-start-date-when-difference-between-two-consecutive/m-p/408406#M12457</link>
      <description>&lt;P&gt;Data have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Input id $ date1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Datalines;&lt;/P&gt;
&lt;P&gt;101&amp;nbsp; 20-Oct-2016&lt;/P&gt;
&lt;P&gt;101 15-Nov-2016&lt;/P&gt;
&lt;P&gt;101 25-Jan-2017&lt;/P&gt;
&lt;P&gt;101 6-May-2017&lt;/P&gt;
&lt;P&gt;102&amp;nbsp; 10-Oct-2016&lt;/P&gt;
&lt;P&gt;102 25-Nov-2016&lt;/P&gt;
&lt;P&gt;102 15-Jan-2017&lt;/P&gt;
&lt;P&gt;102 26-May-2017&lt;/P&gt;
&lt;P&gt;103&amp;nbsp; 27-Oct-2016&lt;/P&gt;
&lt;P&gt;103 05-Nov-2016&lt;/P&gt;
&lt;P&gt;103 25-Jan-2017&lt;/P&gt;
&lt;P&gt;103 16-Feb-2017&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want achieve two things:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Calculate the number of days between consecutive dates (each dates and previous date within each id). For example, the number of days between 15-Nov-2016 and 20-Oct-2016; 25-Jan-2017 and 15-Nov-2016; 6-May-2017 and 25-Jan-2017, etc&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If the number of days is &amp;gt;=46 I want to count number of id that has any difference in date at least 46 days. And the I want to flag the date the counting began. For example, for id=102, the number of days between 26-May-2017 and 15-Jan-2017, is more than 46 days, therefore I want to flag 15-Jan-2017 since that was the date the counting began.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Sun, 29 Oct 2017 20:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/flagging-a-start-date-when-difference-between-two-consecutive/m-p/408406#M12457</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2017-10-29T20:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: flagging a start date when difference between two consecutive dates is greater than certain valu</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/flagging-a-start-date-when-difference-between-two-consecutive/m-p/408429#M12460</link>
      <description>&lt;P&gt;A slight variation on what you ask turns a difficult problem into an easy one.&amp;nbsp; Consider this approach and whether it would suit your needs.&amp;nbsp; The set-up is up to you:&amp;nbsp; your data set must be sorted by ID and DATE1, and DATE1 must actually be a SAS date and not a character string.&amp;nbsp; From that point:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;prior_date = lag(date);&lt;/P&gt;
&lt;P&gt;interval = date - prior_date;&lt;/P&gt;
&lt;P&gt;if first.id then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;interval = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;prior_date = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;interval_count = 0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if interval &amp;gt;= 46 and interval_count=0 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;interval_count + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;flag = 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;format prior_date date11.;&lt;/P&gt;
&lt;P&gt;drop interval_count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The difference between what you ask and what the program does:&amp;nbsp; the flag appears on the SECOND date of the interval you are seeking.&amp;nbsp; However, the observation contains the variable PRIOR_DATE, which holds the first date of the interval.&amp;nbsp; So it should support what you need to do.&amp;nbsp; For your second bullet point, SAS has many ways to count.&amp;nbsp; This would be one of them:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=want;&lt;/P&gt;
&lt;P&gt;tables ID;&lt;/P&gt;
&lt;P&gt;where flag = 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2017 23:23:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/flagging-a-start-date-when-difference-between-two-consecutive/m-p/408429#M12460</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-29T23:23:48Z</dc:date>
    </item>
  </channel>
</rss>

