Dear Statalist,
I have several chi-square tests to be run on categorical variables, so to automate this process I am trying to take advantage of the data stored after the tabulate and sum commands to write those results to a tab delimited text file that can be opened in Excel. This works fine, except that depending on the variable (some are dichotomous, some have three or four categories) there are zeros for some rows. Rather than remove the zeros in the resultant Excel file, is there a way to only display the results for the non-missing value labels of each variable only? The code used within the write command is given below. Columns are written for N0 _var__num, N1_var_num and total_var_num (num representing each row of the categorical variable) and p. I am new to this, so there is probably a more sophisticated way of getting the same result. Thanks.
foreach var of varlist Cat1 Cat2 Cat3 {
qui tab `var' Obese, chi2 row
local p=r(p)
foreach num of numlist 0/4 {
foreach i of numlist 0/1 {
local forlab: value label `var'
local label: label `forlab' `num'
qui sum Obese if `var' ==`num'
matrix N`i'_`num' =r(N)
svmat N`i'_`num' , names(N`i'_`var'`num')
}
qui sum Obese if `var' ==`num'
matrix total_`num' = r(N)
svmat total_`num' , names(total_`var'`num')
local cat=`num'+1
I have several chi-square tests to be run on categorical variables, so to automate this process I am trying to take advantage of the data stored after the tabulate and sum commands to write those results to a tab delimited text file that can be opened in Excel. This works fine, except that depending on the variable (some are dichotomous, some have three or four categories) there are zeros for some rows. Rather than remove the zeros in the resultant Excel file, is there a way to only display the results for the non-missing value labels of each variable only? The code used within the write command is given below. Columns are written for N0 _var__num, N1_var_num and total_var_num (num representing each row of the categorical variable) and p. I am new to this, so there is probably a more sophisticated way of getting the same result. Thanks.
foreach var of varlist Cat1 Cat2 Cat3 {
qui tab `var' Obese, chi2 row
local p=r(p)
foreach num of numlist 0/4 {
foreach i of numlist 0/1 {
local forlab: value label `var'
local label: label `forlab' `num'
qui sum Obese if `var' ==`num'
matrix N`i'_`num' =r(N)
svmat N`i'_`num' , names(N`i'_`var'`num')
}
qui sum Obese if `var' ==`num'
matrix total_`num' = r(N)
svmat total_`num' , names(total_`var'`num')
local cat=`num'+1
Comment