Hello statalists,
I'm trying to loop some basic descriptive stats of age according to sex. Right now the matrix looks like this:
age[1,5]
with codes:
But I would like to have the matrix to look like this:
So I wrote the following codes:
I end up getting this error message: conformability error
r(503);
When I list the matrix, this is what I get:
Why am I getting this error? It seems to happen as soon as Stata is trying to run the second row. How do I fix it? I am using Stata 15.1 on a PC, thanks!
I'm trying to loop some basic descriptive stats of age according to sex. Right now the matrix looks like this:
age[1,5]
mu_men | sd_men | mu_women | sd_women | p | |
Age | 48.584151 | 22.583488 | 51.528371 | 22.524248 | . |
with codes:
matrix age = J(1,5,.)
matrix rownames age = "age"
matrix colnames age = "mu_men" "sd_men" "mu_women" "sd_women" "p"
tab1 sex if index==1
local j=1
local s=1 // s is for values of sex (1 = Men, 0 = Women)
while (`s'>=0){
quietly sum age if sex==`s' & index==1, detail
local mu=r(mean)
local sd=r(sd)
matrix age[1,`j']=`mu'
matrix age[1,`j'+1]=`sd'
local s=`s'-1
local j=`j'+2
}
matrix rownames age = "age"
matrix colnames age = "mu_men" "sd_men" "mu_women" "sd_women" "p"
tab1 sex if index==1
local j=1
local s=1 // s is for values of sex (1 = Men, 0 = Women)
while (`s'>=0){
quietly sum age if sex==`s' & index==1, detail
local mu=r(mean)
local sd=r(sd)
matrix age[1,`j']=`mu'
matrix age[1,`j'+1]=`sd'
local s=`s'-1
local j=`j'+2
}
mu | sd | |
men | ||
women |
matrix age = J(2,3,.)
matrix rownames age= "men" "women"
matrix colnames age= "mu" "sd" "p"
local s=1
while (`s'>=0) {
sum age if sex==`s' & index==1, detail
local mu=r(mean)
local sd=r(sd)
matrix age[`s',1]=`mu'
matrix age[`s',2]=`sd'
local s=`s'-1
}
matrix rownames age= "men" "women"
matrix colnames age= "mu" "sd" "p"
local s=1
while (`s'>=0) {
sum age if sex==`s' & index==1, detail
local mu=r(mean)
local sd=r(sd)
matrix age[`s',1]=`mu'
matrix age[`s',2]=`sd'
local s=`s'-1
}
I end up getting this error message: conformability error
r(503);
When I list the matrix, this is what I get:
mu | sd | p | |
men | 48.584151 | 22.583488 | . |
women | . | . | . |
Comment