The inbuilt strpos()
will yield a positive value if a string is found inside another. Building on that
gen breakfast = strpos(meal, "bacon") | strpos(meal, "eggs")
and so forth. In practice, working with a string made lower case will often help, or indeed be essential. Also, if you have a long list, you may prefer
gen breakfast = 0
quietly foreach thing in bacon eggs cereal "orange juice" {
replace breakfast = breakfast | strpos(lower(meal), `"`thing'"')
}
The principle here is using | (or) as a logical operator, yielding 1 (true) if any argument is non-zero. Note that lower()
is included to compare with a lower case version of the original.
This technique is naturally not robust to spelling mistakes or small variations in wording.