Announcement

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

  • Looping ttest on multiple independent variables

    Hi,

    I have a problem running a loop for multiple independent variables. Here are the name of the variables:
    group 1:
    Code:
    CDR2_SDP_staff_250m_unmet CDR2_SDP_sixdays_250m_unmet CDR2_SDP_proj_250m_unmet CDR2_SDP_phar_HC_250m_unmet
    group 2:
    Code:
    CDR2_SDP_staff_250m_mcp    CDR2_SDP_sixdays_250m_mcp    CDR2_SDP_proj_250m_mcp    CDR2_SDP_phar_HC_250m_mcp
    As you can see the names are almost the same, except the "mcp" or "unmet" at the end of the variable name.

    the test I need is:
    Code:
    ttest CDR2_SDP_staff_250m_unmet==CDR2_SDP_staff_250m_mcp,unequal
    I need a loop to run this ttest. These are part of my variables. I have 36 variables in each group (36 in "mcp" group and 36 in "unmet" group.

    Thanks for your help.

  • #2
    Something like the following might be a useful approach.
    Code:
    foreach var in CDR2_SDP_staff_250m CDR2_SDP_sixdays_250m CDR2_SDP_proj_250m CDR2_SDP_phar_HC_250m {
    ttest `var'_unmet==`var'_mcp,unequal
    }

    Comment


    • #3
      Wonderful, thanks. It works perfect.
      Could you please explain why does this work? I mean in first part of foreach, (
      CDR2_SDP_staff_250m CDR2_SDP_sixdays_250m CDR2_SDP_proj_250m CDR2_SDP_phar_HC_250m)these are not complete variable names. What are we telling stata to do here here? I would appreciate if you could explain

      Comment


      • #4
        Anything I wrote would not be as helpful as the material on Macros in Section 18.3 of the Stata User's Guide PDF included with your Stata installation and accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        I'm sympathetic to you as a new user of Stata - it's a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

        When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

        All of these manuals are included as PDFs in the Stata installation. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

        Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

        Comment

        Working...
        X