Epidemiology & Technology

Using the results of Stata Direct Standardization for display, HTML, putExcel etc

The dstdize command saves certain matrices and scalars. these can be used to access the results and programmatically write them to docx, excel, html files etc

dstdize biCat360 p  sex ageGrp  , by(q) using("allraabDistsWtsCalc_2.dta")  ///
level(95) format(%10.6f)  print // 

dstdize msvi p ageGrp if sex==1, by(sex) using(stdPop) print
local cases : di %-9.0g  el(r(crude),1,1) * el(r(Nobs),1,1)
local pop : di %-12.0g el(r(Nobs),1,1)
local crude: di %-5.2f el(r(crude),1,1)*100
local adj: di %-5.2f el(r(adj),1,1)*100
local lb_adj: di %-4.2f el(r(lb_adj),1,1)*100
local ub_adj: di %-4.2f el(r(ub_adj),1,1)*100
local se: di %-9.5f el(r(se),1,1)*100
di `" Population = `pop'  Cases = `cases'  Crude Prevalence = `crude'% "'
di `"  Standardized Prevalence = `adj'%  [`lb_adj',  `ub_adj'] "'


// sample function to calculate Directly standardized results and put values into 4 HTML table cells
// depends on: htput / htsummary / htlog etc commands. ;
// findit htput 

cap program drop dshtmlprint
program dshtmlprint
	local cases : di %-9.0g  el(r(crude),1,1) * el(r(Nobs),1,1)
	local pop : di %-12.0g el(r(Nobs),1,1)
	local crude: di %-5.2f el(r(crude),1,1)*100
	local adj: di %-5.2f el(r(adj),1,1)*100
	local lb_adj: di %-4.2f el(r(lb_adj),1,1)*100
	local ub_adj: di %-4.2f el(r(ub_adj),1,1)*100
	local se: di %-9.5f el(r(se),1,1)*100
	 //di `" Population = `pop'  Cases = `cases'  Crude Prev = `crude'% Adjusted Prev = `adj'% "'
	 //di `" lb_adj = `lb_adj'   ub_adj = `ub_adj'     se  = `se' "'
	local print: di `" <td>`pop' </td> <td>`cases'</td>  <td>`adj'</td><td>[`lb_adj', `ub_adj']</td> "'
	// di `" `print' "'
	htput `print'
end
Code language: PHP (php)

Example Usage

cap htclose
htopen using "Table 2b.html", replace
htput <style> 
htput table, td, th { border: 1px solid black; } 
htput table { min-width: 60%; border-collapse: collapse;}
htput tr:hover {background-color: #f5f5f5;}
htput th, td {text-align: left; padding: 4px;}
htput </style>


htput <h2> Age Standardized Cataract Prevalence (Column %) for each district</h2>
htput <table> 
htput <thead><tr> 
	htput <th> District Name  </th> 
	htput <th colspan="4"> Male </th> 
	htput <th colspan="4"> Female </th>  
	htput <th colspan="4"> All </th> 
htput </tr> </thead>

cls
// District wise 
levelsof areacode2, local(levels)
foreach l of local levels  {   
	local DistName : label(areacode2) `l'
	di as error _newline(1) "Working with `DistName'"

// restrict main data to that distritc
	use "allraabDistsWtsCalc_2.dta", clear
	keep if areacode2==`l'
	save "allraabDistsWtsCalc_2_`l'.dta", replace
// restrict Std population data to that distritc
	use "RAAB_Data_CSC_ageStd.dta", clear	
	keep if areacode2==`l'
	save "00 - RAAB_Data_CSC_1_`l'.dta", replace

	htput <tr> // Open new HTML table Row
        htput <td>`DistName'</td> // Distritc name in first column of row

	di as error _newline(1) "Males: Age - Standardized - `DistName'"
	dstdize biCat360 p ageGrp if sex==1 , by(areacode2) using("allraabDistsWtsCalc_2_`l'.dta")  level(95) format(%10.6f) print  // print
	dshtmlprint

	di as error _newline(1) "Females: Age - Standardized - `DistName'"
	dstdize biCat360 p ageGrp if sex==2 , by(areacode2) using("allraabDistsWtsCalc_2_`l'.dta")  level(95) format(%10.6f) print  // print
	dshtmlprint	

	di as error _newline(1) "Overall: Age - Sex Standardized - `DistName'"
	dstdize biCat360 p  sex ageGrp , by(areacode2) using("allraabDistsWtsCalc_2_`l'.dta")  level(95) format(%10.6f)  print // 
	dshtmlprint

htput </tr> // Close HTML table Row


// clean up
rm "allraabDistsWtsCalc_2_`l'.dta"
rm "00 - RAAB_Data_CSC_1_`l'.dta"
}		
		
htput </table> Code language: HTML, XML (xml)

Related posts