Announcement

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

  • Trim functions stata 17

    Hello,
    I am trying to trim some leading blanks in a string variable using STATA 17 but nothing happens as indicated below- is there some package additional package I needed to load?

    . codebook FORENAME

    ----------------------------------------------------------------------------------------------------------------------------------
    FORENAME FORENAME
    ----------------------------------------------------------------------------------------------------------------------------------

    Type: String (str56)

    Unique values: 8,206 Missing "": 0/58,996

    Examples: "Courtney"
    "Harvey"
    "Lewis"
    "Owen"

    Warning: Variable has embedded blanks.

    . replace FORENAME=strltrim(FORENAME)
    (0 real changes made)



  • #2
    Code:
    help ustrltrim()

    Comment


    • #3
      Leading blanks are not the same thing as embedded blanks; the codebook output reports the latter.

      You get rid of (consecutive) embedded blanks with stritrim(); there is no Unicode variation of that function.

      I do not remember (and have no access to my computer right now) whether codebook also reports single embedded spaces as in "foo bar", which typically do not pose any problem but are intended to be there.

      Comment


      • #4
        To expand on #3, consider the data

        Code:
        clear
        input str20 a
        "foo bar"
        "  foobar"
        "foobar  "
        "foo    bar"
        end
        then we get (condensing the output here):

        Code:
        . codebook a
        ...
                       Warning: Variable has leading, embedded, and trailing blanks.
        
        . codebook a in 1
        ...
                       Warning: Variable has embedded blanks.
        
        . codebook a in 2
        ...
                       Warning: Variable has leading blanks.
        
        . codebook a in 3
        ...
                       Warning: Variable has trailing blanks.
        
        . codebook a in 4
        ...
                       Warning: Variable has embedded blanks.
        So your warning of "embedded blanks" could come from just single spaces between words (as in the first observation here), which would never be trimmed and may not be a problem in the first place.

        Comment

        Working...
        X