Suppose you have the macro
global LabNames "3M" "ABBOTT" "MERCK SHARP DOHME"
I am using the quotes so that the words are correctly grouped (MERCK SHARP DOHME is one company, not three different ones). I am trying to write a program that goes over a variable and replaces it when it has one of the strings of LabNames as a substring.
Let us start with the part of the code that works fine.
foreach company of global LabNames {
display "`company'"
}
This code proceeds as needed in my case - lists 3 different companies (not 5). The following code, however, does not run correctly. It breaks down for 3M.
gen hasLab = 0
foreach company of global LabNames {
display "`company'"
replace hasLab = (index(lab,`"`company'"'))
replace lab = `"`company'"' if hasLab > 0
}
If we apply this code to
lab
asdf 3M
3M
ABBOTT
ABBOTT asdf
MERCK SHARP DOHME AS
MERCK SHARP DOHME 4
we get
lab
3M
asdf 3M
ABBOTT
ABBOTT
MERCK SHARP DOHME
MERCK SHARP DOHME
Would you know what to do so that the code can handle the 3M case correctly?