Announcement

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

  • Remove some characters (at the end) from string

    Hi!

    I am trying to remove some chracters which are generally at the end of strings.
    It looks like this:

    AGRITOPE INC
    AGRIUM CORP
    AGWAY CO
    AHI HEATH SYSTEMS INC

    I want to remove the last things such as INC, COPR, CO....
    I tried subinword, but I think I need some more improve.

    Can anyone help me?

    Best,
    Sang-Min


  • #2
    Here's one approach.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str30 text
    "AGRITOPE INC"        
    "AGRIUM CORP"          
    "AGWAY CO"            
    "AHI HEATH SYSTEMS INC"
    "LOCO COLOR CO TODAY"
    end
    replace text = regexr(text," INC$","")
    replace text = regexr(text," CORP$","")
    replace text = regexr(text," CO$","")
    list, clean
    Code:
    . list, clean
    
                          text  
      1.              AGRITOPE  
      2.                AGRIUM  
      3.                 AGWAY  
      4.     AHI HEATH SYSTEMS  
      5.   LOCO COLOR CO TODAY
    Added: Crossed with #3, from which I draw inspiration for the following improvement to the code above.
    Code:
    local to_remove INC CORP CO
    foreach t of local to_remove {
        replace text = regexr(text,`" `t'$"',"")
        }
    Last edited by William Lisowski; 30 Apr 2018, 13:42.

    Comment


    • #3
      -subinword()- will at least prevent you from replacing "SINCLAIR CORP" WITH "SLAIR CORP". But it won't protect you against the rare possibility that CO (or one of the others) appears as a word within the name in a non-final position. So, for example, subinword() would change "JAN CO SMITH AND WADE CORP" to "JAN SMITH AND WADE CORP".

      I think what you want to do is this

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str26 name
      "AGRITOPE INC"              
      "AGRIUM CORP"              
      "AGWAY CO"                  
      "AHI HEATH SYSTEMS INC"    
      "JAN CO SMITH AND WADE CORP"
      end
      
      list
      
      local to_remove INC CORP CO
      gen rname = reverse(name)
      foreach t of local to_remove {
          local trev = reverse(`"`t'"')
          replace name = reverse(subinword(rname, `"`trev'"', "", 1))  ///
              if strpos(rname, `"`trev'"') == 1
      }
      drop rname
      
      list
      In the future, when asking for help with code, please use the -dataex- command show example data If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      When asking for help with code, always show example data. When showing example data, always use -dataex-.

      Added: Crossed with #2, which shows a different way of doing it.

      Comment


      • #4
        See also https://www.statalist.org/forums/for...-a-local-macro for the same problem and yet other solutions.

        Comment

        Working...
        X