Announcement

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

  • String macros in loop and inlist

    Hello Everyone,

    I appreciate some help with the following code. I've exhausted all quotation marks/ backtick combinations to no luck.

    This is a simple program that has aa bb and cc, all strings as inputs.
    Then I want the permutations of them : aa bb, aa cc then bb cc.
    So I read aa first as var1, and put it in var1s to later know aa was already used. Then var2 will bb so I get aa bb and then cc so I get aa cc.
    Now I go to the second var1 which will be bb. I put this in var1s for future. Then I read var2 which is aa bb and cc.
    Now I want the program to use inlist and know that aa is already used and so don't try bb aa and so on.
    This program just understands the new addition not what I stored in var1s.

    capture drop local var1s

    //local testvariables "aa" "bb" "cc"
    local testvariables `""aa", "bb", "cc""'

    local var1s=.

    foreach var1 of local testvariables{


    local var1s `" "`var1s'", "`var1'" "'

    foreach var1 of local var1s{

    di" members of var1s"

    di "`var1'"
    }


    foreach var2 of local testvariables {


    di"var2"
    di"`var2'"

    scalar result=inlist("`var2'", `var1s')
    display result

    } //loopvar2


    } //loopvar1




    I greatly appreciate your input.

  • #2
    I'd check out tuples from SSC, which I hope does what you want.

    Comment


    • #3
      Thank you for your input Dr. Cox. Since this is a basic topic, may I know how the use of "" `' is justified?

      For saving variables inside a list in a loop

      a) local var1s `" "`var1s' ", "`var1' " " '

      b) local var1s "`var1s' " "`var1'"
      Which on is correct? So b) "` ' " to me is correct because its a macro and its a string. I got a) from some post but I don't understand the justification of another round of `" "'?

      Or:

      For example why " `var2' " != "`var1' " is not acceptable?









      Comment


      • #4
        Suppose you want to create a new local macro, let's call it my_list, whose elements are "aa bb", "c", and "dd ee". You must always bear in mind that when you define a local macro in Stata, Stata will strip away any initial quote and any final quote. So if you write
        Code:
        local my_list "aa bb" "c" "dd ee"
        before creating the local macro, Stata "simplifies" this to
        Code:
        local my_list aa bb " "c" "dd ee
        Notice how aa bb is no longer bound in quotes, and how the first quotes bind a blank space. In fact, Stata interprets this as a four element macro. The first element is aa. The second element is bb", the third is c, and the fourth is dd ee. Not even close to what you wanted.

        So to preserve what you want, you need an extra pair of "throwaway" quotes around the whole thing. These extra quotes cannot be the normal quotation mark (") because you can't nest "" inside "". So we use the compound double quotes for that:
        Code:
        local my_list `""aa bb" "c" "dd ee""'
        Now, Stata strips away the outer `" and "' and sees three elements for your list, "aa bb", "c", and "dd ee", as you intended.

        By the way, the quotes around "c" in that command are not necessary. It is only necessary to bind something in quotes if it contains an internal blank.

        Comment


        • #5
          Clyde Schechter nicely explained the main points. Meanwhile a question in #2 seems to have no context but can be answered any way.

          why " `var2' " != "`var1' " is not acceptable?
          Code:
          " `var2' " != "`var1' "
          is legal code with nothing else said. It's however unlikely to be exactly what anyone needs.

          1. The references are to local macros var1 and var2. If var1 and var2 hold the same content the strings will still not be equal as (for example, " frog" is not equal to "frog ", as the leading and trailing spaces are included in the comparison. If var1 and var2 hold different content, it will be untrue.

          2. If either local macro includes a double quotation mark, this is likely to be misinterpreted for the reasons Clyde describes.
          Last edited by Nick Cox; 27 Jan 2024, 12:05.

          Comment


          • #6
            There is yet another rogue space in the code cited just above.

            Comment

            Working...
            X