Announcement

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

  • Generating new variable using hospital codes of multiple diagnosis

    Hello,

    I have a dataset with patient ids. Each patient is in the dataset multiple times and have multiple diagnosis. I would like to code out a new variable for specific codes so that I can retain those patients. The criteria will apply to any of the multiple diagnosis. The diagnoses are string variables and id is numerical. Below is my data example and the code I am trying to use but to no avail. I would prefer not to repeat the coding for each variable separately as in my regional dataset, I have 100 diagnoses and 75 codes. I appreciate your help.


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str5(diag1 diag2 diag3 diag4 diag5)
     0 "22.3" "20"   "10"   "44"   "23.3"
     1 "33.3" "16.2" "16.2" "20.1" "14"  
     2 "20.1" "20.1" "33.3" "14.5" "16.2"
     3 "13.2" "17.9" "12.0" "13"   "12.0"
     4 "15"   "12.0" "13.2" "16.2" "33.3"
     5 "12"   "18.4" "14.5" "12.0" "16.2"
     6 "33.3" "10"   "16.2" "13.2" "10"  
     7 "19"   "33.3" "14.5" "10"   "20.1"
     8 "28.9" "14.5" "33.3" "16.2" "20.1"
     9 "13.2" "16.2" "14.5" "10"   "17"  
    10 "13.2" "20.1" "16.2" "13.2" "13.2"
    end
    ------------------ copy up to and including the previous line ------------------

    [/CODE]
    gen New=1 if diag1|diag2|diag3|diag4|diag5=="33.3"|"20.1" |"13.2"|"17"

  • #2
    your attempted code is illegal in several senses; also, it gives a 1/missing variable and generally a 1/0 variable will be more useful; here is one attempt and the result:
    Code:
    . gen byte New=inlist(diag1,"33.3","20.1","13.2","17") | inlist(diag2,"33.3","20.1","13.2","17") | inlist(diag3,"33.3","20.1","13.2","17") | inlist(diag4,"33.3","20.1","13.2","17") | inlist(diag5,"33.3","20.1","13.2","17")
    
    . li, clean noo
    
        id   diag1   diag2   diag3   diag4   diag5   New  
         0    22.3      20      10      44    23.3     0  
         1    33.3    16.2    16.2    20.1      14     1  
         2    20.1    20.1    33.3    14.5    16.2     1  
         3    13.2    17.9    12.0      13    12.0     1  
         4      15    12.0    13.2    16.2    33.3     1  
         5      12    18.4    14.5    12.0    16.2     0  
         6    33.3      10    16.2    13.2      10     1  
         7      19    33.3    14.5      10    20.1     1  
         8    28.9    14.5    33.3    16.2    20.1     1  
         9    13.2    16.2    14.5      10      17     1  
        10    13.2    20.1    16.2    13.2    13.2     1

    Comment


    • #3
      I like Rich's straightforward approach, but because inlist() is limited to 10 string arguments, note that you will need to repeat it several times to handle the different diagnosis variables and codes. Another solution would involve a loop over variables and codes:
      Code:
      // Construct long lists on multiple lines for convenience
      local dlist diag1 diag2
      local dlist `dlist' diag3 diag4 diag5
      local clist 33.3 20.1
      local clist `clist' 13.2 17
      //
      gen byte wanted = 0
      foreach d of varlist `dlist' {
         foreach c of local clist {
            replace wanted = 1 if (`d' == "`c'")
         }
      }
      Another possibility would be to -reshape long-, so that there is one diagnosis variable with multiple observations per patient. For the current purpose, that probably isn't any easier, but it might be if you end up needing to use the long format anyway.

      Comment


      • #4
        Thank you Rich and Mike for responding quickly and offering very useful solutions. I started using Mike's code because it's simple enough for what I need and requires much less work. Mike, when I run the second part of the card starting with "generate" code with my variables, I get the following procedure error message;

        varlist required

        Do you know what I am doing wrong? Thank you.

        May

        Comment


        • #5
          I ran Rich's code with the data you show in #1, and I cannot reproduce your problem.

          The varlist required error often shows up in a -foreach v of varlist `some_local_macro'- command when the local macro is empty. Now, clearly local macro dlist has been defined in the code. But if you are running this code line by line or in chunks, bear in mind that once the end of the chunk is reached, any local macros that were defined in that chunk disappear. If you then try to use one of those local macros in a subsequent chunk of code, it won't be there, and in this context, would produce exactly the error message you are seeing. The solution is to run all the code all in one pass--do not run it in pieces.

          In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

          Comment


          • #6
            Thank you Clyde, that worked perfectly. I was running the code in bits before.

            Comment

            Working...
            X