Announcement

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

  • Forloop: too many variables specified r(103) error

    Hello,

    I've been around for a year on this community, but this is my first post. I am running a forloop, and it gives me this error: too many variables specified r(103);.

    Here is my code:

    Code:
    levelsof uscity if us == 1, local(uscitylist)
    foreach city of local uscitylist {
        g `city'_NS = .
    }
    There are 9 items in local uscitylist. The code works for the first 5 items, and then gives the error.

    How can I make the forloop work?


    Thank you
    Last edited by Hyunseo Yoo; 30 Jul 2021, 03:33.

  • #2
    Suppose the cities include Boston, Chicago and New York. Then Boston_NS and Chicago_NS work fine but


    Code:
    gen New York_NS = .
    would trigger this error message. If you push your city names through strtoname() that should help.

    Code:
    . di strtoname("New York")
    New_York

    Comment


    • #3
      Thanks so much Nick Cox !! This code works nicely:

      Code:
      replace uscity = strtoname(uscity)
      
      levelsof uscity if us == 1, local(uscitylist)
      
      foreach city of local uscitylist {
          g `city'_NS = .
      }

      Comment

      Working...
      X