Announcement

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

  • strtrim not producing desired results

    Hi Stata Users,

    I am using strtrim command in Stata 15. The goal is to remove blank spaces at the beginning and end of a string character.

    An example of the data is below

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str54 code
    "50800000-3 "
    " 33169000-2"
    " 66514100-7"
    " 66514100-7"
    " 66514100-7"
    " 66514100-7"
    " 50410000-2"
    " 39710000-2"
    "03200000-3 "
    end
    and the command am is
    Code:
    replace code = strtrim(code)
    Please let me know what am doing wrong or a different way to approach the same results.

    Thanks in advance!

  • #2
    -strtrim()- will eliminate leading and trailing spaces. I think what you want is to change the display format of the variable after getting rid of the spaces.

    Code:
    format code %12s

    Comment


    • #3
      Looks fine to me. Strictly, strtrim() is a function, not a command.


      Code:
      . clear
      
      . input str54 code
      
                                                             code
        1. "50800000-3 "
        2. " 33169000-2"
        3. " 66514100-7"
        4. " 66514100-7"
        5. " 66514100-7"
        6. " 66514100-7"
        7. " 50410000-2"
        8. " 39710000-2"
        9. "03200000-3 "
       10. end
      
      .
      . replace code = strtrim(code)
      (9 real changes made)
      
      .
      . list
      
           +------------+
           |       code |
           |------------|
        1. | 50800000-3 |
        2. | 33169000-2 |
        3. | 66514100-7 |
        4. | 66514100-7 |
        5. | 66514100-7 |
           |------------|
        6. | 66514100-7 |
        7. | 50410000-2 |
        8. | 39710000-2 |
        9. | 03200000-3 |
           +------------+
      Last edited by Nick Cox; 26 Jan 2021, 04:31.

      Comment


      • #4
        Thanks Andrew Musau Unfortunately the issue isn't formatting

        Comment


        • #5
          Thanks Nick Cox for your reply. Not sure why am not getting similar results.

          Comment


          • #6
            Use chartab (SSC) to check for characters that look like spaces but are not. For example,

            Code:
            . di "|"  uchar(160)  "|"
            | |


            That looks like a space between two pipe characters, doesn't it?
            Last edited by Nick Cox; 26 Jan 2021, 05:11.

            Comment


            • #7
              Thanks Nick Cox for the additional hint. I sincerely appreciate.

              Comment


              • #8
                Nick Cox you never disappoint!!! Your hint was really helpful!

                Your proposal helped me identify potentially how to resolve the problem

                Code:
                . chartab code
                
                   decimal  hexadecimal   character |     frequency    unique name
                ------------------------------------+--------------------------------
                        45       \u002d       -     |             9    HYPHEN-MINUS
                        48       \u0030       0     |            32    DIGIT ZERO
                        49       \u0031       1     |            11    DIGIT ONE
                        50       \u0032       2     |             4    DIGIT TWO
                        51       \u0033       3     |             6    DIGIT THREE
                        52       \u0034       4     |             5    DIGIT FOUR
                        53       \u0035       5     |             6    DIGIT FIVE
                        54       \u0036       6     |             9    DIGIT SIX
                        55       \u0037       7     |             5    DIGIT SEVEN
                        56       \u0038       8     |             1    DIGIT EIGHT
                        57       \u0039       9     |             2    DIGIT NINE
                       160       \u00a0             |             8    NO-BREAK SPACE
                ------------------------------------+--------------------------------
                
                                                    freq. count   distinct
                ASCII characters              =              90         11
                Multibyte UTF-8 characters    =               8          1
                Unicode replacement character =               0          0
                Total Unicode characters      =              98         12
                and the code below resolves the issue

                Code:
                replace code = subinstr(code, uchar(160), "", .)
                Thanks so much once again!
                Last edited by Stephen Okiya; 26 Jan 2021, 05:24.

                Comment

                Working...
                X