* Coefficient of Variation * [ sd / mean] sysuse auto * To calculate a Coefficient of Variation for all * observations summarize price display r(sd)/r(mean) tabstat price, stat(cv) * For groups of observations tabstat price, stat(mean sd cv) by(foreign) collapse (mean) mu=price (sd) s=price, by(foreign) generate covprice = s/mu merge 1:m foreign using "C:\Program Files (x86)\Stata12\auto" tab covprice * A solution that does not require saving a data set levelsof foreign, local(flevels) generate covprice2 = . foreach f of local flevels { quietly: tabstat price if foreign==`f', stat(cv) save matrix cv = r(StatTotal) replace covprice2 = cv[1,1] if foreign==`f' } tab covprice2 * A solution that requires fewer passes through the data levelsof foreign, local(flevels) local nlevels: word count `flevels' generate covprice2 = . tabstat price, stat(cv) by(foreign) save forvalues i = 1/`nlevels' { tokenize `flevels' matrix cv = r(Stat`i') replace covprice2 = cv[1,1] if foreign==``i'' } tab covprice2