0

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?

user159193
  • 21
  • 7

1 Answers1

0

Trace your loops. Set i to 1 and then

 cond_`i' 

becames

 cond_1 

and your replace command compares your variable with that as a literal string. That's legal but nothing will happen if that is not a (trimmed) value of your variable. The same is true for the other 16 cases, cond_2 to cond_17.

What you should have written is quite different:

forvalues i = 1/17 {
    generate condition_`i' = 0
    foreach dx of local cond_`i' {
        replace condition_`i' = 1 if strtrim(ICD9CODX) == "`dx'"
    }
}
Nick Cox
  • 35,529
  • 6
  • 31
  • 47