I am looking for a way to find 94th percentile in a recursive window. My goal is to find  the the 94th percentile of all previous observations of the variable “value” for each firm.


asrol can find percentiles in a rolling window or in a recursive window. To show its use, I generate a dataset of 50,000 observations, where there are 100 firms’ ids and 500 time periods.

*Generate a dummy dataset
 loc time = 500
 loc panel = 100
 local obs=`time' * `panel'
 set obs `obs'
 gen id=mod(_n,`panel')+1
 bys id: gen year=_n
 gen value =uniform()

 *Use asrol to find the given percentile
 bys id: asrol value, stat(median) perc(.94) window(year -100000 -1) gen(Perc94)


bys id: The calculations are performed for each firm separately

asrol value: asrol is the command name and value is the variable for which the percentiles are calculated

stat(median) perc(.94) : These two options are used together for find percentiles. So when option stat(median) is used, the default of perc(.5) is used. In all other cases, option perc() needs to be used.

window(year -100000 -1) : This set the window for including observation in range. The first element of the window is year, which is a variable in the dataset. -100000 is the length of the back window, it is larger enough that include all previous observation. The final value is -1, that will exclude the current observation from the calculation.

gen(Perc94): This option is used to generate a new variable Perc94, it will contain the percentile values.