Dear Forum Members,
I recently came across an issue when trying to replace multiple words (by nothing, i.e., I need to exclude the words). In short, I wish to create a variable, say, "Country", and this is from the variable "region" whose part of the string presents the name of the country.
Now, if I exclude in a one-by-one basis, the regexr command works. But if I try:
I get this:
Besides that, I didn't find a way to exclude "(Kanpur)" (at the same as the other words) as well.
Thank you in advance for any helpful code.
I recently came across an issue when trying to replace multiple words (by nothing, i.e., I need to exclude the words). In short, I wish to create a variable, say, "Country", and this is from the variable "region" whose part of the string presents the name of the country.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str21 region "Northern Ethiopia" "Ethiopia" "Uganda" "Southwestern Ethiopia" "Western Ethiopia" "Pakistan" "Northeastern Ethiopia" "Southern Ethiopia" "Ethiopia" "Ethiopia" "Ethiopia" "Ethiopia" "Southern India" "Ethiopia" "Eastern Ethiopia" "Egypt" "Ethiopia" "Central Ethiopia" "Nigeria" "Northwestern Ethiopia" "Pakistan" "India" "Pakistan" "Northwestern Ethiopia" "Northern Ethiopia" "Ethiopia" "Northwestern Ethiopia" "Southeastern Nigeria" "Turkey" "South India" "Northwestern Ethiopia" "India" "Southeastern Nigeria" "Philippines" "India" "Northwestern Ethiopia" "India" "Nigeria" "India" "Ghana" "Ethiopia" "Ghana" "India (Kanpur)" "Iraq" "Ethiopia" "Uganda" "Ethiopia" "El Salvador" "Ethiopia" "Brazil" "Philippines" end
Code:
. gen Country=regexr(region, "Northeastern|Central|Eastern|Northern|Northwestern|South|Southern|Southwestern|Southeastern|Western", "")
Code:
. tab Country Country | Freq. Percent Cum. -----------------+----------------------------------- Ethiopia | 11 21.57 21.57 India | 1 1.96 23.53 Brazil | 1 1.96 25.49 Egypt | 1 1.96 27.45 El Salvador | 1 1.96 29.41 Ethiopia | 12 23.53 52.94 Ghana | 2 3.92 56.86 India | 5 9.80 66.67 India (Kanpur) | 1 1.96 68.63 Iraq | 1 1.96 70.59 Nigeria | 2 3.92 74.51 Pakistan | 3 5.88 80.39 Philippines | 2 3.92 84.31 Turkey | 1 1.96 86.27 Uganda | 2 3.92 90.20 eastern Nigeria | 2 3.92 94.12 ern Ethiopia | 1 1.96 96.08 ern India | 1 1.96 98.04 western Ethiopia | 1 1.96 100.00 -----------------+----------------------------------- Total | 51 100.00
Thank you in advance for any helpful code.
Comment