Announcement

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

  • removing single quote

    Dear members,

    I have a dataset that looks like the following. I am trying to remove the sigle quotes from the data, unfortunately with no luck. I would appreciate any suggestion.

    Thanks,
    Anwesha

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 var1 str6 var2 str4 var3 str6 var4 float var5
    "'TL'" "'AHA'" "'IT'" "'1973'"  29408.17
    "'TL'" "'AHA'" "'IT'" "'1974'"  29408.22
    "'TL'" "'AHA'" "'IT'" "'1975'" 29407.965
    "'TL'" "'AHA'" "'IT'" "'1976'" 29408.234
    "'TL'" "'AHA'" "'IT'" "'1977'"  29407.78
    "'TL'" "'AHA'" "'IT'" "'1978'" 29405.285
    "'TL'" "'AHA'" "'IT'" "'1979'" 29405.336
    "'TL'" "'AHA'" "'IT'" "'1980'"  29405.56
    "'TL'" "'AHA'" "'IT'" "'1981'"  29405.81
    "'TL'" "'AHA'" "'IT'" "'1982'" 29406.805
    end

  • #2
    Code:
    foreach var of varlist var* {
    replace `var' = subinstr(`var',"'","",.)
    }

    Comment


    • #3
      That worked. Thank you.

      Comment


      • #4
        This works too. Find out which ASCII character that is and remove it in those terms. Here I illustrate on a simple string constant, but for variables it's the same idea.

        Code:
        . mata: ascii("'")
          39
        
        . di subinstr("foo'bar", char(39), "", .)
        foobar
        I don't think this is a better solution in this case, but the approach seems more attractive with e.g. tab characters.

        Comment


        • #5
          In this case, and often true, regular expressions can offer a more surgical solution.

          Code:
          foreach v of varlist var1-var4 {
            replace `v' = ustrregexra(`v', "^[\']", "", .)
            replace `v' = ustrregexra(`v', "[\']$", "", .)
          }
          compress

          Comment

          Working...
          X