Announcement

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

  • Left single quote problem

    I would like to display a left-single-quote character, i.e. ` . I know this can be done trivially with

    Code:
    dis "`"
    However, this is part of a larger program where other strings are also evaluated, some of which contain double-quote characters. As a result, the enclosing quotes must be Stata's compound quotes. However, this does not work correctly:
    Code:
    dis `"`"'
    Nor does escaping out the forward quote work correctly:
    Code:
    dis `"\`"'
    The problem seems to be the closing compound quotes immediately following the left-single-quote. For instance, if there were a space in between:
    Code:
    dis `"\` "'
    Visually, this looks correct (since the space is invisible), but does not solve my programming problem wherein I cannot have the space being inserted. Ideas?

  • #2
    a " is char(34)

    Comment


    • #3
      The left single quote is char(96)
      Code:
      di "`=char(96)'"
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Originally posted by Carole J. Wilson View Post
        The left single quote is char(96)
        Code:
        di "`=char(96)'"
        Carole, I've tried that (sorry should have posted that result too). It doesn't solve my problem, which is the need to enclose within compound quotes. In other words, this does not work:
        Code:
        di `"`=char(96)'"'
        I don't think it should matter, but I should clarify that I am using Stata/SE 14.1 on Mac OSX 10.11.4.
        Last edited by Hemanshu Kumar; 02 May 2016, 22:24.

        Comment


        • #5
          Here is what I do

          Code:
          program display_string
              version 11.2
              
              /*
                  check for presence of double quotes */
              local dump : subinstr local 0 `"""' "" , count(local dq)
              if (`dq') {
                  /* has double quotes, so use compound quotes */
                  display `"`macval(0)'"'
                      /* macval() prevents any single quote in 0
                          to be interpreted as part of a local */
              }
              else {
                  /* no double quotes so no compound quotes needed */
                  display "`0'"
              }
          end
          
          display_string `
          display_string ""
          display_string "single ` quote"
          The first trick I use is to check for presence of double quotes and use compound quotes only when double quotes are present. The second trick is to use the macval() function. The purpose of this function is to prevent macro expansion of locals inside a local. That is why left single quotes in whatever is passed (in this case just a string) does not cause Stata to interpret it as meaning a local macro.

          Best
          Daniel
          Last edited by daniel klein; 02 May 2016, 23:53. Reason: add comments to the code

          Comment


          • #6
            Even simpler would be Mata

            Code:
            program print_string
                version 11.2
                mata : printf("{txt}%s\n", st_local("0"))
            end
            
            print_string `
            print_string ""
            print_string "single ` quote"
            Best
            Daniel

            Comment


            • #7
              daniel klein Thanks! That works! I would have preferred a more elegant solution, but this gets the job done. I would still like to understand though, why this code does not work:

              Code:
              dis `"\`"'
              Any thoughts?
              Last edited by Hemanshu Kumar; 04 May 2016, 09:20.

              Comment


              • #8
                No idea, but this works

                Code:
                dis `"\foo"'
                Best
                Daniel

                Comment

                Working...
                X