<?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 enrollment months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836515#M330750</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I am trying to count the number of enrollment months based on enrollment days and deceased dates. The example data is as attached, and the last column Enrolled_Months is the target field to construct. Basically, I need to count in any month with at least one enrollment&amp;nbsp;day, but exclude the month a person dies and any subsequent months. Generally the enrollment days are consistent with decease date, however, there could be some data entry error due to time lag, such as Person20.&lt;/P&gt;&lt;P&gt;Any suggestion would be greatly appreciate!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Lichee&lt;/P&gt;</description>
    <pubDate>Mon, 03 Oct 2022 14:24:20 GMT</pubDate>
    <dc:creator>lichee</dc:creator>
    <dc:date>2022-10-03T14:24:20Z</dc:date>
    <item>
      <title>Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836515#M330750</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I am trying to count the number of enrollment months based on enrollment days and deceased dates. The example data is as attached, and the last column Enrolled_Months is the target field to construct. Basically, I need to count in any month with at least one enrollment&amp;nbsp;day, but exclude the month a person dies and any subsequent months. Generally the enrollment days are consistent with decease date, however, there could be some data entry error due to time lag, such as Person20.&lt;/P&gt;&lt;P&gt;Any suggestion would be greatly appreciate!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Lichee&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2022 14:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836515#M330750</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-10-03T14:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836516#M330751</link>
      <description>&lt;P&gt;Many of us will not download Excel files or other Microsoft Office files as they are a security risk. Please provide (a portion of) your data as &lt;FONT color="#FF0000"&gt;working&lt;/FONT&gt; SAS data step code, which you can type in yourself, or follow &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions,&lt;/A&gt;&amp;nbsp;and include this code in your reply.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2022 14:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836516#M330751</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-03T14:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836554#M330764</link>
      <description>&lt;P&gt;Thanks for pointing that out! Here is the code for the example data. Again, the last column Enrolled_Months is the target to construct. Thank you!&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input DummyID $ Decease_Date :mmddyy. EnrolledDays_201801 EnrolledDays_201802 EnrolledDays_201803 EnrolledDays_201804 EnrolledDays_201805 EnrolledDays_201806 EnrolledDays_201807&lt;BR /&gt;EnrolledDays_201808 EnrolledDays_201809 EnrolledDays_201810 EnrolledDays_201811 EnrolledDays_201812 Enrolled_Months&lt;BR /&gt;;&lt;BR /&gt;format Decease_Date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;Person1,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person2,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person3,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person4,2/25/2018,31,25,0,0,0,0,0,0,0,0,0,0,1&lt;BR /&gt;Person5,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person6,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person7,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person8,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person9,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person10,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person11,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person12,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person13,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person14,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person15,10/5/2018,0,0,14,30,31,30,31,31,30,5,0,0,7&lt;BR /&gt;Person16,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person17,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person18,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person19,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person20,6/7/2018,31,28,31,30,31,30,7,0,0,0,0,0,6&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2022 16:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836554#M330764</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-10-03T16:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836572#M330772</link>
      <description>&lt;P&gt;Is your last record correct, or should it be 5?&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;Person20,6/7/2018,31,28,31,30,31,30,7,0,0,0,0,0,6&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The person dies in June so according to the criteria originally stated:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Basically, I need to count in any month with at least one enrollment&amp;nbsp;day, but exclude the month a person dies and any subsequent months. Generally the enrollment days are consistent with decease date, however, there could be some data entry error due to time lag, such as Person20.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If 5 is correct for the last record this should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array _enr(12) EnrolledDays_2018:;
if not missing(decease_date) then death_month = month(decease_date);

enrolled_months_calc =0;

do i=1 to min(death_month-1, 12);
if _enr(i) ne 0 then enrolled_months_calc+1;
end;

check= enrolled_months - enrolled_months_calc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/430334"&gt;@lichee&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for pointing that out! Here is the code for the example data. Again, the last column Enrolled_Months is the target to construct. Thank you!&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input DummyID $ Decease_Date :mmddyy. EnrolledDays_201801 EnrolledDays_201802 EnrolledDays_201803 EnrolledDays_201804 EnrolledDays_201805 EnrolledDays_201806 EnrolledDays_201807&lt;BR /&gt;EnrolledDays_201808 EnrolledDays_201809 EnrolledDays_201810 EnrolledDays_201811 EnrolledDays_201812 Enrolled_Months&lt;BR /&gt;;&lt;BR /&gt;format Decease_Date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;Person1,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person2,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person3,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person4,2/25/2018,31,25,0,0,0,0,0,0,0,0,0,0,1&lt;BR /&gt;Person5,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person6,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person7,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person8,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person9,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person10,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person11,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person12,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person13,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person14,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person15,10/5/2018,0,0,14,30,31,30,31,31,30,5,0,0,7&lt;BR /&gt;Person16,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person17,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person18,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person19,,31,28,31,30,31,30,31,31,30,31,30,31,12&lt;BR /&gt;Person20,6/7/2018,31,28,31,30,31,30,7,0,0,0,0,0,6&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2022 17:52:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836572#M330772</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-10-03T17:52:49Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836574#M330774</link>
      <description>&lt;P&gt;Wouldn't person 20 have 5 enrolled months because their deceased date is June 7, 2018?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's talk about &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 19&lt;/A&gt;. Long data sets are easier to work with and easier to program than wide data sets, where month information are included in the variable name. So first I create a long data set via PROC TRANSPOSE. Then counting is done via simple IF statements and you can then use PROC SUMMARY to add up the number of months enrolled. (Lesson: don't create wide data sets with calendar information in the variable name. Assuming you create the data as a long data set, there is no need for PROC TRANSPOSE and the month information is a variable in your data set, and this is much easier to work with.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have(drop=enrolled_months) out=have1 prefix=enrolledDays;
    by dummyid notsorted decease_date;
    var enrolled:;
run;
data have2;
    set have1;
    month_enrolled=input(scan(_name_,2,'_'),yymmn6.);
    if missing(decease_date) and enrolleddays1&amp;gt;0 then enrolled=1;
    else if not missing(decease_date) and month(decease_date)&amp;gt;month(month_enrolled) and enrolleddays1&amp;gt;0 then enrolled=1;
    else enrolled=0;
    format month_enrolled yymms7.;
    drop _name_;
run;
proc summary data=have2 nway;
    class dummyid;
    var enrolled;
    output out=want sum=enrolled_months;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, the above would need to be modified if the data collected crossed year boundaries, but you didn't give us data that exists in more than one years, so the above does work.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2022 18:18:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836574#M330774</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-03T18:18:16Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836580#M330779</link>
      <description>You are right. The last one should have Enrolled_Months=5. Thanks a lot!</description>
      <pubDate>Mon, 03 Oct 2022 18:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836580#M330779</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-10-03T18:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836944#M330931</link>
      <description>&lt;P&gt;Actually, I do want to explore and learn the code for the data crossing year boundaries. So if I have the data below, how would the code look like? Thanks again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines truncover dsd;&lt;BR /&gt;input DummyID $ Decease_Date :mmddyy. EnrolledDays_201707 EnrolledDays_201708 EnrolledDays_201709 EnrolledDays_201710&lt;BR /&gt;EnrolledDays_201711 EnrolledDays_201712 EnrolledDays_201801 EnrolledDays_201802 EnrolledDays_201803 EnrolledDays_201804&lt;BR /&gt;EnrolledDays_201805 EnrolledDays_201806&lt;BR /&gt;Enrolled_Months&lt;BR /&gt;;&lt;BR /&gt;format Decease_Date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;Person1,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person2,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person3,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person4,2/25/2018,0,0,0,0,0,0,31,25,0,0,0,0,1&lt;BR /&gt;Person5,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person6,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person7,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person8,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person9,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person10,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person11,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person12,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person13,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person14,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person15,10/5/2018,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person16,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person17,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person18,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person19,,31,31,30,31,30,31,31,28,31,30,31,30,12&lt;BR /&gt;Person20,6/7/2018,0,0,0,0,0,0,31,28,31,30,31,30,5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 13:22:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836944#M330931</guid>
      <dc:creator>lichee</dc:creator>
      <dc:date>2022-10-05T13:22:13Z</dc:date>
    </item>
    <item>
      <title>Re: Count enrollment months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836945#M330932</link>
      <description>&lt;P&gt;I believe the only change would be to this line&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else if not missing(decease_date) and month(decease_date)&amp;gt;month(month_enrolled) and enrolleddays1&amp;gt;0 then enrolled=1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think this following code works, but I will let you do the testing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else if not missing(decease_date) and intnx('month',decease_date,0,'b')&amp;gt;month_enrolled and enrolleddays1&amp;gt;0 then enrolled=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This should work because we are using actual SAS date values and not character strings or variable names to represent the month_enrolled. Since you're really just looking at months, we use the first day of each month to do the comparisons of decease_date to month_enrolled.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As stated above, I think you would be wise to not create wide data sets with calendar information in the variable name, this is not a good format for your data set. Wide data sets like the one you have means that every month, you have to modify your code to accommodate the new month. Having a long data set, with a variable named month_enrolled, is a better approach in the long run, no modification to the code is needed as the months change.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 13:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-enrollment-months/m-p/836945#M330932</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-10-05T13:36:33Z</dc:date>
    </item>
  </channel>
</rss>

