Site icon Stata.Professor : Your Partner in Research

12 month rolling percentiles

Hi, I am trying to calculate all rolling percentiles of all panels in the past 12 months (excluding the current month) and then assign the current observation into the respective percentiles in the past 12 months. What would be the most efficient way to do it as I have 1.4 Million firm-month observations. Currently my code looks like this ( as an example):
foreach x in iv v {

gen P_`x'=.
forvalue i=0.01 (1) 0.99 {
	loc d=`i'*100
	asrol  `x', stat(median) perc(`i') window(month_id 12) xf(focal) gen(P`d')
		}
}
forvalue i=1 (1) 99 {
	replace P_`x'=0 if`x'<P1
	replace P_`x'=`i' if `x'>P`i' | `x'==P`i' & `x'<P`i'+1
	replace P_`x'=100 if`x'>P99 
	replace P_`x'=. if`x'==.
}
        forvalue i=1 (1) 99 {	
	  drop P`i'
        }
}
I have seen here on the website that the window option now features three arguments so w(n -13 -1) would also work but Stata does not allow it for me. Also, the loop stops somehow after P1 so there is a mistake with loc d. Thank you very much for your help. best, Mike
Exit mobile version