# 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

## 13 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

## Attaullah Shah

May 22, 2019at 5:37 pmDear 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

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

## Simon

May 4, 2019at 3:12 pmDear 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.

Regards,

Simon

## Attaullah Shah

May 5, 2019at 1:38 amSimonPresently, asrol does not support these statistics.

## Anonymous

May 5, 2019at 5:42 pmMany Thanks Professor

## Yazidu Ustarz

December 14, 2019at 7:49 amDear 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 amYazidu Ustarz

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

## Yazidu Ustarz

December 14, 2019at 8:32 amThank 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:

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

## Attaullah Shah

December 14, 2019at 8:34 amI 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.

like

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