# Rolling window and by-group statistics in Stata using asrol

asrol is a Stata program for rolling window and group statistics. It can be installed from ssc by:

ssc install asrol

After installation, you can read the help file by typing:

help asrol

asrol can be used for the following purposes. To know further details, click on the following links:

▬ Find the mean, standard deviation, and other statistics in a rolling window

▬ Find statistics excluding focal observation

▬ Find the cumulative product of stock returns / monthly cumulative stock returns

▬ Find geometric mean of stock returns in Stata

▬ Count number of firms that have paid dividends in a rolling window of 5 years

▬ Find the coefficient of variation in a rolling window of 5 years

▬ Find industry returns in each year / month

▬ Find portfolio returns in each year / month

▬ Find cumulative average / abnormal returns in event studies

▬ Find descriptive statistics in a recursive window

## 5 Comments

## Faisal Nawaz Mir

September 1, 2018at 4:26 pmI am very glad to see the code of arsol from our Pakistani author.

We are proud on you as one of us is contributing to finance field.

Adab!

Basad Ijz-o-Niaz

## eva huang

December 3, 2018at 7:49 amwhy can I not find the description page for ASM ?

when I click asm, it will direct to asrol command page.

## Attaullah Shah

December 3, 2018at 11:40 amEva Huange: Thanks for pointing out the incorrect url linking. We have correct it. Further, you can click on this link to directly access the asm page

https://fintechprofessor.com/stata-programs/asm-stata-program-to-construct-j-k-momentum-portfolios/

## David K Howe

March 27, 2019at 12:36 amDr. Attaullah Shah

Institute of Management Sciences, Peshawar, Pakistan

Dear Dr. Shah,

I believe I may have found a bug in the asrol command. I have enclosed a do

file and dataset showing the issues. The dataset is from Robert Shiller’s website.

Windows of width 180 and 360 do not create complete datasets when the gmean

option is used. Many missing values are produced.

I hope you find this feedback to be helpful. I truly appreciate the work you did

on this command.

Let me know if I can provide clarification. I am running Stata 15.1 on a Windows

10 machine.

Sincerely,

David K Howe

## Attaullah Shah

March 27, 2019at 10:17 amThank you David

This is not a bug in asrol, rather this is how asrol is supposed to work. The help file of asrol provides details on how the rolling window works.

Those details are reproduced bellow.

Rolling window calculationsThe default for the rolling window is to calculate required statistics on the available observation that are within the range. Therefore, the calculations of the required statistics start with one observation at the beginning of the rolling window. As we progress in the data set, the number of observations gradually increase until the maximum length of the rolling window is reached. Consider the following data of 10 observations, where X is the variable of interest for which we would like to calculate arithmetic mean in a rolling window of 5; and months is the rangevar. To understand the mechanics of the rolling window more clearly, we shall generate three additional statistics: count, first, and last.

ExplanationFor the first observation, that is 2016m10, the mean value is based on a single observation, as there are no previous data. The same is reflected by the variables count, first, and last. For the second observation, the mean value is based on two observations of X, i.e., (0.6881 + .9795) / 2 = .8338 . We can also observe such details from the variable count, that has a value of 2; variable first which shows that the first value in the rolling window this far is .6881 and last, which shows that the last value in the rolling window is .9795. As we move down the data points, the rolling window keeps on adding more observations until the fifth observation, i.e. 2017m2. After this observation, the observations at the start of the rolling window are dropped and more recent observations are added. It is pertinent to mention that users can limit the calculations of required statistics until minimum number of observations are available

The problem at hand

The dataset you have shared has the sdate variable that runs several years into the future where the rest of the data die at 2018m9. Since you have used the date variable sdate in the window option, asrol goes along the way with this variable until it reaches the end, that is 2023m12. The corresponding data variable spearnreal dies at sdate = 2018m9. From this point onwards, there are no current values of the spearnreal variable, however, its lags are still available in the periods beyond 2018m9, depending upon how long the window length is. In the case of window(sdate 360), enough lagged values of the spearnreal variable are present, therefore, there are values for the calculation of the geometric mean. If we use window(sdate 2), the calculation will stop just at sdate = 2018m9