Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    You would get a better answer if you presented an example using dataex (Stata 15.1 or SSC). Here's my guess at what you want:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str142 icd
    "[ICD-401.9] Ipertensione essenziale non specificata,[ICD-272.2] Iperlipidemia mista,[ICD-278.02] Sovrappeso,[ICD-427.31] Fibrillazione atriale"
    end
    moss icd, match("(\[[^[]+)") regex
    list _match*
    and the results:
    Code:
    . list _match*
    
         +-------------------------------------------------------------+
      1. |                                                 _match1     |
         |    [ICD-401.9] Ipertensione essenziale non specificata,     |
         |-------------------------------------------------------------|
         |                          _match2 |                  _match3 |
         | [ICD-272.2] Iperlipidemia mista, | [ICD-278.02] Sovrappeso, |
         |-------------------------------------------------------------|
         |                                        _match4              |
         |             [ICD-427.31] Fibrillazione atriale              |
         +-------------------------------------------------------------+

    Comment


    • #17
      Thank you so much Robert. Is there any way to extract just the actual ICD code?

      Manuel

      Comment


      • #18
        I was going to say just specify an expression for the numbers, but for all I know other numbers could occur in the data.

        You could try a different regular expression, but this is just to illustrate that other string functions can be used too. The main idea is just to look for square brackets.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str142 icd
        "[ICD-401.9] Ipertensione essenziale non specificata,[ICD-272.2] Iperlipidemia mista,[ICD-278.02] Sovrappeso,[ICD-427.31] Fibrillazione atriale"
        end
        moss icd, match("(\[[^[]+)") regex
        list _match*
        
        local j = 0 
        quietly foreach v of var _match* { 
            local ++j 
            gen code`j' = substr(`v', strpos(`v', "[") + 5, .) 
            replace code`j' = substr(code`j', 1, strpos(code`j', "]") - 1) 
        }
         
        . list code*
        
             +---------------------------------+
             | code1   code2    code3    code4 |
             |---------------------------------|
          1. | 401.9   272.2   278.02   427.31 |
             +---------------------------------+

        Comment


        • #19
          Thank you Nick, it works great. Just because my knowledge of coding for regular expressions is very limited, how would you generalize your code for instances where the code includes a letter (e.g. ICD-V34.6 I would like to isolate V34.6)

          Again, many thanks for your help.

          Comment


          • #20
            It's just the same problem so far as I can see: my code ignores ICD- so should still work regardless of this twist. Did you try it?

            You had a strong hint from Robert about giving examples in #16 and I think it's timely to underline that.

            Comment


            • #21
              Here's my example, revised to target the specific code:
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str142 icd
              "[ICD-401.9] Ipertensione essenziale non specificata,[ICD-272.2] Iperlipidemia mista,[ICD-278.02] Sovrappeso,[ICD-427.31] Fibrillazione atriale"
              end
              moss icd, match("\[ICD-([^]]+)\]") regex
              list _match*
              and the results
              Code:
              . list _match*
              
                   +---------------------------------------+
                   | _match1   _match2   _match3   _match4 |
                   |---------------------------------------|
                1. |   401.9     272.2    278.02    427.31 |
                   +---------------------------------------+

              Comment

              Working...
              X