Hi all!
How can I store in some locals the percentages shown by tab command?
Thank you in advance.
How can I store in some locals the percentages shown by tab command?
Thank you in advance.
. sysuse auto, clear (1978 Automobile Data) . tab foreign Car type | Freq. Percent Cum. ------------+----------------------------------- Domestic | 52 70.27 70.27 Foreign | 22 29.73 100.00 ------------+----------------------------------- Total | 74 100.00 . preserve . generate dummy = 1 . collapse (percent) dummy, by(foreign) . list, clean foreign dummy 1. Domestic 70.27027 2. Foreign 29.72973 . local pctd = dummy[1] . local pctf = dummy[2] . restore . display "domestic: `pctd' foreign: `pctf'" domestic: 70.27027027027027 foreign: 29.72972972972973 .
. sysuse auto, clear (1978 Automobile Data) . tab foreign, matcell(table) Car type | Freq. Percent Cum. ------------+----------------------------------- Domestic | 52 70.27 70.27 Foreign | 22 29.73 100.00 ------------+----------------------------------- Total | 74 100.00 . matrix list table table[2,1] c1 r1 52 r2 22 . return list scalars: r(N) = 74 r(r) = 2 . local obs = r(N) . local cat = r(r) . forvalues i=1/`cat' { 2. local dec`i' = table[`i',1] / `obs' 3. } . display "domestic: `dec1' foreign: `dec2'" domestic: .7027027027027027 foreign: .2972972972972973 .
Comment