Announcement

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

  • Constructing a loop

    Hi,
    I would be grateful for any advice on the following:
    I am running the command reg several times in Stata 14 using 3 variables per regression.
    Within each regression the dependent variable is Y at timepoint2, then the first independent variable X1 is treatment and the second independent variable X2 is the baseline value of Y.
    For every regression the Ys and X2s correspond to different variables and only treatment is the same across all models
    How can I construct a loop which will correctly substitute the Ys and X2s from my variable list for every regression I am fitting.
    Thank you,
    Nick

  • #2
    Well, to give a concrete answer requires knowing the names of your variables. I'm going to make the simple assumption that they are named Y_1, Y_2, Y_3, etc., and that the X2 variables are named X2_1, X2_2, X2_3, etc. Then it's

    Code:
    forvalues i = 1/3 {
        regress Y_`i' X1 X2_`i'
    }
    If your variables are not that simply named, you could consider renaming them according to that scheme. Or there are other ways to loop over parallel lists. If you want to pursue one of these other ways, you'll have to show the variable names.

    Comment

    Working...
    X