statat-test

Distinguishing between several t-tests


I am running a loop in Stata for various t-tests. The code I am using is:

ds s_a1_1 s_a1_4a s_a2 s_b3 s_b4 s_b5 s_b6 s_c7 s_c8 s_c9 

global teach_vars_detail `r(varlist)'

ds m7saq10 m3sdq3_tsup m3sdq6_tsup m7sfq9_pknw_filter urban_rural 

global teach_vars_basic `r(varlist)'

foreach var of varlist $teach_vars_detail teach_literacy_score teach_numeracy_score {
    
    foreach x of varlist $teach_vars_basic {
        
         ttest `var' if unique_teach_id!="" , by(`x')

    }
}

I am getting the results I want but since the t-tests are for so many iterations, I was wondering if there was a way to identify from the result which variable was the t-test run on and which was the by variable in that particular result.

Two-sample t test with equal variances
------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
---------+--------------------------------------------------------------------
       0 |      98    3.622449    .0513141    .5079838    3.520605    3.724293
       1 |      98    3.673469    .0518448    .5132374    3.570572    3.776367
---------+--------------------------------------------------------------------
Combined |     196    3.647959    .0364249    .5099481    3.576122    3.719796
---------+--------------------------------------------------------------------
    diff |           -.0510204    .0729453               -.1948881    .0928473
------------------------------------------------------------------------------
    diff = mean(0) - mean(1)                                      t =  -0.6994
H0: diff = 0                                     Degrees of freedom =      194

    Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
 Pr(T < t) = 0.2426         Pr(|T| > |t|) = 0.4851          Pr(T > t) = 0.7574

Solution

  • Insert a command within your inner loop before the ttest command, such as

    display "`var', by(`x')"