I have three variables called Sex (coded as male/female), HTN (yes/no) and Cancer (yes/no).
I want to creat a MS Word table with relative frequencies of all variables within cancer.
My syntax is as follows:
Code:
clear
input Sex HTN Cancer n
0 0 0 20
0 1 0 20
0 0 1 15
0 1 1 20
1 0 0 20
1 1 0 20
1 1 1 25
1 0 1 20
end
expand n
label define dsex 0 "Male" 1 "Female"
label values Sex dsex
label define dyn 0 "No" 1 "Yes"
label values HTN Cancer dyn
putdocx clear
putdocx begin
** Two rows (one for each variable), three columns (one for variable name and one for each category within cancer)
putdocx table Table1 = (2,3)
foreach v of varlist Sex HTN {
forvalues n = 1/2 {
tabulate `v' Cancer, matcell(`v')
putdocx table Table1(`n',1)=("`v'")
putdocx table Table1(`n',2)=(`v'[2,1]*100/(`v'[2,1]+`v'[1,1]))
putdocx table Table1(`n',3)=(`v'[2,2]*100/(`v'[2,2]+`v'[1,2]))
}
}
putdocx save Table1, replace| HTN | 50 | 56.25 |
| HTN | 50 | 56.25 |
May anyone give some ideas?
Thank you!
0 Response to Putdocx and looping
Post a Comment