Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: question about calculating discounted sum


From   Robin Luo <rluosf@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: question about calculating discounted sum
Date   Sat, 19 Feb 2005 11:17:45 -0800

I need to calculate discounted sum for a variable in a panel data. My
data is like:

 FirmID      year     income       
   100        1980     50000
   100        1981     51000
   100        1982     54000
   100        1983     60000
   100        1984     59000
   100        1985     62000
   101        1970     18000
   101        1971     18500
   101        1972     20000
   101        1973     24000

Basically, the data is a panel data, grouped by "FirmID", with
hundreds of frims. What I need to do is to calculate a moving
discounted sum of income for each firm-year. That is, calculate a sum
of income from a firm's first year to the current year, but each
year's income will be discounted by that year's distance to the
current year and the discount rate is the inverse of the distance
"1/(current year - year +1)". For example, for firm 100 at 1982, this
calculation would be like: 54000 + 51000*1/2 +50000*1/3, while for
firm 100 at 1984 it would be like 59000 + 60000*1/2 + 54000*1/3 +
51000*1/4 + 50000*1/5. I tried the following program:

by FirmID: gen seq=_n

gen suminc=0

quietly forval i=1/`=seq' {
           by FirmID: replace suminc = suminc + income[`i']/(`=seq' - `i' +1)
           }          

But problems are: 1) the loop seems not allowing "by" command; 2) the
loop does not really launch, because even after I get rid of "by
FirmID" the loop seems to go only one cycle instead of "_n" cycles.

Many many thanks for any help!

Robin Luo
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index