<?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: Solve equations based on values across observations: compute IRR after each cash flow in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698824#M213746</link>
    <description>I see. Since we have a date for each cash flow, the IRR function should take care of timing issues and give us an annulized rate, regardless of how many years are involved.</description>
    <pubDate>Fri, 13 Nov 2020 21:49:43 GMT</pubDate>
    <dc:creator>xyxu</dc:creator>
    <dc:date>2020-11-13T21:49:43Z</dc:date>
    <item>
      <title>Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698407#M213587</link>
      <description>&lt;P&gt;I have a dataset that includes each project's initial date and initial cost, as well as the date and amount of cash flows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input project $1. date_initial :yymmdd10. date_cf :yymmdd10. cf cost_initial 5.;
	format date_initial date_cf yymmdd10.;
	datalines;
A 2004-08-25 2005-03-23 3 30
A 2004-08-25 2005-06-20 4 30
A 2004-08-25 2005-09-25 6 30
A 2004-08-25 2005-12-23 4 30
A 2004-08-25 2006-04-05 7 30
A 2004-08-25 2006-07-05 8 30
B 2007-06-27 2008-01-22 4 45
B 2007-06-27 2008-04-21 6 45
B 2007-06-27 2008-07-21 7 45
B 2007-06-27 2008-10-20 8 45
B 2007-06-27 2009-01-20 9 45
B 2007-06-27 2009-04-20 10 45
B 2007-06-27 2009-07-20 12 45
B 2007-06-27 2009-11-05 11 45
B 2007-06-27 2010-02-22 9 45
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to compute the internal rate of return (IRR) for each project and each observation. That is, after each cash flow, I want an updated IRR at the cash flow date. The IRR at the date of cash flow n is the solution of&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="xyxu_0-1605196832600.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51610i3C27B309830888E6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="xyxu_0-1605196832600.png" alt="xyxu_0-1605196832600.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;where t is the time between the initial date and the date of a cash flow. For example, the IRR for the 2nd observation should solve&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="xyxu_0-1605215471073.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51620i16029DA283C7BF11/image-size/medium?v=v2&amp;amp;px=400" role="button" title="xyxu_0-1605215471073.png" alt="xyxu_0-1605215471073.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;and&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;t1 = intck('day', '25aug2004'd, '23mar2005'd) / 365

t2&amp;nbsp;=&amp;nbsp;intck('day', '25aug2004'd, '30jun2005'd) / 365&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;As the cash flow dates are not strictly quarterly, SAS IRR function (which requires a constant frequency) does not seem to work in this case.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 21:13:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698407#M213587</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-11-12T21:13:55Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698532#M213641</link>
      <description>Added more details for the problem. Any suggestions?</description>
      <pubDate>Thu, 12 Nov 2020 21:14:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698532#M213641</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-11-12T21:14:43Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698617#M213678</link>
      <description>&lt;P&gt;There is a finance function that matches your need.&lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0xrqvy0hix04on1ol882q71xl0e.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0xrqvy0hix04on1ol882q71xl0e.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;However, I'm more curious as to how you want your output to be like.&lt;BR /&gt;Is it detailed in the query?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2020 10:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698617#M213678</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-11-13T10:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698680#M213693</link>
      <description>&lt;P&gt;I want the output to be similar to dataset "have", but includes an additional variable as the IRR computed for each observation, using all cash flows to the project_id up to the date_cf.&lt;BR /&gt;&lt;BR /&gt;To use the finance function, it seems that I need to create many variables that contain the value of previous cash flows, so that I can call the function for every observation. One concern is that each observation corresponds to different number of cash flows. Is there any good way to achieve this?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2020 14:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698680#M213693</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-11-13T14:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698688#M213697</link>
      <description>&lt;P&gt;&amp;gt;I want the output to be similar to dataset "have", but includes an additional variable as the IRR computed for each observation, &amp;gt;using all cash flows to the project_id up to the date_cf&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on your query, below might be what you are expecting but I can't tell since there is no firm description of how you want your output to be like.&lt;BR /&gt;I hope you clarify it first.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile datalines dlm="09"x;
input project $1. date_initial :yymmdd10. date_cf :yymmdd10. cf cost_initial r;
datalines;
1	A	2004-08-25	2005-03-23	3	30	-0.981723006	
2	A	2004-08-25	2005-06-20	4	30	-0.864252017	
3	A	2004-08-25	2005-09-25	6	30	-0.603330209	
4	A	2004-08-25	2005-12-23	4	30	-0.430594664	
5	A	2004-08-25	2006-04-05	7	30	-0.171907236	
6	A	2004-08-25	2006-07-05	8	30	0.0493847285	
7	B	2007-06-27	2008-01-22	4	45	-0.985403284	
8	B	2007-06-27	2008-04-21	6	45	-0.870849089	
9	B	2007-06-27	2008-07-21	7	45	-0.667472842	
10	B	2007-06-27	2008-10-20	8	45	-0.435187718	
11	B	2007-06-27	2009-01-20	9	45	-0.212882285	
12	B	2007-06-27	2009-04-20	10	45	-0.017025432	
13	B	2007-06-27	2009-07-20	12	45	0.1624599799	
14	B	2007-06-27	2009-11-05	11	45	0.2868471891	
15	B	2007-06-27	2010-02-22	9	45	0.3659270159	
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;gt;To use the finance function, it seems that I need to create many variables that contain the value of previous cash flows, so that &amp;gt;I can call the function for every observation. One concern is that each observation corresponds to different number of cash &amp;gt;flows. Is there any good way to achieve this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One idea would be to use array statement to generate multiple variables and do running irr calculation for every observation in a do loop along with _N_ automatic variable.&lt;BR /&gt;Again, this is a sample code based on my understanding stated above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input project $1. date_initial :yymmdd10. date_cf :yymmdd10. cf cost_initial 5.;
	format date_initial date_cf yymmdd10.;
	datalines;
A 2004-08-25 2005-03-23 3 30
A 2004-08-25 2005-06-20 4 30
A 2004-08-25 2005-09-25 6 30
A 2004-08-25 2005-12-23 4 30
A 2004-08-25 2006-04-05 7 30
A 2004-08-25 2006-07-05 8 30
B 2007-06-27 2008-01-22 4 45
B 2007-06-27 2008-04-21 6 45
B 2007-06-27 2008-07-21 7 45
B 2007-06-27 2008-10-20 8 45
B 2007-06-27 2009-01-20 9 45
B 2007-06-27 2009-04-20 10 45
B 2007-06-27 2009-07-20 12 45
B 2007-06-27 2009-11-05 11 45
B 2007-06-27 2010-02-22 9 45
;
run;

data want;
	do _N_=1 by 1 until(last.project);
		set have;
		by project;
		*generate date_cf by observation;
		array _date_cf[365] ;
		*generate _cf by observation;
		array _cf[365];
		*assign date_cf variable value one by one by observation;
		_date_cf[_N_]=date_cf;
		*assign _cf variable value one by one by observation;
		_cf[_N_]=cf;
		*assign 0 if variables have missing values;
		do i=1 to dim(_date_cf);
			if missing(_date_cf[i]) then _date_cf[i]=0;
			if missing(_cf[i]) then _cf[i]=0;
		end;
		*calculate irr;
		r_byproject=finance('xirr', -cost_initial, of _cf[*], date_initial, of _date_cf[*]);
		output;
	end;
	drop i _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Nov 2020 15:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698688#M213697</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-11-13T15:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698746#M213724</link>
      <description>&lt;P&gt;This works!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is the number 365 arbitrarily chosen, just to be large enough?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2020 18:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698746#M213724</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-11-13T18:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698823#M213745</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&amp;gt;Is the number 365 arbitrarily chosen, just to be large enough?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;It is chosen.I thought there might be a situation to generate variables for a whole year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2020 21:41:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698823#M213745</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-11-13T21:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: Solve equations based on values across observations: compute IRR after each cash flow</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698824#M213746</link>
      <description>I see. Since we have a date for each cash flow, the IRR function should take care of timing issues and give us an annulized rate, regardless of how many years are involved.</description>
      <pubDate>Fri, 13 Nov 2020 21:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Solve-equations-based-on-values-across-observations-compute-IRR/m-p/698824#M213746</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-11-13T21:49:43Z</dc:date>
    </item>
  </channel>
</rss>

