Announcement

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

  • Remove all whitespace from a string variable.

    How can I remove all whitespace from a string variable?
    For example, if I have the below data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20 var
    "Hello"     
    "Hello"     
    "Hello"     
    "Hello"     
    "HelloThere"
    "HelloThere"
    end
    I want all the values of the variable to appear as "HelloThere" .
    I tried
    Code:
    strtrim
    but that doesn't seem to seem to remove single space between two words.

  • #2
    Code:
    replace var = subinstr(var," ","",.)

    Comment


    • #3
      #2 will be helpful if you are certain that all whitespace are in fact plain spaces. But what if there are other types of whitespace? For this, you can use regular expressions.

      Code:
      gen newvar = ustrregexra(var, "\s", "")

      Comment

      Working...
      X