Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Table output, how to get percent of row total?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   Re: st: Table output, how to get percent of row total?
Date   Tue, 7 Oct 2003 21:38:26 +0100

Daniel R Sabath
 
> I need to create a table which contains percent of row 
> total and frequency.
> I've been looking through table and tabdisp and haven't 
> figured it out.
> 
> The table I am looking for is very similar to the following. From
> http://www.biomedcentral.com/1472-6963/3/4/table/T1
> ------------------------------------------------------------
> Characteristics of diabetic subjects according smoking 
> status. Figures are
> frequencies (percent of row total).
> 
> 			Never smoked (968)	Current smoker (296)
> Ex-smoker (582)	All	
> 						
> 						
> Sex	Men		374 (40)			166 (18)
> 361 (39)		928	
> 	Women		594 (61)			130 (13)
> 221 (23)		971	
> Age Group (Years)						
> 	< 45		117 (53)			60 (27)
> 42 (19)		219	
> 	45-54		126 (57)			49 (22)
> 45 (20)		221	
> 	55-64		281 (54)			78 (15)
> 151 (29)		520	
> 	65-74		263 (47)			78 (14)
> 200 (36)		556	
> 	? 75		158 (48)			25 (8)
> 134 (41)		326	
> 	Not known	23 (40)			6 (11)		
> 	10
> (18)		57	

You can get frequencies and row totals easily 
from -tabulate-, by e.g. 

. sysuse auto 
. tab for rep78, row 

but the format is not close to that here. 

There may be a command somewhere to do it this 
way, but an alternative is to do your own by 
constructing a _string_ variable with 
contents 

"<cell frequency> (<rounded row percent>)" 

Here is a hack to do something like that: 

program oct7, sortpreserve byable(recall)  
	version 8 
	syntax varlist(max=2) [if] [in] 
	marksample touse, strok 
	tokenize `varlist' 
	args row col 
	tempvar freq percent show 

	qui { 
		bysort `touse' `row' `col' : gen `freq' = _N
		by `touse' `row' : ///
		gen `percent' = string(round(100 * (`freq') / _N)) 
		replace `percent' = "(" + `percent' + ")" 
		replace `percent' = " " + `percent' if length(`percent') == 3
		if "`col'" == "" gen `show' = `freq' 
		else gen `show' = string(`freq') + " " + `percent' 
	}

	if "`col'" == "" label var `show' "Freq."

	tabdisp `row' `col' if `touse', c(`show') 
end 	

and here are some examples: 

* one variable, row total will always be 100% 

. oct7 foreign

----------------------
 Car type |      Freq.
----------+-----------
 Domestic |         52
  Foreign |         22
----------------------

* two variables, more interesting 

. oct7 foreign rep78

-------------------------------------------------------
          |             Repair Record 1978             
 Car type |       1        2        3        4        5
----------+--------------------------------------------
 Domestic |  2  (4)   8 (17)  27 (56)   9 (19)   2  (4)
  Foreign |                    3 (14)   9 (43)   9 (43)
-------------------------------------------------------

* can be done -by <varlist>- 

. gen himpg = mpg > 20 

. bysort himpg : oct7 foreign rep78

________________________________________________________
-> himpg = 0

-------------------------------------------------------
          |             Repair Record 1978             
 Car type |       1        2        3        4        5
----------+--------------------------------------------
 Domestic |  1  (3)   5 (16)  20 (63)   6 (19)         
  Foreign |                                     4 (100)
-------------------------------------------------------

______________________________________________________
-> himpg = 1

--------------------------------------------------
          |           Repair Record 1978          
 Car type |      1       2       3       4       5
----------+---------------------------------------
 Domestic | 1  (6)  3 (19)  7 (44)  3 (19)  2 (13)
  Foreign |                 3 (18)  9 (53)  5 (29)
--------------------------------------------------

Nick 
[email protected] 


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index