Hi,

I have a panel dataset of multiple firms (gvkeys) across multiple quarters.

I am trying to get gvkey- and quarter-specific AR(1) predictions for three focal variables (revenue_a, epssurp, marketshare) and save them as variables. In other words, I am trying to predict marketshare of gvkey i at quarter t using marketshare of gvkey i at quarter t-1.

My dataset looks like this:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long gvkey float(quarter revenue_a epssurp marketshare)
1045 188  .1980463 .  51.84315
1045 189 .21362144 .  7.200194
1045 190   .238256 .  9.752722
1045 191  .2172393 . 10.783447
1045 192  .1973652 .  9.241472
1045 193  .2025437 .  8.251666
1045 194  .1990759 . 8.9912815
1045 195 .19899364 .  49.23114
1045 196 .19855045 .  25.69328
1045 197  .2192003 .  49.48811
end
format %tq quarter
I can think of two approaches: (1) using rolling3 with foreach loop or (2) using rangerun and a customized program. I prefer the latter for the unprecedented time-efficiency of rangerun. However, my code for the approach (2) does not seem to do the job.

Just so you know, my very slow yet working code based on approach (1) is as follows:

Code:
levelsof gvkey, local(gvkeylev)
foreach x of varlist revenue_a epssurp marketshare {
    foreach i of local gvkeylev {
        rolling3 _b _se , window(20) step(1) predict p(yhat) saving(`i'.dta, replace) : arima `x', arima(1,0,0), if gvkey==`i'
        }
}
The code I want to make work is as follows:

Code:
program myprog, rclass
levelsof gvkey, local(gvkeylev)
    foreach i of local gvkeylev {
        if gvkey == `i' {
            arima revenue_a, arima(1,0,0)
           predict rhat, y
gen pred_rev = rhat
        }
end
rangerun myprog, interval(quarter -20 -1) use(revenue_a) by(permno)

*** To be repeated for other two variables
I guess I will need to edit my program, but I am a bit lost.
Please throw me some ideas to accomplish the goal more efficiently! Thank you for your help in advance.