In my data, I have a string variable ICD9CODX
like the one below:
|ICD9CODX|
|:-------|
|410 |
|411 |
|398 |
|196 |
I would like to search/match each observation within some code sets defined as Stata local macros. So if the code belongs to a set, a variable that flags the existence of codes for that set is created and set to value 1
, and 0
otherwise.
Examples of my local macros are the following:
local cond_1 410 411
local cond_2 398 402 428
...
local cond_17 196 197 198 199
Expected result:
|ICD9CODX|condition_1|condition_2|........|condition_17|
|:-------|----------:|----------:|--------|-----------:|
|410 | 1| 0|........| 0|
|411 | 1| 0|........| 0|
|398 | 0| 1|........| 0|
|196 | 0| 0|........| 1|
I tried using the loops below but 0
real changes were made:
forvalues i = 1/17 {
generate condition_`i' = 0
foreach dx in cond_`i' {
replace condition_`i' = 1 if strtrim(ICD9CODX) == "`dx'"
}
}
What is wrong with my loops?