asrol : Rolling window and by-groups statistics in Stata

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


Faisal Nawaz Mir

September 1, 2018at 4:26 pm

I 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.
Basad Ijz-o-Niaz

eva huang

December 3, 2018at 7:49 am

why can I not find the description page for ASM ?
when I click asm, it will direct to asrol command page.

David K Howe

March 27, 2019at 12:36 am

Dr. 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.


David K Howe

    Attaullah Shah

    March 27, 2019at 10:17 am

    Thank 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 calculations
    The 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.

    bys id: asrol X, window(months 5) stat(count) gen(count)
            bys id: asrol X, window(months 5) stat(mean) gen(mean)
            bys id: asrol X, window(months 5) stat(first) gen(first)
            bys id: asrol X, window(months 5) stat(last) gen(last)
              | id    months        X    mean   count   first     last |
              |  1   2016m10    .6881   .6881       1   .6881    .6881 |
              |  1   2016m11    .9795   .8338       2   .6881    .9795 |
              |  1   2016m12    .6702   .7792       3   .6881    .6702 |
              |  1    2017m1    .5949   .7331       4   .6881    .5949 |
              |  1    2017m2    .7971   .7459       5   .6881    .7971 |
              |  1    2017m3    .7836    .765       5   .9795    .7836 |
              |  1    2017m4    .6546   .7001       5   .6702    .6546 |
              |  1    2017m5    .0968   .5854       5   .5949    .9689 |
              |  1    2017m6    .6885   .6041       5   .7971    .6885 |
              |  1    2017m7    .8725   .6192       5   .7836    .8725 |

    For 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

Leave a Reply

twenty − 10 =