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

Re: st: RE: question about calculating discounted sum


From   Ichiro Fujikake <[email protected]>
To   <[email protected]>
Subject   Re: st: RE: question about calculating discounted sum
Date   Mon, 21 Feb 2005 09:28:45 +0900

Hi Robin

I hope the following codes help you.

sort firmid year
by firmid: gen i=_n
qui sum i
local imax=r(max)
gen sum=income
forv j=1/`imax' {
    local h= `j'-1
    forv k=1/`j'-1 {
        by firmid: replace sum=sum+income[_n-`k']/(`k'+1) if i==`j'
    }
}

Ichiro


On 05.2.21 8:36, "Robin Luo" <[email protected]> wrote:

> Btw, Scott, concerning `=seq', do you have any idea about how to
> integrate a variable into "forvalue" loop?
> 
> Many thanks!
> 
> Robin
> 
> 
> On Sun, 20 Feb 2005 16:37:04 -0600, Scott Merryman <[email protected]>
> wrote:
>> How about attacking this problem directly:
>> 
>> bysort firm (year): gen num = _n
>> gen sum = 0
>> by firm: replace sum = income + income[1]/num[2] if num== 2
>> by firm: replace sum = income + income[1]/num[3] + income[2]/num[2] ///
>>         if num ==  3
>> by firm: replace sum = income + income[1]/num[4] + income[2]/num[3] + ///
>>         income[3]/num[2] if num ==  4
>> by firm: replace sum = income + income[1]/num[5] + income[2]/num[4] + ///
>>         income[3]/num[3] + income[4]/num[2] if num == 5
>> by firm: replace sum = income + income[1]/num[6] + income[2]/num[5] + ///
>> income[3]/num[4] + income[4]/num[3] + income[5]/num[2] if num == 6
>> 
>> Hope this helps,
>> 
>> Scott
>> 
>> P.S.  `=seq' is the same as seq[1] or 1
>> 
>>> -----Original Message-----
>>> From: [email protected] [mailto:owner-
>>> [email protected]] On Behalf Of Robin Luo
>>> Sent: Saturday, February 19, 2005 1:18 PM
>>> To: [email protected]
>>> Subject: st: question about calculating discounted sum
>>> 
>>> 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/
>> 
> *
> *   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/
> 


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index