Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Alter the window in the rolling command

 From "S.A.J.van Vijfeijken" To statalist@hsphsun2.harvard.edu Subject st: Alter the window in the rolling command Date Mon, 30 May 2011 15:46:26 +0200

```Dear Stata users,

I run into the following problem. I have a dataset on which I want to perform a rolling calculation of a standard deviation and skewness on a regression residual called epsilon. Epsilon is available in the dataset that is used.

The data looks as follows:

id          time            epsilon
1           31jan2001     0.05
1           28feb2001     -.05
1           31mar2001    0.02
2           31jan2001     -0.01
2           28feb20012    0.01
2           31mar2001    -.01

and so on. The data points are all gathered between end of January 2001 and the end of december 2010, but not every id has 10 years of data, some have less and start somewhere during the time span.

I want to calculate a rolling sd and skewness, taking into account the 48 months before the date (t-4years).

The code I use is:

rolling sd= r(sd) skewness= r(skewness),window (48) clear: summarize epsilon, detail

and it works, however by doing so the output gives a rolling SD and skewness starting at end 2004m12. This is what the rolling command tells it to do. However, by doing so I loose the first four years of data.

My question is how to prevent this. I want Stata to calculate the standard deviation and skewness also when there are less than 48 months available. So, for 2001m3 I want it to take the SD and skewness of the three months available, and for 2004m11 for the 47 months available.

I was hoping you could give me some help on how to code this, how to make the rolling window calculate an SD and skewness also when less than 48 points are available.

Suggestions are most welkom.

Bas van Vijfeijken

s.a.j.vanvijfeijken@uvt.nl

*
*   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/
```