Stata remove entire word from string

Viewed 1126

I have a string variable where I want to remove certain words, but many other words would be a partial match, which I don't want to remove. I want to remove words, if and only if they are a complete match.

clear
* Add in some example data
input index str50 words
1 "more mor morph test"
2 "ten tennis tenner tenth keeper"
3 "badder baddy bad other"
end

* I create a copy to compare obefore/after strip
gen strip_words = words

* This is a list of words I want removed. In reality, this is a fairly long list
local removs "mor ten bad"
* For each of words, remove the complete word from teh string
foreach w of local removs {
    replace strip_words = subinstr(strip_words, "`w'","", .) 
}

list
     +---------------------------------------------------------------+
     | index                            words            strip_words |
     |---------------------------------------------------------------|
  1. |     1              more mor morph test            e ph test   |
  2. |     2   ten tennis tenner tenth keeper     nis ner th keeper  |
  3. |     3           badder baddy bad other         der dy other   |
     +---------------------------------------------------------------+

I've tried padding some spaces with replace strip_words = " " + strip_words + " ", but then this also removes the spaces separating the other words. My desired output would be

     +-------------------------------------------------------------------------+
     | index                            words                      strip_words |
     |-------------------------------------------------------------------------|
  1. |     1              more mor morph test              more  morph test    |
  2. |     2   ten tennis tenner tenth keeper    tennis tenner tenth keeper    |
  3. |     3           badder baddy bad other           badder baddy  other    |
     +-------------------------------------------------------------------------+
'''
3 Answers

See help string functions for subinword().

clear
* Add in some example data
input index str50 words
1 "more mor morph test"
2 "ten tennis tenner tenth keeper"
3 "badder baddy bad other"
end

* I create a copy to compare obefore/after strip
gen strip_words = words

* This is a list of words I want removed. In reality, this is a fairly long list
local removs "mor ten bad"
* For each of words, remove the complete word from teh string
foreach w of local removs {
    replace strip_words = subinword(strip_words, "`w'","", .) 
}

replace strip_words = itrim(strip_words) 

This can be handled with regular expressions. For an introduction: link

Stata's Unicode-based regex commands support \b to indicate word boundaries.

clear
input index str50 words
1 "more mor morph test"
2 "ten tennis tenner tenth keeper"
3 "badder baddy bad other"
end

local rmv "(mor|ten|bad)"
gen wanted = ustrregexra(words, "\b`rmv'\b", "")
list

     +----------------------------------------------------------------------+
     | index                            words                        wanted |
     |----------------------------------------------------------------------|
  1. |     1              more mor morph test              more  morph test |
  2. |     2   ten tennis tenner tenth keeper    tennis tenner tenth keeper |
  3. |     3           badder baddy bad other           badder baddy  other |
     +----------------------------------------------------------------------+

From your example it seems you want to keep spaces as above. Otherwise you can remove them with strtrim() and stritrim().

Using your example but using subinword instead of subinstr you can get your desired output.

clear
* Add in some example data
input index str50 words
1 "more mor morph test"
2 "ten tennis tenner tenth keeper"
3 "badder baddy bad other"
end

* I create a copy to compare obefore/after strip
gen strip_words = words
gen strip_words_2 = words

* This is a list of words I want removed. In reality, this is a fairly long list
local removs "mor ten bad"
* For each of words, remove the complete word from teh string
foreach w of local removs {
    replace strip_words   = subinstr(strip_words, "`w'","", .) 
    replace strip_words_2 = subinword(strip_words_2,"`w'","",.)
    }

list
     

     +-------------------------------------------------------------------------------------------+
     | index                            words          strip_words                 strip_words_2 |
     |-------------------------------------------------------------------------------------------|
  1. |     1              more mor morph test           e  ph test              more  morph test |
  2. |     2   ten tennis tenner tenth keeper    nis ner th keeper    tennis tenner tenth keeper |
  3. |     3           badder baddy bad other        der dy  other           badder baddy  other |
     +-------------------------------------------------------------------------------------------+
     
     
     
Related