Others have given positive advice.
A remaining question is: Why didn't Hua's code work? (I don't understand the uncertainty implied by "doesn't seem to work", as this code contains illegal statements.)
Here are some of the reasons:
1. The line
gen Cret(`f',`d') = ...
is illegal. You can't specify subscripts on the LHS of a -generate- statement. Even if you could they wouldn't be matrix style and they would be based on [] and not ().
2. Writing
gen Cret = ...
inside the loop will fail because the variable already exists.
3. Hua's basic approach is to -generate- a variable at the start of the loop and then to -drop- the same variable at the end. No progress could be made that way.
Nick
[email protected]
Hua Pan
I want to loop over firms and days, in order to calculate cumulative daily return for each firm and from day 1 to day 30. That means,
firm day ret Cret
1 1 x1 x1
1 2 x2 (1+x1)*(1+x2)-1
… … … …
1 30 x30 (1+x1)*(1+x2)*…(1+x30)-1
2 1
… …
2 30
..
firm and day have numeric format.
I tried:
sort firm day
generate Cret = 0
levelsof firm, local(firms)
levelsof day, local(days)
foreach f of local firms {
foreach d of local days {
gen Cret(`f’, `d’) = (1+Cret)*(1+ret)-1 if firm == `f' & day == `d'
replace Cret = Cret(`f’, `d’) if firm == `f' & day >= `d'
drop Cret(`f’, `d’)
}
}
But it doesn’t seem to work. Have anyone idea about cumulative return or just cumulative sum for each firm and everyday, or just looping of this kind?
Any help and suggestion would be greatly appreciated. Thank you very much in advance for your time.
*
* 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/