<?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: How to calculate days for each month using two dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901240#M356168</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/359105"&gt;@Scooby3g&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;When I try&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s suggestion, obs 8 should be 11 (sep 20 to sep 30) but the output is 30 instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I tried&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;'s suggestion was missing the Year_month that i want the number of days to be attributed to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;Please describe exactly how "year_month" is missing?&lt;/P&gt;
&lt;P class="1698934695689"&gt;If you apply a format of YYMMD7 to date1 I think you will see the VALUE in the APPEARANCE you want.&lt;/P&gt;
&lt;P class="1698934695689"&gt;It is a bad idea to store anything that should actually be a date as other than a date value.&lt;/P&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="1698934695689"&gt;"dedupe" Proc Sort with the nodupkey . Or don't put repeated values into the process to begin with (sort BEFORE this step).&lt;/P&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Nov 2023 14:27:20 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-11-02T14:27:20Z</dc:date>
    <item>
      <title>How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900965#M356071</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I've coded this in the past but for the life of me I cannot recall how I did it in the past. It feels like its on the tip of my mind but I just cant remember.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I calculate number of days for each month using date1 and date2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, obs 1 will have 31 days for 2023_01 and 4 days for 2023_02&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines ;
format date1 date2 date7.;
input date1 :date7.  State:$  date2 :date7. ID ;
datalines;
01Jan23	    NY	04Feb23	 1
01Jan23	    NY	04Feb23	 1
01Jan23		NY	04Feb23	 1
01Jan23		NY	04Feb23	 1
01Jul23 	CA	04Jul23	 2
01Jul23 	CA	04Jul23	 2
01Jul23 	CA	04Jul23	 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 20:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900965#M356071</guid>
      <dc:creator>Scooby3g</dc:creator>
      <dc:date>2023-10-31T20:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900966#M356072</link>
      <description>Subtract the two dates and add 1?</description>
      <pubDate>Tue, 31 Oct 2023 20:47:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900966#M356072</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-31T20:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900967#M356073</link>
      <description>that only gives number of days between date1 and date2. I want to get days by month by state.</description>
      <pubDate>Tue, 31 Oct 2023 20:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900967#M356073</guid>
      <dc:creator>Scooby3g</dc:creator>
      <dc:date>2023-10-31T20:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900969#M356074</link>
      <description>Please show expected output. Is it always two months or will the amount of months vary.</description>
      <pubDate>Tue, 31 Oct 2023 21:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900969#M356074</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-31T21:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900971#M356075</link>
      <description>state yr_month days&lt;BR /&gt;NY      2023_01  31&lt;BR /&gt;NY      2023_02 4&lt;BR /&gt;CA      2023_07 4&lt;BR /&gt;&lt;BR /&gt;The number of months would depend on how many months are between date1 and date1&lt;BR /&gt;</description>
      <pubDate>Tue, 31 Oct 2023 21:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900971#M356075</guid>
      <dc:creator>Scooby3g</dc:creator>
      <dc:date>2023-10-31T21:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900976#M356076</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/359105"&gt;@Scooby3g&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;state yr_month days&lt;BR /&gt;NY 2023_01 31&lt;BR /&gt;NY 2023_02 4&lt;BR /&gt;CA 2023_07 4&lt;BR /&gt;&lt;BR /&gt;The number of months would depend on how many months are between date1 and date1&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Doesn't bear much resemblance to example input data. You example in the first post repeats dates:&lt;/P&gt;
&lt;P&gt;So there request is for each "start date" "end_date" to increment the month as needed, create an additional output observation with the "new"month start and the number of days?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this perhaps:&lt;/P&gt;
&lt;PRE&gt;data have;
infile datalines ;
format date1 date2 date7.;
input date1 :date7.  State:$  date2 :date7. ID ;
datalines;
01Jan23	    NY	04Feb23	 1
01Jul23 	CA	04Jul23	 2
15Jan23 aa  21May23 3
;
run;

data want;
   set have;
   do i=0 to intck('month',date1,date2);
      days=1+ intck('day',date1,min(intnx('month',date1,0,'e'),date2));
      output;
      date1=intnx('month',date1,1,'b');
   end;
   drop  i;
run;&lt;/PRE&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>Tue, 31 Oct 2023 21:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900976#M356076</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-31T21:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900977#M356077</link>
      <description>&lt;P&gt;This does not account for dates that may start not on the first of the month for date1.&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 id;
if first.id;
nmonths = intck('month', date1, date2);

do i = 0 to nmonths;
    month = intnx('month', date1, i, 'b');
    format month yymmd7.;
    num_days = intck('days', intnx('month', date1, i, 'b'), min(date2, intnx('month', date1, i, 'e')))+1;
    output;
end;

keep id state month num_days;
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/359105"&gt;@Scooby3g&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;state yr_month days&lt;BR /&gt;NY 2023_01 31&lt;BR /&gt;NY 2023_02 4&lt;BR /&gt;CA 2023_07 4&lt;BR /&gt;&lt;BR /&gt;The number of months would depend on how many months are between date1 and date1&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 21:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/900977#M356077</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-31T21:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901232#M356164</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Thank you both for your feedback. What I am trying to do is calculate patient days during the patients admission for each month on each unit they stay on.&amp;nbsp; date1 would be the patients admission date and date2 would be the discharge date.&amp;nbsp; Consider State as the location of the patient. I tried the codes you suggested but added an additional row to the "HAVE" table data step before running your suggestions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines ;
format date1 date2 date7.;
input date1 :date7.  State:$  date2 :date7. ID ;
datalines;
01Jan23	    NY	04Feb23	 1
01Jan23	    NY	04Feb23	 1
01Jan23		NY	04Feb23	 1
01Jan23		NY	04Feb23	 1
02Jul23 	CA	04Jul23	 2
02Jul23 	CA	04Jul23	 2
02Jul23 	CA	04Jul23	 2
20Sep23		CA  30Sep23  3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;When I try&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s suggestion, obs 8 should be 11 (sep 20 to sep 30) but the output is 30 instead.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Scooby3g_0-1698930462539.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89333iCB5489E1EDD586DB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Scooby3g_0-1698930462539.png" alt="Scooby3g_0-1698930462539.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;When I tried&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;'s suggestion was missing the Year_month that i want the number of days to be attributed to. Is there a way to dedup to have just the below obs/row (obs 1, 2, 9, 12) along with the year_month the days are being attributed to?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Scooby3g_1-1698931303320.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89335iD2A1D43B813052B3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Scooby3g_1-1698931303320.png" alt="Scooby3g_1-1698931303320.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 14:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901232#M356164</guid>
      <dc:creator>Scooby3g</dc:creator>
      <dc:date>2023-11-02T14:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901240#M356168</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/359105"&gt;@Scooby3g&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;When I try&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s suggestion, obs 8 should be 11 (sep 20 to sep 30) but the output is 30 instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I tried&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;'s suggestion was missing the Year_month that i want the number of days to be attributed to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;Please describe exactly how "year_month" is missing?&lt;/P&gt;
&lt;P class="1698934695689"&gt;If you apply a format of YYMMD7 to date1 I think you will see the VALUE in the APPEARANCE you want.&lt;/P&gt;
&lt;P class="1698934695689"&gt;It is a bad idea to store anything that should actually be a date as other than a date value.&lt;/P&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="1698934695689"&gt;"dedupe" Proc Sort with the nodupkey . Or don't put repeated values into the process to begin with (sort BEFORE this step).&lt;/P&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="1698934695689"&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 14:27:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901240#M356168</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-11-02T14:27:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate days for each month using two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901256#M356174</link>
      <description>&lt;P&gt;I think I may have found what I needed in this post&amp;nbsp;&lt;A href="https://communities.sas.com/t5/New-SAS-User/Calculating-patietn-days-for-each-month-using-start-and-end/td-p/583954" target="_self"&gt;https://communities.sas.com/t5/New-SAS-User/Calculating-patietn-days-for-each-month-using-start-and-end/td-p/583954&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Scooby3g_0-1698939342531.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89337i924512F0A61AC2D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Scooby3g_0-1698939342531.png" alt="Scooby3g_0-1698939342531.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 15:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-days-for-each-month-using-two-dates/m-p/901256#M356174</guid>
      <dc:creator>Scooby3g</dc:creator>
      <dc:date>2023-11-02T15:35:53Z</dc:date>
    </item>
  </channel>
</rss>

