Check out
capture
_rc
and various FAQs, including
http://www.stata.com/support/faqs/data/foreach.html
In fact, you might start with that FAQ.
More generally, there is much in the FAQs and
the Stata Journal that provides guidance in
this territory.
Nick
[email protected]
Yvonne Capstick
> Using Nick's helpful method for looping (which I initially
> required to
> generate proportions), I'm now trying to do looping
> regressions. What I'd
> like is the coefficient on regressions for the last 5 years,
> and to store these just like I was storing the proportions.
>
> My structure is permno year ret rp, where permno is a number which
> identifies each company uniquely.
>
> I initially try:
>
> gen brp = .
> gen bsmb = .
> gen bhml = .
>
> forval m = 0/4 {
> gen begin = year if mod(year,5) == `m'
> bysort permno (year): replace begin = begin[_n-1] if
> missing(begin)
> local i = 1990+`m'
> local j = 10107
> while `j' < 12000 {
> while `i' < 2005 {
> reg sret rp if permno == `j' & begin == `i'
> replace brp = _b[rp] if year == `i' + 5 &
> permno == `j' & month == 1 &
> month[_n-1] == 12
> local i = `i' + 5
> }
> local j = `j' + 1
> }
> drop begin
> }
>
> but, after doing everything successfully for permno 10007, it
> comes up with
> no observations for permno 10008. There is indeed no company
> with permno
> 10008 - but I would like it to skip to the next permno. Is
> there an option
> that asks Stata to skip and increment j once more if there are no
> observations?
>
> I tried a long-winded way of doing this using an 'if' function, i.e.
>
> forval m = 0/4 {
> gen begin = year if mod(year,5) == `m'
> bysort permno (year): replace begin = begin[_n-1] if
> missing(begin)
> local i = 1990+`m'
> local j = 10107
> while `j' < 12000 {
> qui gen temp = 1 if permno == `j'
> if sum(temp) == 0 {
> }
> else {
> while `i' < 2005 {
> reg sret rp if permno == `j' & begin == `i'
> replace brp = _b[rp] if year == `i' + 5
> & permno == `j' & month == 1 &
> month[_n-1] == 12
> local i = `i' + 5
> }
> }
> local j = `j' + 1
> local i = 1990+`m'
> drop temp
> }
> drop begin
> }
>
> but this didn't seem to increment either.
>
> Any advice would be much appreciated. In addition, is there
> an easier way of
> doing the whole thing without using 'while'? I tried something like:
>
> forval m = 0/4 {
> gen begin = year if mod(year,5) == `m'
> bysort permno (year): replace begin = begin[_n-1] if
> missing(begin)
> bysort (permno begin): reg sret rp
>
> but this seems to only run one regression.
>
*
* 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/