<?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 extract duration per year from a multi-year time period? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476195#M122512</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  TOTAL_VALUE   (START_DATE   END_DATE) (:yymmdd10.);
format START_DATE   END_DATE yymmdd10.;
cards;
1   1000          15-11-04     17-09-30
2   3000          16-11-29     17-12-15
3   5000          15-12-08     16-01-11
;
data temp;
 set have;
 range=end_date-start_date;
 do date=start_date to end_date;
  year=year(date);
  output;
 end;
 format date yymmdd10.;
run;
proc summary data=temp;
by id total_value range year;
output out=temp1;
run;
data want;
 set temp1;
 value=total_value*_freq_/range;
 keep id year value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 07 Jul 2018 10:47:07 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-07-07T10:47:07Z</dc:date>
    <item>
      <title>How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476154#M122505</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example of data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID&amp;nbsp; TOTAL_VALUE&amp;nbsp; &amp;nbsp;START_DATE&amp;nbsp; &amp;nbsp;END_DATE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15-11-04&amp;nbsp; &amp;nbsp; &amp;nbsp;17-09-30&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp;3000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 16-11-29&amp;nbsp; &amp;nbsp; &amp;nbsp;17-12-15&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3&amp;nbsp; &amp;nbsp;5000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15-12-08&amp;nbsp; &amp;nbsp; &amp;nbsp;16-01-11&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;(all dates are in SAS date)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I would like to get pro rata transaction value for each year involved. For example, for ID1, the transaction starts in Nov 2015 and ends in Sep 2017. It has 2 months in 2015, 12 months in 2016, and 9 months in 2017. So for 2015, the assigned value = 2/23 * 1000, for 2016, 12/23 * 1000, for 2017, 9/23 * 1000.&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;My desired output:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID&amp;nbsp; YEAR&amp;nbsp; &amp;nbsp; VALUE&amp;nbsp; &amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; 86.96&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp;2016&amp;nbsp; &amp;nbsp; 521.73&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; 391.30&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp;2016&amp;nbsp; &amp;nbsp; 230.77&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; 2769.23&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3&amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; 2500.00&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3&amp;nbsp; &amp;nbsp;2016&amp;nbsp; &amp;nbsp; 2500.00&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Calculation in terms of number of days, or round down to number of months as I have shown in the example are both acceptable.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I'm very new to SAS, and I can only think of extracting years from&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;start_date&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;end_date&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and then create an array of years out of it. But I can't figure out how to do the prorata part. Any thoughts would help. Thank you!&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jul 2018 20:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476154#M122505</guid>
      <dc:creator>mssun</dc:creator>
      <dc:date>2018-07-06T20:55:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476160#M122506</link>
      <description>&lt;P&gt;I'm afraid my result doesn;t seem to match your ID2,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you correct the logic and see where i am wrong&lt;/P&gt;&lt;P&gt;For ID2, my logic computes&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;2016&amp;nbsp; 2/14*3000&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;2017 12/14*3000&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  TOTAL_VALUE   (START_DATE   END_DATE) (:yymmdd10.);
format START_DATE   END_DATE yymmdd10.;
cards;
1   1000          15-11-04     17-09-30
2   3000          16-11-29     17-12-15
3   5000          15-12-08     16-01-11
;
data want;
if _n_=1 then do;
 dcl hash H (ordered: "A", multidata:'y') ;
   h.definekey  ("id") ;
   h.definedata ('k','month') ;
   h.definedone () ;
end;
set have;
_month=0;
do k=year(start_date) to year(end_date);
if k ne year(end_date) then do;
month=intck('month',start_date,intnx('year',start_date,0,'e'))+1;
h.add();
_month+month;
start_date=intnx('year',start_date,1,'b');
end;
else if k=year(end_date) then do; month=intck('month',mdy(1,1,k),end_date)+1;
h.add();
_month+month;
end;
end;
do while(h.do_over(key:id) eq 0);
value=month/_month*TOTAL_VALUE;
output;
end;
keep id k value;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS Output&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Obs ID k value1234567 &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;TD&gt;86.96&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;521.74&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;391.30&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;428.57&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;2571.43&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;TD&gt;2500.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;2500.00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 06 Jul 2018 21:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476160#M122506</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-06T21:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476175#M122507</link>
      <description>&lt;P&gt;Unless I misunderstand the OP request, that is way too much code.&amp;nbsp; I believe a single loop will do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID  TOTAL_VALUE   (START_DATE   END_DATE) (:yymmdd10.);
  format START_DATE   END_DATE yymmdd10.;
cards;
1   1000          15-11-04     17-09-30
2   3000          16-11-29     17-12-15
3   5000          15-12-08     16-01-11
;
data want (drop=_:);
  set have;
  _ndays=1+end_date-start_date;
  do year=year(start_date) to year(end_date);
    value=  total_value*(1+ min(end_date,mdy(12,31,year))-max(start_date,mdy(1,1,year)) )/_ndays;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The above is in days.&amp;nbsp; If you want months (where each month in a range counts as a whole month):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=_:);
  set have;
  _nmonths=1+intck('month',start_date,end_date);

  do year=year(start_date) to year(end_date);
    value= total_value
         *(1+intck('month',max(start_date,mdy(1,1,year)),min(end_date,mdy(12,31,year))))
         /_nmonths;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited additional note: this problem fits nicely in sas base programming forum, where it would&amp;nbsp;get the widest audience.&lt;/P&gt;</description>
      <pubDate>Sat, 07 Jul 2018 00:44:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476175#M122507</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-07T00:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476182#M122508</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;Thank you sir for helping me learn so much and your points have always been amazing, not just this thread. Paying attention to your points, I cleaned my code a little bit, though yours is classy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  TOTAL_VALUE   (START_DATE   END_DATE) (:yymmdd10.);
format START_DATE   END_DATE yymmdd10.;
cards;
1   1000          15-11-04     17-09-30
2   3000          16-11-29     17-12-15
3   5000          15-12-08     16-01-11
;
data want;
set have;
_month=intck('month',start_date,end_Date)+1; 
do year=year(start_date) to year(end_date);
if year=year(start_date) then months=intck('month',start_date,intnx('year',start_date,0,'e'))+1; 
else if year=year(end_date) then months=intck('month',intnx('year',end_date,0,'b'),end_date)+1; 
else months=12;
value=months/_month*TOTAL_VALUE;
output;
end;
keep id year value;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
_month=intck('month',start_date,end_Date)+1; 
do year=year(start_date) to year(end_date);
if year=year(start_date) then months=12-month(start_date)+1; 
else if year=year(end_date) then months=month(end_date); 
else months=12;
value=months/_month*TOTAL_VALUE;
output;
end;
keep id year value;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Jul 2018 03:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476182#M122508</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-07T03:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476195#M122512</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  TOTAL_VALUE   (START_DATE   END_DATE) (:yymmdd10.);
format START_DATE   END_DATE yymmdd10.;
cards;
1   1000          15-11-04     17-09-30
2   3000          16-11-29     17-12-15
3   5000          15-12-08     16-01-11
;
data temp;
 set have;
 range=end_date-start_date;
 do date=start_date to end_date;
  year=year(date);
  output;
 end;
 format date yymmdd10.;
run;
proc summary data=temp;
by id total_value range year;
output out=temp1;
run;
data want;
 set temp1;
 value=total_value*_freq_/range;
 keep id year value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Jul 2018 10:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476195#M122512</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-07T10:47:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476263#M122533</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code makes the logic clearer to the viewer than my dense single line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it assumes that all intervals are truly multi-year.&amp;nbsp; If the interval starts and ends in the same year and is less than 12 months, you'll need to make some changes.&amp;nbsp; Otherwise, the title of this discussion better be 100% correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Jul 2018 05:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476263#M122533</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-08T05:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476440#M122617</link>
      <description>Thank you! Your solution is elegant and works like a charm, especially in handling single-year situation.&lt;BR /&gt;I'll also take a look at sas base programming forum. Thanks!</description>
      <pubDate>Mon, 09 Jul 2018 13:21:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476440#M122617</guid>
      <dc:creator>mssun</dc:creator>
      <dc:date>2018-07-09T13:21:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476462#M122627</link>
      <description>Hi! Your solution works for multi-year situation, but doesn't work for duration less than one year. Sorry that I didn't make it clear in description.&lt;BR /&gt;I took a second thought on number of months - for example, if end date is Oct 1, the entire Oct would be taken into account in your code, but I excluded Oct when I eyeballed the data. That's why our results differ. Your logic would work perfectly if we both counted number of days.&lt;BR /&gt;Thank you for the answer!</description>
      <pubDate>Mon, 09 Jul 2018 14:08:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476462#M122627</guid>
      <dc:creator>mssun</dc:creator>
      <dc:date>2018-07-09T14:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract duration per year from a multi-year time period?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476472#M122633</link>
      <description>Hi! I think your solution works too, but mkeintz answered earlier and his/her solution runs faster so I accepted that one. Thank you for answering!</description>
      <pubDate>Mon, 09 Jul 2018 14:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-duration-per-year-from-a-multi-year-time-period/m-p/476472#M122633</guid>
      <dc:creator>mssun</dc:creator>
      <dc:date>2018-07-09T14:36:23Z</dc:date>
    </item>
  </channel>
</rss>

