Announcement

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

  • Export ttest output to MS Excel

    Hello,

    I am running several two independent sample t-tests and I would like to export the output directly to MS excel in a table that would look like the table below.

    Anybody could help? Thanks in advance.

    Silvia


    The command to run multiple ttest at the same time:

    foreach var of varlist h_worked net_inc {
    ttest `var', by(gender)
    }

    The output I would like to obtain:
    ------------------------------------------------------------------------------------------------
    Obs(men) Obs(women) Mean(men) Mean(women) Difference (p-value)
    ------------------------------------------------------------------------------------------------
    Hours worked 749 106 19.83 24.77 -4.95 0.00
    Net income 749 106 3,317.12 2,315.91 1,001.21 0.00
    ------------------------------------------------------------------------------------------------


  • #2
    Silvia,

    First, I am assuming that you are doing a lot more than just two outcomes (hours and income) and/or one stratification variable (gender). Otherwise, you might as well enter the data into Excel by hand.

    I am not aware of a command or user-written program for this and since it has been some time since you posted without a response I assume no one else is aware of such a thing either. Accordingly, you may have to come up with something yourself. I can give you some hints, but I'm not sure how close this can get you to what you want. The first step is to realize that after you run a ttest, most of the quantities you need are available as scalars. To get the complete list, type return list after doing a ttest. The ones you want are r(N_1), r(N_2), r(mu_1), r(mu_2), and r(p). The only missing is the difference and you can calculate that by subtracting the two means.

    If you have Stata 13, check out the putexcel command. Otherwise, you will have to save the results to a data set and then export as excel. An easier way may be to use display to put the data in the output window in a nice format and then you can cut and paste that to Excel (or elsewhere). The potential solutions will be somewhat different depending on which direction you want to head. Let us know if you have any specific questions.

    Regards,
    Joe

    Comment


    • #3
      Thanks Joe for the suggestion. Indeed I had to table out the output of more than two variables. In the end I used the estpost command and then copied manually the table which was containing all the variables of interest to excel.

      estpost ttest h_worked net_inc, by(gender)

      Comment


      • #4
        I stumbled upon this post while searching for a way to export a ttest table into Excel. I used putexcel, as Joe suggested, and came up with this:

        putexcel set "T-test.xlsx", sheet("t_test") replace
        putexcel A1=("Group") B1= ("Obs") C1=("Mean") D1=("Std. Err.") E1=("Std. Dev.") F1=("95% Conf. Interval") A5=("Ha: diff < 0") B5=("Ha: diff != 0") C5=("Ha: diff > 0") D5=("t-value") E5=("df")
        local row=2
        qui ttest something, by(something)
        putexcel A`row' = ("something")
        putexcel B`row' = (r(N_1))
        putexcel C`row' = (r(mu_1))
        putexcel D`row' = (r(sd_1)/sqrt(r(N_1)))
        putexcel E`row' = (r(sd_1))
        putexcel F`row' = (r(mu_1)-1.96*(r(sd_1)/sqrt(r(N_1))))
        putexcel G`row' = (r(mu_1)+1.96*(r(sd_1)/sqrt(r(N_1))))
        local row=3
        qui ttest something, by(something)
        putexcel A`row' = ("something")
        putexcel B`row' = (r(N_2))
        putexcel C`row' = (r(mu_2))
        putexcel D`row' = (r(sd_2)/sqrt(r(N_2)))
        putexcel E`row' = (r(sd_2))
        putexcel F`row' = (r(mu_2)-1.96*(r(sd_2)/sqrt(r(N_2))))
        putexcel G`row' = (r(mu_2)+1.96*(r(sd_2)/sqrt(r(N_2))))
        local row=4
        qui ttest something, by(something)
        putexcel A`row' = ("diff")
        putexcel B`row' = (r(N_1)+r(N_2))
        putexcel C`row' = (r(mu_1)-r(mu_2))
        putexcel D`row' = (r(se))
        local row=6
        qui ttest something, by(something)
        putexcel A`row' = (r(p_l))
        putexcel B`row' = (r(p))
        putexcel C`row' = (r(p_u))
        putexcel D`row' = (r(t))
        putexcel E`row' = (r(df_t))

        I hope this will be helpful to the next person looking for a solution to a similar problem. You should only be careful with the 95% confidence interval when you're dealing with a low number of observations.
        There is really no other way to do it, is there?

        Comment


        • #5
          I have these codes for exporting ttest to excel I thought it might help....

          foreach var of varlist var1 var2 var2... {
          ttest `var', by(group)
          matrix ttest= (r(mu_1), r(N_1), r(mu_2), r(N_2), r(mu_1 - mu_2), r(p))
          matrix rownames ttest= `var'
          matrix colnames ttest= mean1 N1 mean2 N2 mean_differences pscore
          mat2txt, matrix(ttest) sav("put path where to save you file\filename.xls") append
          }

          Comment

          Working...
          X