Announcement

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

  • too few quotes error

    Hi,
    I get a "too few quotes" error when I run the following and could not figure it out. My dataset has a variable named "institution" that has the school ID and I am trying to drop observations that are not listed in the school_ID macro



    Code:
    local school_ID "500" "100" "700" "300" "744" "723" "712" "450" "720" "721" "722"
    gen flag = 0
    foreach item of local school_ID {
    replace flag = 1 if institution == "`item'"
    }
    keep if flag == 1

    Thank you in advance!
    Best,
    Bikalpa
    Last edited by Bikalpa Baniya; 22 Sep 2024, 18:29.

  • #2
    The use of quotes in macros can be tricky. One key thing to be aware of is that when you define a local macro, Stata strips off any initial and final quote from the definition. Watch:
    Code:
    . local school_ID "500" "100" "700" "300" "744" "723" "712" "450" "720" "721" "722"
    
    . macro list _school_ID
    _school_ID:     500" "100" "700" "300" "744" "723" "712" "450" "720" "721" "722
    So when you get down to your loop over the items of school_ID you are in trouble at the very first one, because although you probably think it's "500", it's actually just 500". So when Stata says -if institution == "`item'"- it reads that as -if institution == "500""- which, indeed, has unbalanced quotes.

    In your case the solution is actually quite simple. Just get rid of all the queost in your definition of local macro institution. None of them are necessary, and they are getting you in trouble.

    Only use quotes in macro definitions if you need Stata treat something that contains one or more internal blank spaces as a single item in the macro. And, if at all possible, avoid making that one either the first or the last item in the macro. If there are reasons why such an item must be ordered first or last in the macro, then wrap the entire macro definition in compound double quotes (`"..."').

    Comment


    • #3
      Thank you Clyde!

      Comment

      Working...
      X