Announcement

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

  • levelsof within for loop

    I am using STATA 17 and want to run the following code

    Code:
    * for each block
    forvalues b = 1/`nblock' {
    levelsof choice_set if block == `b', local(choice_sets)
    * for each choice set
    foreach c_set in local choice_sets {
    display `c_set'
    }
    }

    I ran into an error `local not found`
    Not sure what went wrong, I went through the post "levels of loop within a loop" by Peggy Fan in 2014. It doesn't seem to be working

    Appreciate any help and suggestions! Thank you


  • #2
    Code:
    foreach c_set of local choice_sets

    foreach lname in list { ... } allows a general list. Elements are separated from each other by
    one or more blanks.

    foreach lname of local list { ... } and foreach lname of global list { ... } obtain the list
    from the indicated place. This method of using foreach produces the fastest executing code.
    from

    Code:
    help foreach

    Comment


    • #3
      Big thanks, Andrew!

      Comment

      Working...
      X