I've just installed Stata 18 and have been trying out a few things with the new -dtable- command. I'm sure I'll use this command frequently in my work and especially like the ease of automating and reproducing descriptive tables. Thanks Stata!!!
I do however have one question/wish.
Here's my code:
and the resulting output:
This is great when there aren't many variables. However, if there are many more variables, I prefer to display a single row for the binary variables, i.e. a single row for "History of hypertension" rather than 3 rows as shown above. I've attempted a work-around. Apart from only partially giving me what I want, it has introduced an undesirable consequence.
In my work-around I'm treating history of hypertension as a continuous variable (mean=percentage).
The resulting output is:
My questions are:
I'm aware my attempted work-around is convoluted. There may be a more straightforward way to do what I want that I missed when reading the help for -dtable-, in which case I will feel very silly.
Kind regards,
Suzanna
I do however have one question/wish.
Here's my code:
Code:
*Reading in the low birthweight dataset available on the Stata Press website.
use https://www.stata-press.com/data/r18/lbw,clear
*Adding value label to the low birthweight and hypertension variables.
lab def noyes 0 No 1 Yes
lab val low ht noyes
*Creating descriptive table.
dtable age i.race i.ht, by(low, nototals) ///
sample(, statistic(frequency) place(seplabels)) ///
sformat("n=%s" frequency) ///
nformat(%2.0f fvpercent mean) nformat(%3.1f sd)
and the resulting output:
Code:
----------------------------------------------
Birthweight<2500g
No Yes
n=130 n=59
----------------------------------------------
Age of mother 24 (5.6) 22 (4.5)
Race
White 73 (56%) 23 (39%)
Black 15 (12%) 11 (19%)
Other 42 (32%) 25 (42%)
Has history of hypertension
No 125 (96%) 52 (88%)
Yes 5 (4%) 7 (12%)
----------------------------------------------
In my work-around I'm treating history of hypertension as a continuous variable (mean=percentage).
Code:
*ht is a binary 0/1 variable. Multiplying by 100 so its mean is the same as percentage.
gen ht100=ht*100
lab var ht100 "History of hypertension"
*Creating descriptive table.
dtable age i.race ht100, by(low, nototals) ///
sample(, statistic(frequency) place(seplabels)) ///
sformat("n=%s" frequency) ///
nformat(%2.0f fvpercent mean) nformat(%3.1f sd) ///
define(htperc=mean) sformat("(%s%%)" htperc) ///
continuous(ht100, statistic(htperc))
Code:
-----------------------------------------------------
Birthweight<2500g
No Yes
n=130 n=59
-----------------------------------------------------
Age of mother 24 (5.6) (24%) 22 (4.5) (22%)
Race
White 73 (56%) 23 (39%)
Black 15 (12%) 11 (19%)
Other 42 (32%) 25 (42%)
History of hypertension 4 (4%) 12 (12%)
-----------------------------------------------------
- The percentage is repeated, first just the number and then in the specified format (#%). This is the case for both history of hypertension and mum's age (both treated as continuous). How do I tell Stata to display the mean only once within a cell? Can this be a different format for mum's age (showing just the mean, i.e. without (%)) vs history of hypertension (showing the mean with (%)).
- Is there a way to show the frequency of people with a history of hypertension?
Code:
----------------------------------------------
Birthweight<2500g
No Yes
n=130 n=59
----------------------------------------------
Age of mother 24 (5.6) 22 (4.5)
Race
White 73 (56%) 23 (39%)
Black 15 (12%) 11 (19%)
Other 42 (32%) 25 (42%)
History of hypertension 5 (4%) 7 (12%)
----------------------------------------------
I'm aware my attempted work-around is convoluted. There may be a more straightforward way to do what I want that I missed when reading the help for -dtable-, in which case I will feel very silly.
Kind regards,
Suzanna
Comment