问题
I am trying to find a word from a column in Stata that could appear in all caps or all lower case.
foreach varlist_cust in "xyz" "XYZ" "XyZ" {
replace cus_tag = strpos(customer_name, "`varlist_cust'") if cus_tag==0
}
Is there a more efficient method of conducting this analysis? Maybe with the use of regular expressions?
回答1:
No need for regular expressions as Stata has a built in lower()
function. See, for example
clear
input str9 customer_name
"Ander2Ed"
"sonu"
"abcXyZcba"
"XYZ"
"zXyZ"
end
gen cus_tag = strpos(lower(customer_name), "xyz")
Simply evaluate the customer_name
variable as lowercase against the lowercase values you are looking for.
see help lower
and help string functions
for more.
来源:https://stackoverflow.com/questions/38921539/matching-strings-that-appear-in-both-lower-and-upper-case-or-are-a-combination