Announcement

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

  • Replacing quotes from a string variable

    Hi,

    I am struggling with a possibly very trivial issue. I need to get rid of inverted commas (") from a string variable.

    If I try string functions such as:

    replace v1 = substr(v1, """,.)

    I am returned with the error that quotes are too few.

    Would you please suggest me a solution?

    Best,

    Carlo

  • #2
    Right. Stata thinks that the interior " closes the first one, and so the third is left hanging. That is why Stata Corp. created compound double quotes, to handle quoting expressions that themselves contain quotes. Try
    Code:
    replace v1 = substr(v1, `"""',.)
    Note: It is hard for the human eye to distinguish a pair of consecutive ' characters from a single " character. So I recommend you copy and paste the above into your code and not try to type it manually.

    Comment


    • #3
      Thanks Clyde, it works fine!

      Best, carlo

      Comment


      • #4
        I don't believe this thread so far. I think everyone has been meaning to write

        Code:
        replace v1 = subinstr(v1, `"""', "", .)
        as none of the substr() examples given would work even to extract substrings.

        Another way to do it:

        Code:
          
        replace v1 = subinstr(v1, char(34), "", .)

        Comment


        • #5
          Nick is, of course, correct.

          In responding to #1, I immediately noticed that putting the " character inside compound double quotes was the gist of the problem. I didn't even look carefully enough at the rest of the code to notice that an argument was missing, and I just copied and edited the original code into my response. Sorry for that carelessness.

          Comment

          Working...
          X