Announcement

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

  • Dropping elements from local created with levelsof

    Is there a way to iterate through elements of the local created by levelsof and remove those elements? I found one way to do this with a macro consisting of numbers (https://www.stata.com/statalist/arch.../msg01052.html):

    Code:
    local alpha 1 2 3 4 5
    di "`alpha'"
    local not 1
    local alpha: list alpha- not
    di "`alpha'"
    but when I try it with levelsof, I'm getting an "invalid name" error. Here are the displays I see:

    `"10151_1A"' `"10151_1B"' `"10641_1"' (after running the levelsof command)
    10151_1A10151_1B10641_1 (when displaying levels_nonmissing the first time)
    `10151_1A"' `"10151_1B"' `"10641_1"'" (when displaying it the second time)

    It looks like the quote at the beginning of the local was moved to the end. My code is below:

    Code:
    pause on
    levelsof id, local(levels)
    levelsof id, local(levels_nonmissing)
    di `levels_nonmissing'
    foreach unt of local levels {
        local not "`unt'"
        local levels_nonmissing: list levels_nonmissing - not
        di "`levels_nonmissing'"
        pause
    }
    For context, I am attempting to follow Andrew's method here:

    https://www.statalist.org/forums/for...a-new-variable

    to iterate through subsets of my data, run regressions, store their coefficients, and label my coefficient columns. Some of my regressions don't work, so I'm using capture and a conditional statement to try to remove those names from the local macro so that I can name the non-missing regressions correctly (skipping the missing id names).

    Thanks!

  • #2
    The problem is that this code
    Code:
        di "`levels_nonmissing'"
    should surround the macro expansion `levels_nonmissing' with Stata's compound double quotes, since the `levels_nonmissing' includes double quote characters. Changing this line to
    Code:
        di `"`levels_nonmissing'"'
    solves this problem.

    If you are not familiar with Stata's compound double quotes, see section 18.3.5 of the Stata User's Guide PDF included in your Stata installation and accessible through Stata's Help menu.
    Last edited by William Lisowski; 02 Mar 2020, 15:58.

    Comment


    • #3
      Thank you for finding my mistake! That was much simpler than I thought it was going to be.

      Comment

      Working...
      X