Announcement

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

  • 'Foreach' Loop - Store values in variable

    Question:
    Could you help me to find a way to store the resulting values of this 'foreach' loop in the variable SD.


    Code:

    clear all
    use "XXXXX"

    generate SD = .

    foreach i in forecast_period {
    tabstat point, statistics(sd) by(forecast_period)
    }

  • #2
    Dear Franciso,

    I think egen sd(exp) does what you want.

    egen SD = sd(point), by(forecast_period)

    Does this help?

    Best wishes,
    Sara

    Comment


    • #3
      Francisco - You'll generally increase your chances of a useful answer by following the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex.

      In this case, you got lucky. Sara answered and her solution is the quickest simplest way to handle the problem.

      If you intended something different and want to store values, you can set up a variable (to write into) with all missing data before the loop, run the analysis in the loop (with an if statement), and then replace the values in the set up variable with the same if statement.

      Comment

      Working...
      X