Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Using regexs/regexm to extract subjects with certain ICD codes.

From   Amal Khanolkar <>
To   "" <>
Subject   st: Using regexs/regexm to extract subjects with certain ICD codes.
Date   Mon, 4 Jun 2012 09:12:10 +0000

Hi All,

Originally, I started using the 'regex' command to extract ICD codes from my variables of interest shown below (mdiag1, mdiag2, mdiag3, mdiag4 etc....). I'm extracting the same ICD codes from all the mdiag variables starting with the numbers/letters: 637, 642 and O1. Initially I extracted the ICD codes from each mdiag variable separately with the idea of combining them at the end. But that seems a bit more complicated then I initially thought. Maybe, one solution could be to extract the ICD codes from all mdiag variables at the same time. There are 12 such mdiag variables. I've shown the codes below, that I initially used to extract ICD codes from each mdiag variable separately.

gen preght1 = regexs(0) if regexm(mdiag1, "^(637|642|O1)")
                        tab preght1
                        gen preght2 = regexs(0) if regexm(mdiag2, "^(637|642|O1)")
                        tab preght2
                        gen preght3 = regexs(0) if regexm(mdiag3, "^(637|642|O1)")
                        tab preght3
                        gen preght4 = regexs(0) if regexm(mdiag4, "^(637|642|O1)")
                        tab preght4
                        gen preght5 = regexs(0) if regexm(mdiag5, "^(637|642|O1)")
                        tab preght5
                        gen preght6 = regexs(0) if regexm(mdiag6, "^(637|642|O1)")
                        tab preght6
                        gen preght7 = regexs(0) if regexm(mdiag7, "^(637|642|O1)")
                        tab preght7
                        gen preght8 = regexs(0) if regexm(mdiag8, "^(637|642|O1)")
                        tab preght8 

The above generates 8 preght variables and works great.

Initially I tried to combine the (mdiagX, "^(637|642|O1) for each mdiag variable by enclosing them in separate brackets one after another. But it doesn't work. How do I modify the regexs/regexm commands to be able to tell Stata to pluck out the ICD codes for several variables in the same command line?


Amal Khanolkar, PhD candidate,

Centre for Health Equity Studies (CHESS),

Karolinska Institutet,

106 91 Stockholm.

Ph# +46(0)8 162584/+46(0)73 0899409

*   For searches and help try:

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