Hi all. I am trying to extract 4 columns from the results. The code below was designed for a binary variable Gender (1 male & 2 female). However, I am trying to modify it so that I can use it for a categorical variable race ( 1 black, 2 white, 3 asian, 4 colored). May you please assist on how I can use it for a categorical variable with more than 2 groups?
COMMAND:
local i = 1
foreach var of global adult_spns_66{
svy: tab `var', col ci
matrix prop = e(Prop)
local rownum = 1
local colstart = 1
local colend = 1
local rows = rowsof(prop)
foreach x in "`colstart'" "`colend'"{
foreach num of numlist 1/`rows'{
matrix sum`num'_`x' = prop[`num',`x']
}
foreach num of numlist 1/`rows'{
if `num' == 1{
matrix sumtotal_`x' = sum`num'_`x'
}
else{
matrix sumtotal_`x' = sumtotal_`x' + sum`num'_`x'
}
}
}
matrix temp1 = prop[`rownum',`colstart']
matrix temp1 = temp1/trace(sumtotal_`colstart')
matrix temp2 = prop[`rownum',`colend']
matrix temp2 = temp2/trace(sumtotal_`colend')
matrix prop`var' = (trace(temp1))
matrix rownames prop`var' = "`var'"
if `i' ==1{
matrix results = prop`var'
}
else{
matrix results = (results\prop`var')
}
local ++i
}
matrix list results
COMMAND:
local i = 1
foreach var of global adult_spns_66{
svy: tab `var', col ci
matrix prop = e(Prop)
local rownum = 1
local colstart = 1
local colend = 1
local rows = rowsof(prop)
foreach x in "`colstart'" "`colend'"{
foreach num of numlist 1/`rows'{
matrix sum`num'_`x' = prop[`num',`x']
}
foreach num of numlist 1/`rows'{
if `num' == 1{
matrix sumtotal_`x' = sum`num'_`x'
}
else{
matrix sumtotal_`x' = sumtotal_`x' + sum`num'_`x'
}
}
}
matrix temp1 = prop[`rownum',`colstart']
matrix temp1 = temp1/trace(sumtotal_`colstart')
matrix temp2 = prop[`rownum',`colend']
matrix temp2 = temp2/trace(sumtotal_`colend')
matrix prop`var' = (trace(temp1))
matrix rownames prop`var' = "`var'"
if `i' ==1{
matrix results = prop`var'
}
else{
matrix results = (results\prop`var')
}
local ++i
}
matrix list results
Comment