Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Lance Erickson <lance_erickson@byu.edu> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: RE: Calculating compound interest using loops? |

Date |
Tue, 10 Jul 2012 19:29:12 +0000 |

Thanks. The payment is a constant throughout the life of the loan in this case and is taken directly from the dataset. Did you mean the line . local interest = 0 isn't necessary? There may be another way to do it, but I included that because without it the line . gen int`i' = (principal - (payment*`j') + `interest') * rate evaluates in the first instance to . gen int1 = (principal - (payment*0) + ) * rate If you don't define the local interest with a value it returns a blank which causes a syntax error. An initial value of 0 solves the syntax problem and still provides a correct calculation. Lance -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Karen Zhang Sent: Tuesday, July 10, 2012 12:51 PM To: statalist@hsphsun2.harvard.edu Subject: Re: st: RE: Calculating compound interest using loops? Neat loop! The only thing I an puzzled when I read the thread is that don't we need to local the initial payment or payment _i in the first loop? Just my opinion:) thanks! Hongyu (Karen) Zhang 510.861.5581 hongyuzhang@berkeley.edu Class of 2012 | University of California, Berkeley B.A. Economics, B.A. Applied Mathematics On Jul 9, 2012, at 2:42 PM, Lance Erickson <lance_erickson@byu.edu> wrote: > Hi Sara, > > Something like this (below) should give you a more efficient version of the code you had. > > Best, > Lance > > > // create test data > clear > input id principal rate payment > 1 200000 .005 1199.1 > end > > // get amount of monthly interest > local interest = 0 > forvalues i = 1/360 { > local j = `i' - 1 > gen int`i' = (principal - (payment*`j') + `interest') * rate > local interest "`interest' + int`i'" > } > > // get amount of yearly interest > local l = 1 > foreach k of numlist 12(12)360 { > local m = `k' - 11 > egen int_y`l' = rowtotal(int`m'-int`k') > local ++l > } > > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu > [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Sara > Kimberlin > Sent: Monday, July 09, 2012 1:23 PM > To: statalist@hsphsun2.harvard.edu > Subject: st: Calculating compound interest using loops? > > Hello, > > I'm trying to calculate annual interest payments on a 30-year loan, with interest compounding monthly, and can't figure out how to write the code efficiently. > > Pre-defined variables include principal, rate, and monthly_payment. I know I can manually calculate the amount of interest for each monthly period like this: > > gen interest_month1 = principal * rate > > gen interest_month2 = (principal - (monthly_payment * 1) + > interest_month1) * rate > > gen interest_month3 = (principal - (monthly_payment * 2) + > interest_month1 + interest_month2) * rate > > gen interest_month4 = (principal - (monthly_payment * 3) + > interest_month1 + interest_month2 + interest_month3) * rate > > ... and so on through interest_month360. > > And then I can manually add up the monthly interest to calculate the annual interest for each year, e.g.: > > gen interest_year1 = interest_month1 + interest_month2... + > interest_month12 > > But I think there must be a way to write this much more efficiently using loops, and I haven't been able to figure it out. I'd appreciate any help. > > Thank you, > Sara > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**st: Calculating compound interest using loops?***From:*Sara Kimberlin <skimberlin@gmail.com>

**st: RE: Calculating compound interest using loops?***From:*Lance Erickson <lance_erickson@byu.edu>

**Re: st: RE: Calculating compound interest using loops?***From:*Karen Zhang <mathkaren@me.com>

- Prev by Date:
**Re: st: data import/varname problem** - Next by Date:
**Re: st: data import/varname problem** - Previous by thread:
**Re: st: RE: Calculating compound interest using loops?** - Next by thread:
**st: name conflict in posting a matrix** - Index(es):