<?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: Grouping data based on a moving time window in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Grouping-data-based-on-a-moving-time-window/m-p/940066#M369044</link>
    <description>&lt;P&gt;The col_b dates in your have and want data sets do not match. See observation 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this does what you want though&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by col_a;

   lag_date = ifn(first.col_a, ., lag(col_b));
   dif_months = intck('month', lag_date, col_b);

   if first.col_a          then col_c = 1;
   else if dif_months &amp;gt; 36 then col_c + 1;

   format lag_date date9.;
   drop lag_date dif_months;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;col_a  col_b      col_c
123    30SEP2005  1
123    31MAY2009  2
123    30APR2014  3
123    30APR2014  3
456    31AUG2010  1
456    31JUL2012  1
456    31OCT2017  2
456    31OCT2021  3
456    31DEC2022  3
456    31JAN2024  3&lt;/PRE&gt;</description>
    <pubDate>Tue, 20 Aug 2024 09:41:17 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2024-08-20T09:41:17Z</dc:date>
    <item>
      <title>Grouping data based on a moving time window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-data-based-on-a-moving-time-window/m-p/940065#M369043</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to create segments for each distinct value in a given column based on a moving time window of 36 months. What I mean by that is that for the given data below, I need to check the time distances between each date of col_b for each distinct value of col_a and where the difference shorter than 36 months I want to create a segment.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
     input col_a col_b :date9.;
     format col_b date9.;
     datalines;
     123 30SEP2005
     123 31MAY2009
     123 30APR2014&lt;BR /&gt;     123 30APR2014
     456 31AUG2010
     456 31JUL2012
     456 31OCT2017
     456 31OCT2021
     456 31DEC2022
     456 31JAN2024
;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
     input col_a col_b :date9. col_c $;
     format col_b date9.;
     datalines;
     123 30SEP2005 SEG_1
     123 31MAY2009 SEG_2
     123 31AUG2010 SEG_2
     123 30APR2011 SEG_2
     456 31AUG2010 SEG_1
     456 31JUL2012 SEG_1
     456 31OCT2017 SEG_2
     456 31OCT2021 SEG_3
     456 31DEC2022 SEG_3
     456 31JAN2024 SEG_3
;
run;&lt;/PRE&gt;&lt;P&gt;The key point here is I need to start checking time differences by taking the earliest date as reference for each col_a value. If the time difference between the first earliest and second earliest dates are longer than 36 months than I need to take the second earliest date as reference for further checks. I hope I could articulate the problem clear enough.&lt;BR /&gt;&lt;BR /&gt;Any idea for how to approach/solve this problem is appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2024 08:14:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-data-based-on-a-moving-time-window/m-p/940065#M369043</guid>
      <dc:creator>nbora</dc:creator>
      <dc:date>2024-08-20T08:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping data based on a moving time window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-data-based-on-a-moving-time-window/m-p/940066#M369044</link>
      <description>&lt;P&gt;The col_b dates in your have and want data sets do not match. See observation 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this does what you want though&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by col_a;

   lag_date = ifn(first.col_a, ., lag(col_b));
   dif_months = intck('month', lag_date, col_b);

   if first.col_a          then col_c = 1;
   else if dif_months &amp;gt; 36 then col_c + 1;

   format lag_date date9.;
   drop lag_date dif_months;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;col_a  col_b      col_c
123    30SEP2005  1
123    31MAY2009  2
123    30APR2014  3
123    30APR2014  3
456    31AUG2010  1
456    31JUL2012  1
456    31OCT2017  2
456    31OCT2021  3
456    31DEC2022  3
456    31JAN2024  3&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Aug 2024 09:41:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-data-based-on-a-moving-time-window/m-p/940066#M369044</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-08-20T09:41:17Z</dc:date>
    </item>
  </channel>
</rss>

