Announcement

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

  • In a loop, strtrim() won't work in some of my variables

    Hello everyone! I'm facing a little difficulty using the strtrim() command here. As always, I looked on the forum to search for possible answers but none of them seemed convincing.

    I'm trying to do a simple change here, that is, to remove all leading spaces in one observation using the command strtrim() function in a loop across several of my variables. However, for a reason I don't know, the code just won't work for all of them. Please consider this simple example :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 FW str22 FX str33 FY
    " [xxx]" "   [yyy]" " [zzz]"
    end
    As you can see, all 3 variables have leading spaces that I wish to remove. However, the code :

    Code:
    foreach var of varlist FW-FY {
    replace `var' = strtrim(`var')
    }
    yields

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 FW str22 FX str33 FY
    "[xxx]" "   [yyy]" "[zzz]"
    end
    the second one didn't change. I noticed that other later operations I also included in the loop such as subinstr do not work on the FX variable as well as many others in my dataset. I suspected that there might be other types of "blank space" characters, and indeed, after looking at -chartab- (from SSC), I tried including:

    Code:
    foreach var of varlist FW-FY {
    replace `var' = strtrim(`var')
    replace `var' = subinstr(`var', uchar(160), "", .)
    }
    But nothing changes too. Could you please tell me what's wrong here please ? It would help me a lot
    Last edited by Thomas Brot; 05 Mar 2023, 16:12.

  • #2
    Just so everyone's clear, is the dataex example you provided your ACTUAL dataset? That is if I could look at your laptop and your actual data, would it say "[yyy]" as a string?

    Comment


    • #3
      Code:
      replace `var' = subinstr(`var', uchar(160), "", .)
      
       // needs to be:
      
      replace `var' = usubinstr(`var', `"`=uchar(160)'"', "", .)
      Last edited by Clyde Schechter; 05 Mar 2023, 17:05.

      Comment

      Working...
      X