Hello, I'm working on building a -foreach- statement in hopes of making more efficient the search criteria for a -replace- command. Below is my code, in which I examine the variable plicd for specific values, comparing two methods:
1. Using -foreach- to make variable tagfor ==1 whenever the search criteria are satisfied. And,
2. Using -replace- to make variable tagman==1 whenever the search criteria are satisfied.
For my -foreach- code, I used as a template Nick Cox's FAQ at
. I
My questions are:
1. My observation is that in my choice of search criteria, there's no advantage over a manual search. Am I not using -foreach- correctly?
2. If I want to specify a range of values for variable plicd (e.g. I21.0 to I21.3), how would I specify that in each of my two methods?
3. Note that the value of "I25.3" is captured, even though in my -index- function I specify only "I25.3" for the "I25.x" range.
1. Using -foreach- to make variable tagfor ==1 whenever the search criteria are satisfied. And,
2. Using -replace- to make variable tagman==1 whenever the search criteria are satisfied.
For my -foreach- code, I used as a template Nick Cox's FAQ at
HTML Code:
http://www.stata.com/support/faqs/data-management/try-all-values-with-foreach/
My questions are:
1. My observation is that in my choice of search criteria, there's no advantage over a manual search. Am I not using -foreach- correctly?
2. If I want to specify a range of values for variable plicd (e.g. I21.0 to I21.3), how would I specify that in each of my two methods?
3. Note that the value of "I25.3" is captured, even though in my -index- function I specify only "I25.3" for the "I25.x" range.
Code:
clear set obs 10 input mrn tagfor str8 plicd 1 . "I21.0" 2 . "I21.1" 3 . "I22.3" 4 . "I25.2" 5 . "I25.3" 6 . "I26.4" end l, noo egen group = group(plicd) su group, meanonly summ group, detail foreach i of num 1/`r(max)' { replace tagfor=1 if index(plicd, "I21.*") | /// index(plicd, "I22.*") | /// index(plicd, "I25.2") } gen tagman = 1 if index(plicd, "I21.*") | /// index(plicd, "I22.*") | /// index(plicd, "I25.2") replace tagman=0 if tagman!=1 & tagman!=. l mrn plicd group tagfor tagman, noo
Comment