I want to shorten the following code. How do I go about it? In essence, I would like to create another global called total_wardlist that would have the global macros mentioned below: i.e. wardlist_01, wardlist_02, wardlist_03 and wardlist_04.
foreach x of global wardlist_01{
display "Checking for `x'"
replace name_colony6_wardname = "`x'" if (strpos(name_colony6, "`x'") > 0)
}
foreach x of global wardlist_02{
display "Checking for `x'"
replace name_colony6_wardname = "`x'" if (strpos(name_colony6, "`x'") > 0)
}
foreach x of global wardlist_03{
display "Checking for `x'"
replace name_colony6_wardname = "`x'" if (strpos(name_colony6, "`x'") > 0)
}
foreach x of global wardlist_04{
display "Checking for `x'"
replace name_colony6_wardname = "`x'" if (strpos(name_colony6, "`x'") > 0)
}
foreach x of global wardlist_05{
display "Checking for `x'"
replace name_colony6_wardname = "`x'" if (strpos(name_colony6, "`x'") > 0)
}
I tried the following code but it returns "0 real changes made" when there should have been changes. Please advise.
global total_wardlist wardlist_01 wardlist_02 wardlist_03 wardlist_04 wardlist_05
foreach x of global $total_wardlist{
display "Checking for `$x'"
replace name_colony6_wardname = "`$x'" if (strpos(name_colony5, "`$x'") > 0)
}
Let's set aside a widespread aversion to and avoidance of global macros by experienced Stata programmers. General advice is to use local macros to the maximum extent possible.
You could loop over those macros:
If you preferred that, I would indent the inner loop.
Or you could combine them first:
Code not checked, but fairly confident. The second solution shows up two kinds of errors in your attempted code.