Announcement

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

  • Foreach Stata 13

    Thanks to all for your help is very useful...

    I have to create 5 variables x1 to x5 so I use

    Code:
    foreach x of newlist x1-x5{
    gen `x' = 2*x
    }
    but I need to have a specific result
    Code:
    x1 = 2*x
    x2 = 4*x
    x3 = 6*x
    x4 = 8*x
    x5 = 10*x
    
    x = rnormal()
    I have difficulty how to put all that in one boucle...

    Thanks for your help

    Patrick

  • #2
    This could be done with -foreach-, but it's easier with -forvalues-
    Code:
    forvalues i = 1/5 {
      generate x`i' = 2 * `i' * x
    }
    It's not clear to me if you want each use of x to be rnormal(), computed anew at each iteration, or for there to be one value of x = rnormal(), established before the loop. I'll leave that to you.

    Comment


    • #3
      Thank Mr. Lacy, this work perfectly

      Comment

      Working...
      X