<?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: Select highest count of consecutive months with respect to a starting date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938454#M368621</link>
    <description>So, why you flag out the 2nd row rather than the 6th row?</description>
    <pubDate>Wed, 07 Aug 2024 02:05:39 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2024-08-07T02:05:39Z</dc:date>
    <item>
      <title>Select highest count of consecutive months with respect to a starting date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938423#M368607</link>
      <description>&lt;P&gt;I want to create an indicator for the row of data for each person that is the highest count of consecutive months of insurance BUT that also includes the month of diagnosis in that span.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In an example of one case, I have information about the enrollment status in insurance for the month of diagnosis and the 6 months after. The month of diagnosis is 12/2020 and they are enrolled that month and the next month (1/2021). However, they are not enrolled in insurance the following month (2/2021). They are enrolled again in insurance 3/2021 through 6/2021. I have created the counts of consecutive months of insurance with the 2 consecutive spans. I now need to create an indicator that will show which row (per person) has the highest count of consecutive months of insurance while in the same consecutive span as the diagnosis month.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This one case has a situation where the highest count of consecutive insurance is in a different date span than the diagnosis date. I've tried different combinations of first last but it's not doing the trick.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the code and the screenshots of the tables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*data of one person with 2 different consecutive spans;&lt;BR /&gt;data have;&lt;BR /&gt;input id dx_date :ddmmyy10. enroll_date :ddmmyy10. insurance consec_months;&lt;BR /&gt;format&lt;BR /&gt;dx_date ddmmyy10.&lt;BR /&gt;enroll_date ddmmyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;99 12/01/2020 12/01/2020 1 1&lt;BR /&gt;99 12/01/2020 01/01/2021 1 2&lt;BR /&gt;99 12/01/2020 03/01/2021 1 1&lt;BR /&gt;99 12/01/2020 04/01/2021 1 2&lt;BR /&gt;99 12/01/2020 05/01/2021 1 3&lt;BR /&gt;99 12/01/2020 06/01/2021 1 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print data=have;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rox26_1-1722975755186.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99066i0344B1DA68E4D628/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rox26_1-1722975755186.png" alt="rox26_1-1722975755186.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*manually showing the output I want for the indicator;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;if enroll_date='01JAN2021'd and consec_months=2 then want_indicator=1;&lt;BR /&gt;else want_indicator=0;&lt;BR /&gt;run;&lt;BR /&gt;proc print data=want;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rox26_2-1722975772332.png" style="width: 489px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99067i33AD782F4704AA0F/image-dimensions/489x192?v=v2" width="489" height="192" role="button" title="rox26_2-1722975772332.png" alt="rox26_2-1722975772332.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 20:28:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938423#M368607</guid>
      <dc:creator>rox26</dc:creator>
      <dc:date>2024-08-06T20:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select highest count of consecutive months with respect to a starting date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938454#M368621</link>
      <description>So, why you flag out the 2nd row rather than the 6th row?</description>
      <pubDate>Wed, 07 Aug 2024 02:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938454#M368621</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-08-07T02:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Select highest count of consecutive months with respect to a starting date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938506#M368630</link>
      <description>&lt;P&gt;There are two different spans of consecutive enrollment in this one case. One is 12/2020 to 1/2021 and the other is 3/2021 to 6/2021. I need the count of the first span (2nd row) because it contains the month of diagnosis (12/2020). Most of my other cases are continuously consecutive, so I was just taking the last row of each but here is an instance where that doesn't work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2024 12:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938506#M368630</guid>
      <dc:creator>rox26</dc:creator>
      <dc:date>2024-08-07T12:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: Select highest count of consecutive months with respect to a starting date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938623#M368665</link>
      <description>&lt;P&gt;Here is an attempt that works for your data. I've corrected your INFORMATs as they didn't seem to match your data:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id dx_date :mmddyy10. enroll_date :mmddyy10. insurance consec_months;
format
dx_date ddmmyy10.
enroll_date ddmmyy10.;
datalines;
99 12/01/2020 12/01/2020 1 1
99 12/01/2020 01/01/2021 1 2
99 12/01/2020 03/01/2021 1 1
99 12/01/2020 04/01/2021 1 2
99 12/01/2020 05/01/2021 1 3
99 12/01/2020 06/01/2021 1 4
;
run;

data Want;
  drop last_:;
  set Have;
  by id dx_date enroll_date;
  retain last_enroll_date DX_Flag last_DX_Flag;
  if first.id then do;
    consec_months_derived = 1;
    last_enroll_date = .;
	DX_Flag = '';
  end;
  else do;
    if enroll_date = intnx('MONTH', last_enroll_date, 1) then do;
      consec_months_derived + 1;
	  if enroll_date &amp;gt;= dx_date and last_DX_Flag in ('','Y') then DX_Flag = 'Y';
	end;
	else do;
      consec_months_derived = 1;
	  DX_Flag = 'N';
    end;
  end;
  output;
  last_enroll_date = enroll_date;
  last_DX_Flag = DX_Flag; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've also created a Y/N flag rather than your 0/1 flag.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2024 04:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938623#M368665</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-08-08T04:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select highest count of consecutive months with respect to a starting date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938699#M368677</link>
      <description>&lt;P&gt;Given your data are&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;sorted by ID/ENROLL_DATE&lt;/LI&gt;
&lt;LI&gt;each time there is a gap in ENROLL_DATE, consec_months is set to 1, otherwise consec_months is incremented by 1&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then you can restate the identification of the desired observation.&amp;nbsp; You want the observation which satisfies&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;the most recent consec_months=1 record has enroll_date&amp;lt;=dx_date.&lt;/LI&gt;
&lt;LI&gt;the upcoming record
&lt;OL&gt;
&lt;LI&gt;Begins a new ID,&amp;nbsp;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;or
&lt;OL&gt;
&lt;LI&gt;follows a GAP in enroll_date, indicated by a CONSEC_MONTH=1 in the following record&lt;/LI&gt;
&lt;LI&gt;...&amp;nbsp;and has an enroll_date &amp;gt; dx_date.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&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;data have;
  input id dx_date :ddmmyy10. enroll_date :ddmmyy10. insurance consec_months;
  format dx_date ddmmyy10.  enroll_date ddmmyy10.;
datalines;
99 12/01/2020 12/01/2020 1 1
99 12/01/2020 01/01/2021 1 2
99 12/01/2020 03/01/2021 1 1
99 12/01/2020 04/01/2021 1 2
99 12/01/2020 05/01/2021 1 3
99 12/01/2020 06/01/2021 1 4
;


data want (drop=_:);
  set have (keep=id);
  by id  ;

  merge have
        have (firstobs=2 keep=enroll_date consec_months
              rename=(enroll_date=_nxt_edate consec_months=_nxt_cm));

  if consec_months=1 then _current_start=enroll_date;  
  retain _current_start;  /*Start of current consecutive ENROLL_DATEs */

  if _current_start&amp;lt;=dx_date and (last.id=1  or (_nxt_cm=1 and _nxt_edate&amp;gt;dx_date));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "trick" here is the use of the self-merge with the "firstobs=2" parameter to retrieve the upcoming enroll_date and consec_months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2024 18:56:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-highest-count-of-consecutive-months-with-respect-to-a/m-p/938699#M368677</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-08-09T18:56:50Z</dc:date>
    </item>
  </channel>
</rss>

