Hello all:
I have this program to do single tabulations of frequency variables (as fractions, numbers and percentages) to use in putdocx. I was fiddling around trying to make the program better and found the code is now not exiting the inner loop and coming to do the math on the second variable that program is calling. The ereturn is only getting me the last variable in the program call. I can see both variables evaluating correctly in set trace but missing in the ereturn. Loop brackets and incrementing macro seem to be right. Have a reproducible example using the nhanes2 dataset.
The return
I have this program to do single tabulations of frequency variables (as fractions, numbers and percentages) to use in putdocx. I was fiddling around trying to make the program better and found the code is now not exiting the inner loop and coming to do the math on the second variable that program is calling. The ereturn is only getting me the last variable in the program call. I can see both variables evaluating correctly in set trace but missing in the ereturn. Loop brackets and incrementing macro seem to be right. Have a reproducible example using the nhanes2 dataset.
Code:
*! d1tab.ado v1.1 GV 29nov2023-editing *! d1tab.ado v1.0 GV 7nov2023 webuse nhanes2, clear // Program d1tab below Single tabulations fractions and percentages. *-------------------------------------------------------------------- cap program drop d1tab program d1tab, eclass version 18.0 syntax varlist(numeric min=1) [if] [, TEX(string asis)] [TEX2(string asis)] [TEX3(string asis)] marksample touse set trace on * Define all factor variables local fs foreach v of local varlist { qui levelsof `v' if(`touse'), local(mylev) if `r(r)' > 10 { di as error "More than 10 levels; Check if continuous" } local maxlev = `r(r)'+1 qui estpost tab `v' if(`touse') qui matmap e(pct) P`v', map(round(@, 0.1)) qui matmap e(b) B`v', map(round(@, 0.1)) local `v'_tot `=B`v'[1,`maxlev']' eret local `v'_tot = "``v'_tot'" local j 0 foreach lev of local mylev{ local ++j local `v'_`lev'n `=B`v'[1,`j']' eret local `v'_`lev'n `=B`v'[1,`j']' local `v'_`lev'pct : di %3.1f `=P`v'[1,`j']' eret local `v'_`lev'p = string(`=P`v'[1,`j']', "%3.1f") + "%" eret local `v'_`lev'pn ``v'_`lev'pct'% (n = ``v'_`lev'n') eret local `v'_`lev'np ``v'_`lev'n' (``v'_`lev'pct'%) eret local `v'_`lev'fn "``v'_`lev'n'/``v'_tot'" eret local `v'_`lev'fp "``v'_`lev'n'/``v'_tot' (``v'_`lev'pct'%)" eret local `v'_`lev'pf "``v'_`lev'pct'% (``v'_`lev'n'/``v'_tot')" } cap matrix drop _all } * Display header text *--------------------- di as text "{hline 65}" di as text " Tabulated variables {c |}" _column(20) as result " `varlist'" di as text "{c +}"_dup(64) "-" eret li end d1tab diabetes sex di " There are `e(diabetes_1pn)' comprising `e(diabetes_1fn)' individuals with `e(sex_2n)' females with poor health."
Code:
macros: e(sex_2pf) : "52.5% (5434/10349)" e(sex_2fp) : "5434/10349 (52.5%)" e(sex_2fn) : "5434/10349" e(sex_2np) : "5434 (52.5%)" e(sex_2pn) : "52.5% (n = 5434)" e(sex_2p) : "52.5%" e(sex_2n) : "5434" e(sex_1pf) : "47.5% (4915/10349)" e(sex_1fp) : "4915/10349 (47.5%)" e(sex_1fn) : "4915/10349" e(sex_1np) : "4915 (47.5%)" e(sex_1pn) : "47.5% (n = 4915)" e(sex_1p) : "47.5%" e(sex_1n) : "4915" e(sex_tot) : "10349" e(cmd) : "estpost" e(subcmd) : "tabulate" e(depvar) : "sex" e(properties) : "b"
Comment