<?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: Calculate days within a given range of years in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488864#M127487</link>
    <description>&lt;P&gt;Play around with min() and max() functions. Note that there never is a 31st of November, and 2016 was a leap year. And the 2nd of February is always the 32nd day of a year.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let year1=2015;
%let year2=2016;

data have;
input id start :date9. end :date9.;
format start end date9.;
cards;
1 23AUG2014 13DEC2017
2 01JAN2015 30NOV2015
3 01JAN2015 01FEB2016
;
run;

data want;
set have;
year1_count = max(min(end,"31dec&amp;amp;year1"d) - max(start,"01jan&amp;amp;year1"d) + 1,0);
year2_count = max(min(end,"31dec&amp;amp;year2"d) - max(start,"01jan&amp;amp;year2"d) + 1,0);
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                                year1_    year2_
id        start          end     count     count

 1    23AUG2014    13DEC2017      365       366 
 2    01JAN2015    30NOV2015      334         0 
 3    01JAN2015    01FEB2016      365        32 
&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Aug 2018 13:15:22 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-08-22T13:15:22Z</dc:date>
    <item>
      <title>Calculate days within a given range of years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488857#M127482</link>
      <description>&lt;P&gt;I have a dataset with a unique ID, a start date, and an end date. I'm trying to calculate the number of days within a given year that the dates fall within.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Year 1 = 01JAN2015 to 31Dec2015&lt;/P&gt;&lt;P&gt;Year 2 = 01JAN2016 to 31DEC2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Start&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;END&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 23AUG2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13DEC2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31NOV2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01FEB2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I would like to create:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Start&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;END&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;year1_count&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; year2_count&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 23AUG2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13DEC2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 365&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 365&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31NOV2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 335&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01FEB2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;365&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&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 test;
    input ID startdate date9. enddate date9.;
    format startdate enddate date9.;
    datalines;

    1 23AUG2014  13DEC2017
    2 01JAN2015   30NOV2015
    3 01JAN2015   01FEB2016
    ;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Aug 2018 13:42:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488857#M127482</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2018-08-22T13:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days within a given range of years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488863#M127486</link>
      <description>&lt;P&gt;Not tested (post test data in the form of a datastep!):&lt;/P&gt;
&lt;PRE&gt;data inter;
  set have;
  do i=year(start) to year(end);
    days=365;
    output;
  end;
  tmp=ymd(year(end),month(start),day(start));
  i+1;
  days=intnx('days',tmp,end);
  output;
run;

proc transpose data=inter out=want;
  by id start end;
  var days;
  id i;
  idlabel i;
run;
    &lt;/PRE&gt;
&lt;P&gt;So effectively output a row for year diffs, then one row for day diff, then transpose.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Aug 2018 13:13:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488863#M127486</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-22T13:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days within a given range of years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488864#M127487</link>
      <description>&lt;P&gt;Play around with min() and max() functions. Note that there never is a 31st of November, and 2016 was a leap year. And the 2nd of February is always the 32nd day of a year.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let year1=2015;
%let year2=2016;

data have;
input id start :date9. end :date9.;
format start end date9.;
cards;
1 23AUG2014 13DEC2017
2 01JAN2015 30NOV2015
3 01JAN2015 01FEB2016
;
run;

data want;
set have;
year1_count = max(min(end,"31dec&amp;amp;year1"d) - max(start,"01jan&amp;amp;year1"d) + 1,0);
year2_count = max(min(end,"31dec&amp;amp;year2"d) - max(start,"01jan&amp;amp;year2"d) + 1,0);
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                                year1_    year2_
id        start          end     count     count

 1    23AUG2014    13DEC2017      365       366 
 2    01JAN2015    30NOV2015      334         0 
 3    01JAN2015    01FEB2016      365        32 
&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Aug 2018 13:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488864#M127487</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-22T13:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days within a given range of years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488866#M127489</link>
      <description>&lt;P&gt;A better solution is of course this (building from my previous example data):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
do year = year(start) to year(end);
  count = max(min(end,mdy(12,31,year)) - max(start,mdy(1,1,year)) + 1,0);
  output;
end;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id        start          end    year    count

 1    23AUG2014    13DEC2017    2014     131 
 1    23AUG2014    13DEC2017    2015     365 
 1    23AUG2014    13DEC2017    2016     366 
 1    23AUG2014    13DEC2017    2017     347 
 2    01JAN2015    30NOV2015    2015     334 
 3    01JAN2015    01FEB2016    2015     365 
 3    01JAN2015    01FEB2016    2016      32 
&lt;/PRE&gt;
&lt;P&gt;Now you have data (years) as data, and not in structure (column names).&lt;/P&gt;</description>
      <pubDate>Wed, 22 Aug 2018 13:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488866#M127489</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-22T13:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days within a given range of years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488873#M127493</link>
      <description>Edited the question to make it easier (datastep). Thanks for your answer&lt;BR /&gt;</description>
      <pubDate>Wed, 22 Aug 2018 13:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-days-within-a-given-range-of-years/m-p/488873#M127493</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2018-08-22T13:50:26Z</dc:date>
    </item>
  </channel>
</rss>

