# Re: st: Programming question

 From Cameron Hooper To statalist@hsphsun2.harvard.edu Subject Re: st: Programming question Date Fri, 04 Mar 2005 16:36:15 -0500

Hi

I agree that rollreg is probably the best solution. I started with Kit Baum's routine and just got to wondering if I could produce a quick and dirty solution that would work for my data. Since I knew nothing about programming in Stata, the dirty aspect has been realized but not the quick! Still, its been a good learning exercise and thanks to the replies I've received from people on this list I've learnt a lot in a short period of time.

Cheers,

Cameron

Nick Cox wrote:

Frankly, I think you will be better off starting with -rollreg-, notwithstanding the apparent quirk that Kit Baum is currently looking at.
assumes that your identifiers are all present within a sequence;
calculates the SD of residuals even though that is a result left behind by -regress-;
wires in your own particular variable names;
makes presumptions about the sort order of observations.
None of these is necessarily fatal, but the whole means that this program may not bear very much pressure.
Rolling regressions is a little tricky as a Stata programming problem. Even if it weren't it would make sense to start from Kit's program as the best that's visible in Stataland.
Nick n.j.cox@durham.ac.uk
Cameron Hooper

```David Kantor wrote:

```
```At 12:00 PM 3/4/2005 -0500, Cameron Hooper wrote:

```
A novice programming question.
forvalues i = 1(1)<number of companies> {
for values j = 1(1)<number of observations for company `i'> {
<do stuff>
}
}

If you tell us more about what you want to do, then we may
find that you

really don't need to do all that explicit looping.
Experience has shown

```that it is very rare that you really need such looping.

```
David Harrison suggested:

summ id, meanonly
forvalues i = 1/`r(max)' {
qui count if id==`i'
forvalues j = 1/`r(N)' {
<do stuff>
}
}

Which does what I asked for (thanks David). However my ultimate goal is more complex than my original question suggested and upon using David's code I've seen a new difficulty. (I'm learning as I go!) Here is my full problem.

I want to estimate rolling regressions on a firm by firm basis. Then for each regression I want to find the standard deviation of the residuals. Something like:

program rstdres
version 8.0
syntax varlist , window(int) gen(string)
generate `gen' = .
tempvar resid
sum id, meanonly
quietly{
for values i = 1/'r(max)' {
count if id==`i'
while `b' < `r(N)' {
regress y x in `a'/`b' if id == `i' /// See comment below
cap drop `resid'
predict `resid' if e(sample), resid
summarize `resid'
replace `gen' = r(sd) in `b'
local a = `a' + 1
local b = `b' + 1
}
}}

My problem now is getting `a' and `b'. At the moment that are just placeholders in the code. They represent the rolling window. I don't know how to set them properly. Here is how they should behave:

id x y
1 1 5
1 2 6
1 4 2
2 2 5
2 8 9
2 6 1
2 2 4
2 4 9

When id == 2 I and `window' = 3 then I want a and b to be:

Iteration
1 a = 4 b = 6
2 a = 5 b = 7
3 a = 6 b = 8

Can you suggest how to achieve this. Or am I approaching the entire problem in the wrong way?
```
*
*   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/

```
--
Cameron Hooper <chooper@umich.edu>
Ross School of Business at the University of Michigan
701 Tappan St., Ann Arbor, MI 48109
phone: 734-615-4178 fax: 734-936-0282
Public Key: http://wwwkeys.pgp.net:11371/pks/lookup?op=index&search=0xFCCF8C91
*
* 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/