Claire
Claire

Reputation: 1

make new variable if ANY other variables equal ANY of a certain set of values

I want to create a new variable, called antidepressant. I have a list of drug codes that are considered antidepressants, but for each patient, these drug codes may show up in multiple variables across the dataset (because patients may be on multiple drugs at the same time). I want to make antidepressant=1 if ANY variable in the dataset (across the row) equals any of these drug codes (e.g. 123, 453, 859, 205). I know that I should make a numlist for the drug codes, but am not sure how to do the rest...

Upvotes: 0

Views: 1017

Answers (1)

Nick Cox
Nick Cox

Reputation: 37183

Any number of different solutions might be offered depending on your actual variable names, on which no information whatsover....

gen antidepressant = 0 

quietly foreach v in x1 x2 x3 x4 x5 { 
    replace antidepressant = 1 if inlist(`v', 123, 453, 859, 205) 
} 

For a review of pertinent technique, see this paper.

Upvotes: 1

Related Questions