program invpctile, rclass
version 8.2
syntax varname [if] [in], value(real) [nq(integer 100)]
tempvar pcts
* local nq 100
marksample touse
pctile `pcts' = `varlist' if `touse', nq(`nq')
qui replace `pcts' in 1/`nq' = abs(`pcts' - `value')
su `pcts' in 1/`nq', meanonly
qui replace `pcts' in 1/`nq' = cond(`pcts' == r(min),_n,.)
su `pcts' in 1/`nq',meanonly
return scalar pctile = r(mean)
end
. webuse dow1, clear
. invpctile dowclose, value(700)
. return list
scalars:
r(pctile) = 28
If you pctile dowclose, nq(100) you will see that a Dow of 700 is
closest to the 28th percentile.