Announcement

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

  • How can I get the trim(), strltrim(), etc. functions in Stata?

    Basically what ssc package do I have to install in order to use those functions in Stata? Or is there another way to remove leading spaces in my data?
    Thanks

  • #2
    they are built in to Stata; trim() is the old name, strltrim() is the current name (as of version 14 IIRC); type "h function" and click on "string functions" and scroll down

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      they are built in to Stata; trim() is the old name, strltrim() is the current name (as of version 14 IIRC); type "h function" and click on "string functions" and scroll down

      So I have a string variable called price_string and I'm trying to trim leading spaces off of a bunch of observations.

      When I type

      . strltrim(price_string)

      I get


      . strltrim(price_string)
      unrecognized command: strltrim
      r(199);


      And the same happens for any variety of the trim() functions I input.

      Comment


      • #4
        Welcome to Statalist!

        Well, after several edits I see the problem. In post #1 you recognize strltrim as a function, which it is; in post #3 you have used it as a command, which it is not. In Stata, as in most programming languages, function has a specific meaning different from "functionality", and function and command do not have the same meaning. If this doesn't ring a bell, see the output of help function for more details on the use of functions.
        Last edited by William Lisowski; 17 Jul 2016, 19:43.

        Comment


        • #5
          strtrim() is the new name of trim().

          strltrim() is the new name of ltrim().

          Comment


          • #6
            In addition, the use of these function should be part of a command.

            Code:
            clear
            input str6 price_string
            " 3345"
            "3455 "
            " 2216"
            " 0000"
            "     "
            end
            strltrim(price_string)
            
            g new_price=strltrim(price_string)
            list, clean
            replace new_price=strtrim(price_string)
            list, clean
            ***********
            
            
            
            . clear
            
            . input str6 price_string
            
                 price_s~g
              1. " 3345"
              2. "3455 "
              3. " 2216"
              4. " 0000"
              5. "     "
              6. end
            
            . strltrim(price_string)
            unrecognized command:  strltrim
            r(199);
            
            
            . g new_price=strltrim(price_string)
            (1 missing value generated)
            
            . list, clean
            
                   price_~g   new_pr~e  
              1.       3345       3345  
              2.      3455       3455   
              3.       2216       2216  
              4.       0000       0000  
              5.                        
            
            . replace new_price=strtrim(price_string)
            (1 real change made)
            
            . list, clean
            
                   price_~g   new_pr~e  
              1.       3345       3345  
              2.      3455        3455  
              3.       2216       2216  
              4.       0000       0000  
              5.                        
            
            .

            Comment

            Working...
            X