Hello Statalist,


I would like to create some code which can calculate various statistics (moving average, moving standard deviation, perhaps others as well) for each unit in my panel data. I will use the moving average to explain the problem. It would be fairly straightforward to calculate a moving average for each unit in the panel if I could specify a fixed period of time over which the average was to be calculated (i.e if I could say for each unit, calculate a 3 month moving average for example). However, I would like this period to vary according to the characteristics of each unit's data series in such a way that the fewer the positive, non-zero observations in the series for a given unit, the longer the period over which the average is to be taken will be. I would like to do the same for the standard deviation. The two will be calculated for the same periods within a single unit. Both calculations will be trailing.

My data is monthly. I have calculated the number of months over which the moving average should be calculated for each unit and I have generated a variable which holds this information. At the moment, I would like the number of months in the moving average to be time invariant but unit variant. Later, I may also need the period to be time variant as well so it would be extra useful if any one has a solution that is capable of integrating this additional level of flexibility.


Thank you very much for reading this, your help will be very much appreciated!

Charlie