Announcement

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

  • problem with backtick in a string

    I am trying to destring a variable which occasionally contains the backtick ` symbol. I am just not being able to make it work.

    Here is a trivial data example:

    Code:
    clear
    input str12 mobile_no1
    "`7541929932"
    end
    Here are all the destring commands I have tried:
    Code:
    destring mobile_no1, replace ignore("`")
    destring mobile_no1, replace ignore("\`")
    destring mobile_no1, replace ignore("`=char(96)'")
    destring mobile_no1, replace ignore(`"`"')
    destring mobile_no1, replace ignore(`"\`"')
    destring mobile_no1, replace ignore(`"`=char(96)'"')
    They all scream:
    Code:
    too few quotes
    r(132);
    What am I doing wrong?

  • #2
    I believe you need to use an escape character so Stata will recognize this correctly; see
    Code:
    help ustrunescape()

    Comment


    • #3
      Thank you, Rich, I didn't know about that. However, I'm still not sure how to make it work.

      Code:
      dis ustrunescape("\'")
      works just fine. But so does

      Code:
      dis "\`"
      which was already one of the things I tried. However, there's something specific about the way -destring- parses the contents of the -ignore- option, which is creating an issue.

      If you could tell me how to make it work in -destring-, that would be great!

      Comment


      • #4
        I don't know how to make it work either -- with destring. But I have a solution for you, fixing the problem before you call up destring.

        Code:
         
         clear input str12 mobile_no1 "`7541929932" end  replace mobile_no1 = subinstr(mobile_no1, char(96), "", .)

        Comment


        • #5
          Hi Nick, Yes I also had figured out the subinstr() workaround (which works even with the usual escaped "\`" ). But I keep telling myself, surely there has to be a way for -destring- to be able to deal with this!

          Comment


          • #6
            I also had figured out the subinstr() workaround
            Good, but I don't know how we were expected to realise that.

            All I can say otherwise is that I don't know a solution. Stata at a deep level expects a backtick to signal a forthcoming local macro reference and for there to be a matching single quote. That's a long way from your situation, but it's biting.

            There is a small irony here in that back in the day destring was my command before it was ever published or folded into official Stata (in Stata 7), but the company have re-written and revised it again and again since then. It's evidently not quite perfect in coping with all the crud that users find in their datasets.

            Comment

            Working...
            X