Announcement

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

  • List and compound double quotes

    I would like to create a list using a loop, keep the unique elements with quotes and then pass these to mata. However, i have not been successful so far. The closest i have managed to get to is the following:

    Code:
    local belselem1 bel's list is not long
    local belselem2 this is a boring list
    local belselem3 bel's list is not long
    
    forval i=1/4 {
        local belslist1 `"`belslist1'"' `""`belselem`i''""'
    }
    
    local belslist2: list uniq belslist1
    
    di `"`belslist2'"'
    
    mata:
    belslist3 = st_local("belslist2")
    belslist3
    end
    The list should be "bel's list is not long" "this is a boring list". What am i doing wrong?

  • #2
    Any ideas?

    Comment


    • #3
      Change

      Code:
      local belslist1 `"`belslist1'"' `""`belselem`i''""'
      to

      Code:
      local belslist1 `"`belslist1 "`belselem`i''""'
      or use Mata right away

      Code:
      version 14.2
      
      mata :
      
      void belslist()
      {
          string colvector belslist
          
          belslist = J(3, 1, "")
          for (i = 1; i <= 3; ++i) {
              belslist[i] = st_local("belselem" + strofreal(i))
          }
          
          belslist = uniqrows(belslist)
          belslist
      }
      
      end
      
      local belselem1 bel's list is not long
      local belselem2 this is a boring list
      local belselem3 bel's list is not long
      
      mata : belslist()
      If this does not work for you, please explain more details of what you are ultimately want to do.

      Best
      Daniel

      Comment


      • #4
        Code:
        local belslist1 `"`belslist1 "`belselem`i''""'
        This actually does the trick Daniel. Thanks!

        Comment


        • #5
          Worked out a solution and then read that Daniel posted the same idea.

          So at the risk of genial but redundant endorsement I note that

          1. Nesting quoted strings is tricky particularly when you want some " " to stay in place --- as defining the elements -- while noting, or rather fighting, Stata's inclination to strip the outermost quotes when a macro is evaluated. Nesting creates an obligation for later parsing, but like all work that should be avoided if possible.

          2. Putting several items into one only to want to take them out again is puzzling. I've found myself doing this too and try to educate myself out of it by analogies like these.

          a. You have a pen and pencil in your hand.

          b. Put the pen and pencil into this box.

          c. Now take them out again. You now have the pen and pencil in your hand.

          Erm, why the box bit?

          I see this most often in the form saved result put into local; value taken out of local when the saved result can be used directly.

          More generally, the problem here seems to include

          A. Transfer from Stata to Mata. Why not just define stuff in Mata?

          B. Tacit use of string vectors, i.e. a set of strings. Why not use string variables in Stata or vectors in Mata?

          Comment


          • #6
            Nick Cox thank you for your very interesting suggestions. The analogy applies to many cases, including mine and more often than I would like to admit. However, in this particular situation I have elements with spaces, single quotes and I need to collect them, count them and then use them further. I chose locals because, from my experience, they are faster to work with compared to variables. At least collectively in a program. I might be wrong though. And I choose to do the housekeeping in Stata and pass the result to Mata as it requires less coding. Maybe I should had written the entire thing in Mata from the beginning but that's another story. I totally agree though that nested quote strings are particularly error prone and sometimes difficult to debug and thus should be avoided if possible.

            Comment


            • #7
              OK. There is often a very fine balance between trying to narrow a question down to the bare minimum you think you need to know and explaining the real problem which is usually much larger and more complicated. If you put your messages in a string variable then direct access of mymessage[1] mymessage[2] etc should usually be pretty fast. In practice that would usually be a temporary variable.

              Comment


              • #8
                Maybe a last word of caution. Once your strings have left single quotes, you will have a hard processing them in Stata.

                Try

                Code:
                local belselem1 bel's list is not long
                local belselem2 this is a boring list
                local belselem3 bel`s list is not long // <- note single left quote here
                This is usually easier done in Mata.

                Best
                Daniel

                Comment


                • #9
                  daniel klein hence the double quotes! All the processing is done using double quotes for a reason. But yes, Mata is probably the best way to deal with this, if you have left single quotes. Again thanks.

                  Comment


                  • #10
                    Originally posted by Belinda Foster View Post
                    daniel klein hence the double quotes!
                    Your (compound) double quotes do no longer work with unbalanced single left quotes, that is the point.

                    Code:
                    . local belselem1 bel's list is not long
                    
                    . local belselem2 this is a boring list
                    
                    . local belselem3 bel`s list is not long
                    
                    .
                    . forval i=1/4 {
                      2.     local belslist1 `"`belslist1' "`belselem`i''""'
                      3. }
                    invalid syntax
                    r(198);
                    Not so in Mata

                    Code:
                    . mata :
                    ------------------------------------------------- mata (type end to exit) ------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    :
                    : void belslist()
                    > {
                    >     string colvector belslist
                    >    
                    >     belslist = J(3, 1, "")
                    >     for (i = 1; i <= 3; ++i) {
                    >         belslist[i] = st_local("belselem" + strofreal(i))
                    >     }
                    >    
                    >     belslist = uniqrows(belslist)
                    >     belslist
                    > }
                    
                    :
                    : end
                    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    
                    .
                    . local belselem1 bel's list is not long
                    
                    . local belselem2 this is a boring list
                    
                    . local belselem3 bel`s list is not long
                    
                    .
                    . mata : belslist()
                                                1
                        +--------------------------+
                      1 |  bel's list is not long  |
                      2 |  bel`s list is not long  |
                      3 |   this is a boring list  |
                        +--------------------------+
                    Best
                    Daniel

                    Comment


                    • #11
                      daniel klein I actually agreed with what you wrote. It is just that, as you can see in my initial post, I do not have any left single quotes in my locals.

                      Comment

                      Working...
                      X