Announcement

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

  • #16
    If that works, this should too, which is just what was mentioned earlier.

    Sorry, but I can't diagnose errors in code you don't show me.

    Code:
    gen CardArrythm1 = 0
    
    forval j = 1/20 {
               replace CardArrythm1 = 1 if DX`j' =="427" & DXPOA`j' == "Y"
    }

    Comment


    • #17
      In the code the you have given me

      gen CardArrythm1 = 0 forval j = 1/20 { replace CardArrythm1 = 1 if DX`j' =="427" & DXPOA`j' == "Y" } is there a way to search for more variables? for example, what if I want to look for 427, 428, 429, 430, instead of only 427 ? how can I adjust the code to look for those numbers in one code.

      Comment


      • #18
        Originally posted by Nick Cox View Post
        If that works, this should too, which is just what was mentioned earlier.

        Sorry, but I can't diagnose errors in code you don't show me.

        Code:
        gen CardArrythm1 = 0
        
        forval j = 1/20 {
        replace CardArrythm1 = 1 if DX`j' =="427" & DXPOA`j' == "Y"
        }

        is there a way to search for more variables? for example, what if I want to look for 427, 428, 429, 430, instead of only 427 ? how can I adjust the code to look for those numbers in one code.

        Comment


        • #19
          forval j = 1/20 {
          replace CardArrythm1 = 1 if DX`j' =="x1" & DXPOA`j' == "Y" | DX`j' =="x2" & DXPOA`j' == "Y" || DX`j' =="x3" & DXPOA`j' == "Y"
          }

          what do you think of this code?

          Comment


          • #20
            forval j = 1/20 {
            replace CardArrythm1 = 1 if DX`j' =="x1" & DXPOA`j' == "Y" | DX`j' =="x2" & DXPOA`j' == "Y" || DX`j' =="x3" & DXPOA`j' == "Y"
            }

            what do you think of this code?
            The || is illegal. Presumably you mean just |. With that corrected, your code will replace CardArrythm1 with 1 in those observations where DXPOA`j' is "Y" and DX`j' is any of "x1", "x2", or "x3" . Assuming that's what you want, a simpler way to accomplish the same thing is:

            Code:
            forvalues j = 1/20 {
                 replace CardArrythm1 = 1 if inlist(DX`j', "x1", "x2", "x3") & DXPOA`j' == "Y"
            }

            Comment


            • #21
              I'm a bit late to the party but it seems to me like this is better handled with data in long form and matching dx codes using a master list of diseases. I'm making a bunch of assumptions about what the OP is trying to do but it could look something like:

              Code:
              * Create a list of diseases and the codes associated with it.
              * The disease name should be a valid variable name.
              * For each disease, dx codes can only appear once.
              * A dx code can be associated with multiple diseases (e.g. V533)
              clear
              input str20(disease dx)
              com_arrhythmia 4260
              com_arrhythmia 42610
              com_arrhythmia 42611
              com_arrhythmia 42612
              com_arrhythmia 42613
              com_arrhythmia 4267
              com_arrhythmia 4269
              com_arrhythmia 4270
              com_arrhythmia 4271
              com_arrhythmia 4272
              com_arrhythmia V533
              rare_arrhythmia 4279
              rare_arrhythmia V450
              rare_arrhythmia V533
              headache 12345
              headache 56378
              end
              tempfile diseases
              save "`diseases'"
              
              * verify that each disease name can be used as a variable name
              assert disease == strtoname(disease)
              
              * verify that codes are unique within a disease
              isid disease dx, sort
              
              
              * generate some data in the form described in the original post
              clear
              input id str15(dx1 dx2 dx3 dx4 dxpoa1 dxpoa2 dxpoa3 dxpoa4)
              1 1234 56738 V533 1234 Y N Y N
              2 12345 56678 910411 1234 Y N Y N
              3 12346 56378 913011 1234 Y N Y N
              4 12347 56478 910211 1234 Y N Y N
              5 12384 56278 910211 1234 Y N Y N
              6 4260 42610 42611 42612 Y N Y N
              7 42613 4267 4269 4270 Y N Y N
              8 V450 4272 56378 4279 Y N Y N
              end
              tempfile raw
              save "`raw'"
              
              * This is easier to do if patient data is in long form
              reshape long dx dxpoa, i(id) j(n)
              
              * reduce to codes with "Y"
              keep if dxpoa == "Y"
              
              * check that codes appear only once per patient
              isid id dx, sort
              
              * match codes with diseases; use joinby since dx codes can
              * be associate with multiple diseases
              joinby dx using "`diseases'"
              
              * create indicator variables for each disease
              keep id disease
              levelsof disease, clean
              local dlist `r(levels)'
              
              foreach dx of local dlist {
                  gen `dx' = disease == "`dx'"
              }
              
              * reduce to one observation per patient
              collapse (max) `dlist', by(id)
              list

              Comment

              Working...
              X