Announcement

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

  • Drop/keep string observations that contain specific word

    Hi Everyone

    I have a basic question, which I still have not been able to solve. How can you delete observations from a variable that contains strings that have the specific word for instance.

    For example, I have a variable of jobtitle and the observations can vary
    "CEO"
    "Chief Executive Officer"
    "President"
    "President & CEO"
    "President & Chief Executive Officer"
    "Chairman"
    "CFO"
    "Chief Financial Officer"
    "Chairman & Chief Executive Officer"

    Then how could I drop all the observations that contain the word "financial"/"CFO" for example?
    Or is there any way I could keep only observations that contain a specific word such as "CEO" and "Chief Executive Officer" for example?
    Since there are a lot of variations in this variable and I just want to include the one that is CEOs.

    Thank you!


  • #2
    First question
    drop if strpos(jobtitle,"CFO")|strpos(jobtitle,"Financial" )


    Second question
    keep if strpos(jobtitle,"CEO") | strpos(jobtitle,"Chief Executive Officer")


    hth,
    Jeph

    Comment


    • #3
      Here is an example based on your example data.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str37 jobtitle
      "CEO"                                
      "Chief Executive Officer"            
      "President"                          
      "President & CEO"                    
      "President & Chief Executive Officer"
      "Chairman"                           
      "CFO"                                
      "Chief Financial Officer"            
      "Chairman & Chief Executive Officer" 
      end
      
      generate todrop = 0
      replace todrop = 1 if strpos(lower(jobtitle), "financial")>0
      replace todrop = 1 if strpos(lower(jobtitle), "cfo")>0
      list, clean
      drop if todrop
      Code:
      . generate todrop = 0
      
      . replace todrop = 1 if strpos(lower(jobtitle), "financial")>0
      (1 real change made)
      
      . replace todrop = 1 if strpos(lower(jobtitle), "cfo")>0
      (1 real change made)
      
      . list, clean
      
                                        jobtitle   todrop  
        1.                                   CEO        0  
        2.               Chief Executive Officer        0  
        3.                             President        0  
        4.                       President & CEO        0  
        5.   President & Chief Executive Officer        0  
        6.                              Chairman        0  
        7.                                   CFO        1  
        8.               Chief Financial Officer        1  
        9.    Chairman & Chief Executive Officer        0  
      
      . drop if todrop
      (2 observations deleted)

      Comment

      Working...
      X