Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: gen new variable from string variables

From   Joseph Coveney <>
To   Statalist <>
Subject   Re: st: gen new variable from string variables
Date   Tue, 26 Apr 2005 12:50:35 +0900

Belinda L King wrote:

I am wanting to create a variable that tells me whether the patient has an
ICD10 starting with E11. I am not interested in the numbers after the dots,
nor am I interested in any of the other ICD10 codes and I am wanting to drop
patients who do not have E11 in one of the ICD10 columns. I have tried
playing with foreach, but this side of things is new for me and I just keep
getting messages telling me the syntax is incorrect. I would greatly
appreciate any hints someone could give me, thank you in advance.


First, generate a flag (indicator) variable with the value set to zero as a
default.  Then, use -index()- to find patients whose ICD10 codes contain
"E11".  Set the flag to one if one or more ICD10 codes (variables) contain
"E11".  Keep rows for those patients with the flag set, and then drop the

The approach is illustrated below after the "Begin here" comment.  (The
first part is just to enter your dataset.)

Joseph Coveney

tempfile tmpfil0
input str200 v1
"rec id icd10_1 icd10_2 icd10_3"
 "1. 1 K61.3 Z86.43 F05.9"
 "2. 2 B95.8 Z06.2 Z86.43"
 "3. 3 E11.69 R40.2 E11.9"
 "4. 4 Z86.43 Z95.8 E87.6"
 "5. 5 K59.0 K59.0 Z93.1"
 "6. 6 E11.65 E11.66 R63.4"
 "7. 7 E11.22 E66.9 E11.23"
 "8. 8 E11.9 E78.0 K63.50"
 "9. 9 E78.0 E11.65 D50.9"
 "10. 10 E11.65 K59.0 Z93.0"
replace v1 = subinstr(v1, " ", ", ", .)
outsheet using `tmpfil0', nonames noquote
insheet using `tmpfil0', names comma clear
erase `tmpfil0'
drop rec
* Begin here
generate byte has_E11 = 0
forvalues i = 1/3 {
   replace has_E11 = has_E11 | (index(icd10_`i', "E11"))
slist, noobs
keep if has_E11
drop has_E11

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index