Announcement

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

  • Help with subtracting more than one disease code from string variables

    I have a variable called "disease" that is of type string and that contains at the beginning the disease code, for example K00.1 but then it has the description and in some cases, it appears more than one disease having a ; as separator of the diagnoses.
    I need to extract in a different variable the diagnoses 1, diagnosis 2 and diagnosis 3 that appear.


    So far I managed to retrieve the first diagnosis with the command:

    gen diseases_1=substr( disease 1,5)

    But I don't know how to continue to indicate that after each ; continue extracting disease codes.
    I appreciate any guidance
    Regards



    copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id str939 disease
    1 "Z76.2-Consulta para atención y supervisión de la salud de otros niños o lactantes sanos (Nueva)  ;"      
    2 "Z00.1-Control de salud de rutina del niño (Nueva)  ;"        
    3 "Z00.1-Control de salud de rutina del niño (Repetida)  ;"        
    4 "Z00.1-Control de salud de rutina del niño (Nueva)  ;"        
    5 "Z76.2-Consulta para atención y supervisión de la salud de otros niños o lactantes sanos (Nueva)  ;"      
    6 "Z00.1-Control de salud de rutina del niño (Nueva)  ;"        
    7 "0-No Informado (Nueva)  ;"         
    8 "Z00.1-Control de salud de rutina del niño (Nueva)  ;"        
    9 "Z00.1-Control de salud de rutina del niño (Nueva)  ;"        
    10 "K12.2-Absceso Marginal (Nueva)  ;"         
    11 "Z00.1-Control de salud de rutina del niño (Nueva)  ;"        
    12 "Z01.6-Examen radiológico, no clasificado en otra parte (Nueva)  ;"        
    13 "G47-Trastornos del sueño (Repetida)  ;"        
    14 "G47-Trastornos del sueño (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    15 "Z01.4-Examen ginecológico (general) (de rutina) (Nueva)  ;"        
    16 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    17 "Z71.9-Consulta, no especificada (Repetida)  ;"         
    18 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;"        
    19 "F32-Episodio depresivo (Repetida)  ;  G47-Trastornos del sueño (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    20 "Z71.9-Consulta, no especificada (Nueva)  ;"         
    21 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    22 "I87.2-Insuficiencia venosa (crónica) (periférica) (Nueva)  ;"       
    23 "F32-Episodio depresivo (Repetida)  ;"         
    24 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    25 "Z10-Control general de salud de rutina de subpoblaciones definidas (Nueva)  ;"         
    26 "Z71.9-Consulta, no especificada (Repetida)  ;"         
    27 "G47-Trastornos del sueño (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    28 "G47-Trastornos del sueño (Repetida)  ;  F32.1-Episodio depresivo moderado (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    29 "F32-Episodio depresivo (Repetida)  ;"         
    30 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;"        
    31 "Z71.9-Consulta, no especificada (Repetida)  ;"         
    32 "L40-Psoriasis (Nueva)  ;"         
    33 "G47-Trastornos del sueño (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    34 "F32.0-Episodio depresivo leve (Repetida)  ;  G47-Trastornos del sueño (Repetida)  ;"        
    35 "F32.1-Episodio depresivo moderado (Repetida)  ;  G47-Trastornos del sueño (Nueva)  ;"        
    36 "G47-Trastornos del sueño (Nueva)  ;  F32-Episodio depresivo (Repetida)  ;"        
    37 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    38 "Z71.9-Consulta, no especificada (Nueva)  ;"         
    39 "G47-Trastornos del sueño (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    40 "G47-Trastornos del sueño (Repetida)  ;"        
    41 "F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"         
    42 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"        
    43 "L72-Quiste folicular de la piel y del tejido subcutáneo (Nueva)  ;"        
    44 "F32-Episodio depresivo (Repetida)  ;"         
    45 "F32-Episodio depresivo (Repetida)  ;  G47-Trastornos del sueño (Repetida)  ;"        
    46 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    47 "F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;"         
    48 "Z76.0-Consulta para repetición de receta (Nueva)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Repetida)  ;  G47-Trastornos del sueño (Repetida)    ;"
    49 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    50 "L40.0-Psoriasis vulgar (Nueva)  ;"         
    51 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;"        
    52 "F32-Episodio depresivo (Repetida)  ;"         
    53 "Z71.9-Consulta, no especificada (Nueva)  ;"         
    54 "Z71.9-Consulta, no especificada (Repetida)  ;"         
    55 "Z71.9-Consulta, no especificada (Nueva)  ;"         
    56 "F32-Episodio depresivo (Repetida)  ;"         
    57 "Z71.9-Consulta, no especificada (Nueva)  ;"         
    58 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;  F60.3-Trastorno de la personalidad emocionalmente inestable (Nueva)  ;"        
    59 "G47-Trastornos del sueño (Repetida)  ;  F32-Episodio depresivo (Repetida)  ;"        
    60 "0-No Informado (Nueva)  ;"         
    61 "T45.2- Vitaminas, no clasificadas en otra parte (Nueva)  ;"         
    62 "T45.2- Vitaminas, no clasificadas en otra parte (Nueva)  ;"         
    63 "Z10-8-Paciente Sano (Nueva)  ;"         
    64 "Z71.9-Consulta, no especificada (Nueva)  ;"         
    65 "T45.2- Vitaminas, no clasificadas en otra parte (Nueva)  ;"         
    66 "Z01.7-Examen de laboratorio (Nueva)  ;"         
    67 "M79.7-Fibromialgia (Repetida)  ;  E78-Dislipidemia (Nueva)  ;  I10-Hipertensión esencial (primaria) (Repetida)  ;"        
    68 "I10-Hipertensión esencial (primaria) (Nueva)  ;"        
    69 "T45.2- Vitaminas, no clasificadas en otra parte (Nueva)  ;"         
    70 "I10-Hipertensión esencial (primaria) (Repetida)  ;  E78-Dislipidemia (Repetida)  ;  E66-Obesidad (Repetida)  ;"        
    71 "0-No Informado (Nueva)  ;"         
    72 "M79.7-Fibromialgia (Repetida)  ;  I10-Hipertensión esencial (primaria) (Nueva)  ;"        
    73 "Z01.7-Examen de laboratorio (Nueva)  ;"         
    74 "0-No Informado (Nueva)  ;"         
    75 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    76 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    77 "M75.1-Síndrome del manguito rotatorio (Nueva)  ;"        
    78 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    79 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    80 "M75.1-Síndrome del manguito rotatorio (Nueva)  ;"        
    81 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    82 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    83 "M75.1-Síndrome del manguito rotatorio (Nueva)  ;"        
    84 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    85 "M75.1-Síndrome del manguito rotatorio (Nueva)  ;"        
    86 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    87 "M75.1-Síndrome del manguito rotatorio (Nueva)  ;"        
    88 "M75.1-Síndrome del manguito rotatorio (Nueva)  ;"        
    89 "M75.1-Síndrome del manguito rotatorio (Repetida)  ;"        
    90 "Z01.7-Examen de laboratorio (Nueva)  ;"         
    91 "I10-Hipertensión esencial (primaria) (Repetida)  ;"        
    92 "J45-Asma (Repetida)  ;"         
    93 "I10-Hipertensión esencial (primaria) (Repetida)  ;"        
    94 "Z71.8-Otras consultas especificadas (Nueva)  ;"         
    95 "Z76.0-Consulta para repetición de receta (Nueva)  ;  I10-Hipertensión esencial (primaria) (Repetida)  ;"       
    96 "J45-Asma (Repetida)  ;"         
    97 "J30.4-Rinitis alérgica, no especificada (Nueva)  ;  J45.0-Asma predominantemente alérgica (Repetida)  ;"       
    98 "Z71.8-Otras consultas especificadas (Repetida)  ;"         
    99 "J45-Asma (Repetida)  ;"         
    100 "I10-Hipertensión esencial (primaria) (Nueva)  ;"        
    end
    copy up to and including the previous line ------------------

  • #2
    Originally posted by MH Sandoval View Post
    . . . I don't know how to continue to indicate that after each ; continue extracting disease codes.
    I appreciate any guidance
    I recommend using split with the parse() option. Help file here or
    Code:
    help split

    Comment


    • #3
      I think -split- will be a good start, but it won't get you all the way home. The problem is that while your diseases are separated by semicolons, those semicolons also have a bunch of preceding and following blanks, which make it harder to extract the codes from the diseases. Moreover, even when that is taken care of, using the first 5 characters will not always get the right code, because some of the codes are shorter than that. I think the following is a complete solution:
      Code:
      split disease, parse(;)
      
      forvalues i = 1/3 {
          replace disease`i' = trim(itrim(disease`i'))
          gen code`i' = substr(disease`i', 1, strpos(disease`i', "-") - 1)
      }

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        I think -split- will be a good start, but it won't get you all the way home.
        Yeah, sorry about that. The OP asked how to extend extraction and so I gave the pointer to the command, assuming that he could take it from there.

        I thought that split would automatically trim the leading and trailing blank spaces unless the user specified the notrim option, but it seems that I misconstrued the help file. (I also thought that trim(), itrim() etc. wouldn't work with strings containing ó, ñ or é, but I guess that I'm mistaken there, too.)

        One suggestion, though. I noticed that the string length for the concatenated diagnoses is 939, much longer than what's represented in OP's data snippet. So, I suggest either:

        1. Modifying Clyde's helpful code as follows
        Code:
        forvalues i = 1/`r(k_new)' {
        (You can also rework Clyde's solution to refer to the list of new variables directly; they're provided by split in r(varlist).)

        2. Or reshaping the new variables long with something along the lines of
        Code:
        split disease, generate(d) parse(;)
        reshape long d, i(id) j(seq)
        drop if mi(d)
        generate str code = substr(strtrim(d), 1, strpos(d, "-") - 1)

        Comment


        • #5
          Thank you very much Joseph and Clyde for your help.
          Indeed, with the solution suggested by Clyde I can obtain the codes of up to 15 diseases appearing in the same line for a subject. Just change 3 by 15 in the command line you specify Joseph. Thank you both very much for the help.
          This solves my problem

          Best Regards

          Comment

          Working...
          X