Announcement

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

  • Help with strtrim

    Hello,

    I have noticed that I have some likert-scale variables where some of the values have trailing spaces. I want to use "strtrim" to remedy this, but I don't know how. Could someone please help? I have used dataex below with a couple of my problem variables.

    Many thanks,
    Alyssa

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str21(how_important_grp_seeds_plants how_important_grp_ar)
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    ""                      ""                    
    "Extremely important"   "Very important"      
    "Extremely important"   "Very important"      
    "Extremely important"   "Somewhat important"  
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Very important"      
    "Extremely important"   "Very important"      
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Very important"      
    "Extremely important"   ""                    
    "Extremely important"   "Not at all important   "
    "Extremely important"   "Very important"      
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Very important"      
    "Extremely important"   "Very important"      
    "Extremely important"   "Very important"      
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Somewhat important"  
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Not at all important   "
    "Extremely important"   "Very important"      
    "Extremely important"   "Somewhat important"  
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Not at all important   "
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Very important"      
    "Extremely important"   "Very important"      
    "Extremely important"   "Very important"      
    "Extremely important"   "Somewhat important     " 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Very important"      
    "Extremely important"   ""                    
    "Extremely important"   "Extremely important" 
    "Extremely important"   "Very important"      
    "Extremely important"   "Extremely important" 
    "Not at all important"  "Extremely important" 
    "Not at all important   " "Not at all important "
    "Not at all important   " "Not at all important "
    "Not at all important   " "Not at all important "
    "Somewhat important     "   "Somewhat important " 
    "Somewhat important     "   "Very important"      
    "Somewhat important     "   "Somewhat important " 
    "Somewhat important     "   "Not at all important       "
    "Somewhat important     "   "Very important"      
    "Very important"        "Very important"      
    "Very important"        "Somewhat important     " 
    "Very important"        "Extremely important" 
    "Very important"        "Very important"      
    "Very important"        "Very important"      
    "Very important"        "Somewhat important     " 
    "Very important"        "Very important"      
    "Very important"        "Somewhat important     " 
    "Very important"        "Very important"      
    "Very important"        "Somewhat important     " 
    "Very important"        "Very important"      
    "Very important"        "Very important"      
    "Very important"        "Not at all important   "
    "Very important"        "Not at all important   "
    "Very important"        "Extremely important" 
    end

  • #2
    Code:
    foreach v of varlist how_important_* {
        replace `v' = strtrim(`v')
    }
    Note: where there are leading and trailing blanks, there are often extra internal blanks as well. If your data (may) have those and you want to get rid of them, you can do -replace `v' = strtrim(stritrim(`v')- instead.

    Comment


    • #3
      Hi Alyssa, I end up using the old names for these trim functions (trim, itrim, rtrim, etc), and I usually always use trim (because it removes both leading and trailing spaces). But for your purposes, you would do:

      Code:
      replace how_important_grp_ar = rtrim( how_important_grp_ar)
      
      . list in 20/30
      
           +-------------------------------------------+
           | how_important_grp~s   how_important_grp~r |
           |-------------------------------------------|
       20. | Extremely important    Somewhat important |
       21. | Extremely important    Somewhat important |
       22. | Extremely important   Extremely important |
       23. | Extremely important   Extremely important |
       24. | Extremely important   Extremely important |
           |-------------------------------------------|
       25. | Extremely important   Extremely important |
       26. | Extremely important        Very important |
       27. | Extremely important        Very important |
       28. | Extremely important    Somewhat important |
       29. | Extremely important   Extremely important |
           |-------------------------------------------|
       30. | Extremely important   Extremely important |
           +-------------------------------------------+

      Comment


      • #4
        If you wanted to loop over all string variables, you could do:

        Code:
        ds, has(type string)
        foreach var of varlist `r(varlist)' {
             replace `var' = itrim(trim(`var'))
        }
        Or, you could do "replace `var' = stritrim(strtrim(`var'))"

        Comment


        • #5
          Thank you David and Clyde for your responses!

          Comment

          Working...
          X