Announcement

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

  • How to delete an explicit string from a local containing several string elements

    Hello everyone,

    I'm trying to update a local dynamically inside a loop. The idea is documented here: http://www.stata.com/statalist/archi.../msg01052.html

    However, I'm trying to do this with a local of strings and the following does not seem to work:

    Code:
    local strings a b c
    local test1 b
    local test2: list strings- test1
    di `test2'
    Can someone tell me what is wrong with this?

    Thanks in advance!



  • #2
    The last line should be:

    Code:
    display "`test2'"
    
    // OR IF ANYTHING IN test2 IN YOUR REAL SITUATION CONTAINS QUOTES
    display `"`test2'"'
    The problem with -di `test2'- is that Stata starts by expanded the macro reference `test2' and sees -di a c. Which means it is now looking variables a and c and wants to show the values of those variables in the first observation. If either of those variables doesn't exist in your data Stata gives you an error message. If they both exist, it will show you the values of those variables in observation1--but that isn't what you wanted. I imagine you wanted Stata to say: a c. By putting the reference to `test' inside quotes, Stata sees -di "a c"- after macro expansion and knows that you want to see the literal string a c, not the values of some variables.

    Added: Note that the problem you are having is entirely a matter of understanding who -display- works. Your macro manipulations were entirely correct. In particular, if you subsequently want to use the contents of test2 as values for, say, a loop parameter, you do not need to enclose it in double-quotes.

    Code:
    foreach x of local test2 {
        do_something_with_x
    }
    will "do_something" with a and then with c

    Code:
    regress y `test2'
    // IS EQUIVALENT TO
    regress y a c
    Last edited by Clyde Schechter; 13 Mar 2017, 14:15.

    Comment


    • #3
      thanks a lot. That makes perfect sense.

      edit: Yes, I didn't know that display would refer to elements by reference. I thought of it as a function returning names. Thanks for the hint!
      Last edited by Frank Taumann; 13 Mar 2017, 14:25.

      Comment

      Working...
      X