<?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 Count number of enrollment months in the past 12 months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886894#M350438</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two datasets: one with individual IDs and measurement month, and the other with individuals' enrollment months. I wanted to count the number of enrollment months in the past 12 months per measurement month. Could anyone provide some efficient code to do that? Below are the data sets I have (have 1, have2) and the target data file (want). Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have1;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID measure_mth;&lt;BR /&gt;datalines;&lt;BR /&gt;1,202201&lt;BR /&gt;1,202202&lt;BR /&gt;1,202203&lt;BR /&gt;1,202204&lt;BR /&gt;1,202205&lt;BR /&gt;1,202206&lt;BR /&gt;1,202207&lt;BR /&gt;1,202208&lt;BR /&gt;1,202209&lt;BR /&gt;1,202210&lt;BR /&gt;1,202211&lt;BR /&gt;1,202212&lt;BR /&gt;2,202201&lt;BR /&gt;2,202202&lt;BR /&gt;2,202203&lt;BR /&gt;2,202204&lt;BR /&gt;2,202205&lt;BR /&gt;2,202206&lt;BR /&gt;2,202207&lt;BR /&gt;2,202208&lt;BR /&gt;2,202209&lt;BR /&gt;2,202210&lt;BR /&gt;2,202211&lt;BR /&gt;2,202212&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data have2;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID enrolled_mth;&lt;BR /&gt;datalines;&lt;BR /&gt;1,202101&lt;BR /&gt;1,202102&lt;BR /&gt;1,202103&lt;BR /&gt;1,202104&lt;BR /&gt;1,202105&lt;BR /&gt;1,202106&lt;BR /&gt;1,202107&lt;BR /&gt;1,202201&lt;BR /&gt;1,202202&lt;BR /&gt;1,202203&lt;BR /&gt;1,202204&lt;BR /&gt;1,202205&lt;BR /&gt;1,202206&lt;BR /&gt;1,202207&lt;BR /&gt;1,202208&lt;BR /&gt;1,202209&lt;BR /&gt;1,202210&lt;BR /&gt;1,202211&lt;BR /&gt;1,202212&lt;BR /&gt;2,202105&lt;BR /&gt;2,202106&lt;BR /&gt;2,202107&lt;BR /&gt;2,202108&lt;BR /&gt;2,202109&lt;BR /&gt;2,202110&lt;BR /&gt;2,202111&lt;BR /&gt;2,202112&lt;BR /&gt;2,202201&lt;BR /&gt;2,202202&lt;BR /&gt;2,202203&lt;BR /&gt;2,202204&lt;BR /&gt;2,202205&lt;BR /&gt;2,202206&lt;BR /&gt;2,202207&lt;BR /&gt;2,202208&lt;BR /&gt;2,202209&lt;BR /&gt;2,202210&lt;BR /&gt;2,202211&lt;BR /&gt;2,202212&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID measure_mth N_enrolled_mths;&lt;BR /&gt;datalines;&lt;BR /&gt;1,202201,7&lt;BR /&gt;1,202202,7&lt;BR /&gt;1,202203,7&lt;BR /&gt;1,202204,7&lt;BR /&gt;1,202205,7&lt;BR /&gt;1,202206,7&lt;BR /&gt;1,202207,7&lt;BR /&gt;1,202208,8&lt;BR /&gt;1,202209,9&lt;BR /&gt;1,202210,10&lt;BR /&gt;1,202211,11&lt;BR /&gt;1,202212,12&lt;BR /&gt;2,202201,9&lt;BR /&gt;2,202202,10&lt;BR /&gt;2,202203,11&lt;BR /&gt;2,202204,12&lt;BR /&gt;2,202205,12&lt;BR /&gt;2,202206,12&lt;BR /&gt;2,202207,12&lt;BR /&gt;2,202208,12&lt;BR /&gt;2,202209,12&lt;BR /&gt;2,202210,12&lt;BR /&gt;2,202211,12&lt;BR /&gt;2,202212,12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jul 2023 19:27:58 GMT</pubDate>
    <dc:creator>lichee</dc:creator>
    <dc:date>2023-07-28T19:27:58Z</dc:date>
    <item>
      <title>Count number of enrollment months in the past 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886894#M350438</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two datasets: one with individual IDs and measurement month, and the other with individuals' enrollment months. I wanted to count the number of enrollment months in the past 12 months per measurement month. Could anyone provide some efficient code to do that? Below are the data sets I have (have 1, have2) and the target data file (want). Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have1;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID measure_mth;&lt;BR /&gt;datalines;&lt;BR /&gt;1,202201&lt;BR /&gt;1,202202&lt;BR /&gt;1,202203&lt;BR /&gt;1,202204&lt;BR /&gt;1,202205&lt;BR /&gt;1,202206&lt;BR /&gt;1,202207&lt;BR /&gt;1,202208&lt;BR /&gt;1,202209&lt;BR /&gt;1,202210&lt;BR /&gt;1,202211&lt;BR /&gt;1,202212&lt;BR /&gt;2,202201&lt;BR /&gt;2,202202&lt;BR /&gt;2,202203&lt;BR /&gt;2,202204&lt;BR /&gt;2,202205&lt;BR /&gt;2,202206&lt;BR /&gt;2,202207&lt;BR /&gt;2,202208&lt;BR /&gt;2,202209&lt;BR /&gt;2,202210&lt;BR /&gt;2,202211&lt;BR /&gt;2,202212&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data have2;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID enrolled_mth;&lt;BR /&gt;datalines;&lt;BR /&gt;1,202101&lt;BR /&gt;1,202102&lt;BR /&gt;1,202103&lt;BR /&gt;1,202104&lt;BR /&gt;1,202105&lt;BR /&gt;1,202106&lt;BR /&gt;1,202107&lt;BR /&gt;1,202201&lt;BR /&gt;1,202202&lt;BR /&gt;1,202203&lt;BR /&gt;1,202204&lt;BR /&gt;1,202205&lt;BR /&gt;1,202206&lt;BR /&gt;1,202207&lt;BR /&gt;1,202208&lt;BR /&gt;1,202209&lt;BR /&gt;1,202210&lt;BR /&gt;1,202211&lt;BR /&gt;1,202212&lt;BR /&gt;2,202105&lt;BR /&gt;2,202106&lt;BR /&gt;2,202107&lt;BR /&gt;2,202108&lt;BR /&gt;2,202109&lt;BR /&gt;2,202110&lt;BR /&gt;2,202111&lt;BR /&gt;2,202112&lt;BR /&gt;2,202201&lt;BR /&gt;2,202202&lt;BR /&gt;2,202203&lt;BR /&gt;2,202204&lt;BR /&gt;2,202205&lt;BR /&gt;2,202206&lt;BR /&gt;2,202207&lt;BR /&gt;2,202208&lt;BR /&gt;2,202209&lt;BR /&gt;2,202210&lt;BR /&gt;2,202211&lt;BR /&gt;2,202212&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input Person_ID measure_mth N_enrolled_mths;&lt;BR /&gt;datalines;&lt;BR /&gt;1,202201,7&lt;BR /&gt;1,202202,7&lt;BR /&gt;1,202203,7&lt;BR /&gt;1,202204,7&lt;BR /&gt;1,202205,7&lt;BR /&gt;1,202206,7&lt;BR /&gt;1,202207,7&lt;BR /&gt;1,202208,8&lt;BR /&gt;1,202209,9&lt;BR /&gt;1,202210,10&lt;BR /&gt;1,202211,11&lt;BR /&gt;1,202212,12&lt;BR /&gt;2,202201,9&lt;BR /&gt;2,202202,10&lt;BR /&gt;2,202203,11&lt;BR /&gt;2,202204,12&lt;BR /&gt;2,202205,12&lt;BR /&gt;2,202206,12&lt;BR /&gt;2,202207,12&lt;BR /&gt;2,202208,12&lt;BR /&gt;2,202209,12&lt;BR /&gt;2,202210,12&lt;BR /&gt;2,202211,12&lt;BR /&gt;2,202212,12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 19:27:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886894#M350438</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2023-07-28T19:27:58Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of enrollment months in the past 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886930#M350453</link>
      <description>&lt;P&gt;Below solution requires your columns&amp;nbsp;measure_mth and&amp;nbsp;enrolled_mth to store a SAS Date value and not just a number.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've changed your input statements to create such sample data. You would need to do something similar for your real data.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1690592831168.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86237i710CCB0B1A8262C2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1690592831168.png" alt="Patrick_0-1690592831168.png" /&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1690592860426.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86238i37E2436877B9B44F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1690592860426.png" alt="Patrick_1-1690592860426.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you've got SAS Date values you can use function intck() to join the data for your rolling counts.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
/*  create table want2 as*/
  select
    t1.person_id
    ,t1.measure_mth
    ,count(*) as n_enrolled_mths
  from 
    have1 t1 left join have2 t2
    on 
      t1.person_id=t2.person_id
      and intck('month',t2.enrolled_mth,t1.measure_mth) between 0 and 11
  group by t1.person_id, t1.measure_mth
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jul 2023 01:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886930#M350453</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-07-29T01:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count number of enrollment months in the past 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886999#M350491</link>
      <description>&lt;P&gt;Assuming you've corrected have1 and have2 along the lines suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If have2 and have1 are already sorted by person_id, you can:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read all have2 obs for a given id, and add to a two-way array (year by month) of counters from 0 to 11 months forward from enrolled_mth.&lt;/LI&gt;
&lt;LI&gt;Read all have1 obs for the same id and retrieve the value from the array of counters corresponding to measure_mth, and output.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=enrolled_mth);
  set have2 (in=in2)   have1 (in=in1);
  by person_id;

  array n_year_by_month{2021:2024,1:12} _temporary_;

  if first.person_id then call missing(of n_year_by_month{*});

  if in2 then do _n_=0 to 11;
    n_year_by_month{year(enrolled_mth),month(enrolled_mth)}+1;
    enrolled_mth=intnx('month',enrolled_mth,1);
  end;

  if in1;
  n_enrolled_mths=n_year_by_month{year(measure_mth),month(measure_mth)};
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Two points:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Make sure the array bounds cover from the earlier of the minimum enroll_mth or minimum measure_mth through the maximum of measure_mth or 11 months beyond the maximum enroll_mth.&lt;/LI&gt;
&lt;LI&gt;It doesn't matter whether have1 or have2 are chronologically sorted within person_id.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sun, 30 Jul 2023 03:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-number-of-enrollment-months-in-the-past-12-months/m-p/886999#M350491</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-30T03:04:12Z</dc:date>
    </item>
  </channel>
</rss>

