*! version 1.0.0 -- 01/21/93: STB-12: gr12 program define grmeanby version 3.0 local varlist "req ex" local if "opt" local in "opt" local weight "aweight fweight noprefix" local options "B2title(string) MEDian SUmmarize(string) Symbol(string) T1title(string) T2title(string) XSCale(string) XLAbel YLine(string) *" parse "`*'" if "`summari'"=="" { error 198 } parse "`summari'", parse(" ") if "`2'"!="" { error 198 } confirm var `1' _crcunab `summari' local summari "$S_1" if "`xlabel'"!="" { error 198 } /* xlabel not allowed */ if "`median'"=="" { local sumopts local sumres "_result(3)" local ttlhead "Means" } else { local sumopts ", detail" local sumres "_result(10)" local ttlhead "Medians" } tempvar touse x mean mean2 lbl grp strv id mine hi lo wgt quietly { gen `x' = . gen `mean' = . gen `mean2'= . gen str16 `lbl' = "" gen long `id'=_n parse "`varlist'", parse(" ") local ttl "`1'" local o 1 local i 1 gen byte `touse' = 1 `if' `in' if "`weight'"!="" { gen `wgt'=`exp' if `touse'==1 capture assert `wgt'>=0 if _rc { di in red "negative weights not allowed" exit 399 } replace `touse'=. if `wgt'==0 | `wgt'==. local weight "[`weight'=`wgt']" local ttlhead = "Weighted " + /* */ lower(substr("`ttlhead'",1,1)) + /* */ substr("`ttlhead'",2,.) } while "``i''"!="" { if `i'!=1 { local ttl "`ttl', ``i''" } egen `grp'=group(``i'') if `touse'==1 local t : type ``i'' if substr("`t'",1,3)=="str" { gen str8 `strv' = ``i'' } else { local t : value label ``i'' if "`t'"=="" { gen str10 `strv'=string(``i'') } else decode ``i'', gen(`strv') } sum `grp' local maxg=_result(6) local j=1 while `j'<=`maxg' { if `o'+1>_N { error 1001 } sum `summari' `weight' if `grp'==`j' `sumopts' replace `mean' = `sumres' if `id'==`o' replace `mean2'=`sumres' if `id'==`o'+1 replace `x'=`i' if `id'==`o' | `id'==`o'+1 gen `mine'=1 if `grp'==`j' sort `mine' replace `lbl'=`strv'[1] if `id'==`o'+1 drop `mine' local o=`o'+2 local j=`j'+1 } drop `strv' `grp' capture label drop `strv' local i=`i'+1 } local adj=.18*(`i'-1)/3 replace `x'=`x'-`adj' if `lbl'!="" sort `x' `mean' by `x': gen `lo' = `mean'[1] if `x'!=. replace `mean'=-`mean' sort `x' `mean' replace `mean'=-`mean' by `x': gen `hi' = `mean'[1] if `x'!=. sum `summari' `weight' if `touse'==1 `sumopts' local yli=`sumres' } if "`t1title'"=="" & "`t2title'"=="" { local t1 " " local t2 "`ttlhead' of `summari'" local t : variable label `summari' if "`t'"!="" { local t2 "`t2', `t'" } } else { if "`t2title'"=="" { local t1 " " local t2 "`t1title'" } else { local t1 "`t1title'" local t2 "`t2title'" } } if "`b2title'"=="" { local b2title "`ttl'" } if "`yline'"=="" { local yline "`yli'" } if "`xscale'"=="" { local losca=`i'-.5 local xscale ".5,`losca'" } if "`symbol'"=="" { local symbol "s(O..[`lbl'])" } else { local symbol = "s(" + substr("`symbol'",1,1)+"..[`lbl'])" } graph `mean' `hi' `lo' `mean2' `x', `symbol' c(.||.) trim(10) /* */ border xsca(`xscale') t2("`t2'") t1("`t1'") /* */ b2("`b2title'") yline(`yline') `options' end