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

    Attaullah Shah

    May 22, 2019at 5:37 pm

    Dear David K Howe

    Turns out that the reported issue was not specifically a bug in the asrol, rather it was caused by the Stata limitation of storing larger values. Since the geometric mean involves multiplication of the values in the range, Stata will throw an error when we multiply numbers that are outside the range of acceptable limit. I have found an alternative method of finding gmean. You can download this new version by

    net install asrol, from( replace

    Please note that you have to write the above line in full


May 4, 2019at 3:12 pm

Dear Prof. Attaullah,

I am trying to use asrol command for computing rolling skewness and kurtosis. Does asrol support this computation or any other of your program supports it.

Many thanks for your help.



    Attaullah Shah

    May 5, 2019at 1:38 am

    Presently, asrol does not support these statistics.


May 5, 2019at 5:42 pm

Many Thanks Professor

Yazidu Ustarz

December 14, 2019at 7:49 am

Dear Professor Attaullah Shah.

I hope my email finds you well, Please my name is Yazidu Ustarz, a PhD. Candidate at the Stellenbosch University, South Africa. I used the asrol command to calculate rolling window normalise standard deviation. I found it very useful. I however have a series that contain some negative values and when I apply the command some of the values turned to be more than 100%. I realised that it is not able to use the absolute value but rather treat the negative values lower.

Please is there a solution to this problem.

    Attaullah Shah

    December 14, 2019at 8:30 am

    Yazidu Ustarz
    Please share your dataset and the asrol command so that I may understand the problem?

Yazidu Ustarz

December 14, 2019at 8:32 am

Thank you very much, Professor, Please find attached the data, which is a foreign direct investment as a percent of gdp. Below is the command I used:

bys country: asrol fdi, stat(max) wind(year 3) 

gen new = 100 * fdi/max 

bys country: asrol new, stat(count sd) wind(year 3)

I calculated a 3-year rolling window normalize standard deviation.

    Attaullah Shah

    December 14, 2019at 8:34 am

    I do not see any problem with the asrol calculation. If you want to use the negative as absolute values, then first convert them to absolute.


    gen abs_fd = abs(fdi)
    bys country: asrol abs_fd, stat(max) wind(year 3)
     gen new = 100 * fdi/max 
     bys country: asrol new, stat(count sd) wind(year 3)

    I have not used this approach to sd, so cannot tell the theoratical justification of doing so.

Leave a Reply