Hello

I am struggling to store the results of ttest in matrix using loops. I have stname (string variable ) representing state and also state (numeric). I tried using both strings and numerics but not successful. i would like to have to state name also in the output matrix. Kindly help

Below is the code:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
foreach s in "Andhra Pradesh" "Karnataka" "Rajasthan" {

foreach v of varlist $metric_imp {
ds `v'
ds `s'
di `"`: var label `v''"'
capture noisily ttest `v' if stname==`s', by (sample)
*gen stname="Andhra Pradesh"
matrix ap_metric_t= (`s' r(N_1), r(N_2), r(p), r(p_l), r(p_u), r(t))
matrix rownames ap_metric_t= `v'
*matrix colnames AP_int4 = N1 N2 pscore pscorel pscoreu t
mat2txt, matrix(ap_metric_t) sav("census_metric_t.xlx") append
}

}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------