Use code to analyze return on investments

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

Use code to analyze return on investments

Hi guys,

If I place money in a fund that returns a compound interest of 4.25% annually. You

deposit $1,000 every year. How many years will it take to reach $30,000?

data bank;

  deposit=1000;

  do year=1 to 100 until (Total ge 30000);

  (Total+deposit)*1.0425;                          I want to add prior total with current year deposit and then multiple 1.0425.

end;                                                            However the parentheses don't work here.

run;         


Accepted Solutions
Solution
‎09-03-2014 10:01 PM
Respected Advisor
Posts: 4,801

Re: Use code to analyze return on investments

The SAVING function is for the simpler case where the compounding period is the same as the payment period. The period as the fourth argument is a missing value which signals to the SAVING function that the value to be returned is the number of periods. Any one of the arguments can be set to missing and the function will return its value from the other three (non missing).

For the flexibility that your example questions require, you are probably better off with 's approach.

PG

PG

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: Use code to analyze return on investments



Try this:

data want;

     do year=1 by 1 while (total lt 30000);

           total=1000*1.0425**year;

     end;

     year=year-1;

run;

Haikuo

Or mathematically straightforward:

data want;

year=(log(30000)-log(1000))/log(1.0425);

run;

Update: I read the question wrong. Please see 's answer. Sorry.

Contributor
Posts: 68

Re: Use code to analyze return on investments

Thank you all the same.

Regular Contributor
Posts: 233

Re: Use code to analyze return on investments

data bank;

do until (deposit ge 30000);
deposit + 1000;
  deposit + deposit*0.0425;
  year + 1;
  output;
end;
run;

proc print data=bank noobs;
title 'years until atleast $30,000';
run;

Capture.JPG

Contributor
Posts: 68

Re: Use code to analyze return on investments

Thanks Hima!

What if I don't use Do loop to calculate it. Is there any compound function that I can use?

Regular Contributor
Posts: 233

Re: Use code to analyze return on investments

You can use what Edward Ballard provided, its much better than what I provided and achieves what you are looking for in a simple function.

Super User
Posts: 11,101

Re: Use code to analyze return on investments

You may not have all of the financial functions depending on your SAS Install but this may help.

This is simple annual interest.

data _null_;                                                                                                                           

   rate = 0.0425;                                                                                                                      

   years= saving(30000, 1000, rate, .);                                                                                             

   put years= ;                                                                                                                     

run;                                                                                                                                   

Contributor
Posts: 68

Re: Use code to analyze return on investments

Amazing solution!

By the way, what's the point within the parentheses for?

years= saving(30000, 1000, rate, .);

Contributor
Posts: 68

Re: Use code to analyze return on investments

This solution is pretty smart.

However, I still couldn't apply this function flexibly even after reading SAS(R) 9.3 Functions and CALL Routines: Reference. I don't know the order of those arguments.

For example:

if I invest $1000 each year at the beginning, the rate is compounded quarterly, in which year and which quarter the balance will ge 30000?

if I invest $1000 each month for the first year, the rate is compounded quarterly, in which year and which quarter the balance will ge 30000?

if I invest $1000 each month for the first year, the rate is compounded monthly, in which year and which month the balance will ge 30000?

Thank you Ballard and look forward to your further help.

Solution
‎09-03-2014 10:01 PM
Respected Advisor
Posts: 4,801

Re: Use code to analyze return on investments

The SAVING function is for the simpler case where the compounding period is the same as the payment period. The period as the fourth argument is a missing value which signals to the SAVING function that the value to be returned is the number of periods. Any one of the arguments can be set to missing and the function will return its value from the other three (non missing).

For the flexibility that your example questions require, you are probably better off with 's approach.

PG

PG
Respected Advisor
Posts: 3,156

Re: Use code to analyze return on investments

Well, apparently is way smarter than me, but if you really want to get back to the basic, this is the summation of a geometric sequences (like I am preparing my final :-).

data _null_;

years=log(1-(30000/1000)*(1-1.0425))/log(1.0425);

put years= ;

run;

Haikuo

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 860 views
  • 11 likes
  • 5 in conversation