Dear All, In an earlier post (https://www.statalist.org/forums/for...fter-rangestat), I asked how to compute the measure of financial statement comparability. Thanks to Robert Picard, who offered a helpful code in doing this (using runby & rangerunm, both from SSC). However, due to the large dataset (using all listed A shares in China over the 1991-2018 year), it took more than 10 hours (according to my friend) and never ends! I just wonder if the code from Robert can be speeded up somehow. Any suggestions are highly appreciated. The following is taken from #4 of the above link (by Robert):
Code:
clear all
set seed 3123

* demonstration dataset, 50 firms over 40 quarters in 10 industry
set obs 50
gen firmid = _n
gen industry = runiformint(1,10)
expand 70
bysort firmid: gen qdate = yq(1999,4) + _n
format %tq qdate
gen returns = runiform()
gen earnings = runiform()

* pick a quarter to calculate measure, use quarters in 2 previous years
gen q2use = quarter(dofq(qdate)) == 4
gen qlow  = cond(q2use, qdate - 11, 1)
gen qhigh = cond(q2use, qdate - 4, 0)
format %tq qlow qhigh

program get_CompAcct
    reg earnings returns
    predict pearn, xb
    reg earnings2 returns2
    gen pearn2 =  _b[returns2] * returns + _b[_cons]
    count if !mi(pearn,pearn2)
    
    gen CompAcct_nobs = r(N)
    gen CompAcct = -sum(abs(pearn-pearn2)) / CompAcct_nobs
    drop pearn pearn2
end

program pair_by_quarters
    tempfile hold
    save "`hold'"
    rename (firmid returns earnings) (firmid2 returns2 earnings2)
    joinby qdate using "`hold'"
    keep if firmid != firmid2
    sort firmid firmid2 qdate
    rangerun get_CompAcct, by(firmid firmid2) interval(qdate qlow qhigh)
end
runby pair_by_quarters, by(industry) verbose

save "results.dta", replace

sort industry qdate firmid firmid2

* to install, type: ssc install listsome
listsome industry qdate firmid firmid2 CompAcct_nobs CompAcct ///
    if q2use & !mi(CompAcct), sepby(qdate)