Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Balance tests in a loop?

    Hello.

    I am currently trying to run balance tests in a dataset with respondents from 8 countries. The country variables are binary depending on whether the respondent is treated or not. I use the command below in a loop to run the balance tests one by one, and it works as it should. However, I was curious if it is possible to make a loop for the 8 countries at once and not for country_1, country_2 etc., separately. Do you know if that is possible?

    Thank you in advance!

    Code:
        loc covar co1 co2 co3 co4
        loc x country_1
    
        capture mat drop A
        loc i = 1
        foreach var of varlist `covar' {
        ttest `var', by(`x') unequal
            if `i' == 1 mat A =  `r(mu_1)',  `r(mu_2)',  `r(mu_2)'-`r(mu_1)',`r(p)' 
            else mat A =  A \ `r(mu_1)',  `r(mu_2)', `r(mu_2)'-`r(mu_1)', `r(p)'
            loc i = `i' + 1
            loc name  `name'  `var'
            }
        mat rownames A = `name'
    
        levelsof `x', local(VL)
        loc fff
        foreach num of loc VL {
        loc ddd : label (`x') `num'
        loc fff `fff' "`ddd'" 
        }
        mat colnames A = `fff' "Difference"  "p-value"
        mat list A
        estout matrix(A,fmt(3)) , mlabels(none)

  • #2
    I'm not understanding. Please give a data example using dataex.

    Also, what is Stata doing that you don't want it to, or what do you want it to do that it isn't doing?

    Comment


    • #3
      I'm new in here and not familiar with dataex yet, sorry. To simplify my question: is it possible to run the command above for all 8 countries at once using a loop instead of running the command for each country seperately as I am doing now?

      Comment

      Working...
      X