Announcement

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

  • Loop with a condition

    Dear All

    First time poster. Using Stata 15.1 on Windows.

    I am doing a epidemiologic register study on adverse events in colonoscopy. So for each colonoscopy I want to check if the same individual/same id came in with a bleeding or perforation the next 30 days. The data sett is very large with 32 million entries in two files.

    This is the code I found that I was trying out, just to do a simple loop. What I want to do is just when colonoscopy == 1 then do a lot more only if colonoscopy == 1.

    foreach var in colonoscopy {
    if _rc==0 {
    //Check if the same id has an Perforation the next 30 days.
    //Only if colonoscopy == 1
    }
    }

    But it it enters the loop even if colonoscopy is blank or 0.
    How can i do this so it enters only if colonoscopy is equal to 1?

    Here is sample of the dummy test dataset.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id float(colonoscopy Perforation)
    1 . .
    1 . .
    1 . .
    2 . .
    2 . .
    2 1 .
    2 . .
    2 . 1
    2 . 1
    2 . .
    2 . .
    2 . .
    2 . .
    2 . .
    2 . .
    2 . .
    2 . .
    1 . .
    3 . .
    3 . .
    end


    Thanks in advance
    Bjarki



  • #2
    Well, with no date variable, it cannot be done. I will assume that your real data set includes a variable, called date, that gives the date of the events shown in each observation.

    The use of 1/missing value to represent dichotomous variables is common in spreadsheets, and even works well there. In Stata it is a recipe for trouble. So the first step is to convert those missing values to zeroes so we have a nice Stata-friendly boolean 0/1 variable.

    Code:
    //  USE 1/0 REPRESENTATION, NOT 1/. FOR DICHOTOMOUS VARIABLES
    mvencode colonoscopy Perforation, mv(0)
    
    //  CREATE A SEPARATE TEMPORARY FILE OF PERFORATION EVENTS
    preserve
    keep if Perforation
    drop colonoscopy
    tempfile perfs
    save `perfs'
    
    //  JOIN THE RECORDS WITH PERFORATIONS BETWEEN SAME DAY AND
    //  30 DAYS LATER
    restore
    drop Perforation
    rangejoin date 0 30 using `perfs', by(id)
    
    //
    //  RETAIN ONLY THE PAIRINGS THAT BEGIN WITH A COLONOSCOPY
    drop if !colonoscopy
    rename date_U perforation_date
    gen byte had_perf_within_30_days = !missing(perforation_date)
    -rangejoin- is written by Robert Picard and is available from SSC. To use it, you must also install -rangestat-, by Robert Picard, Nick Cox, and Roberto Ferrer, also available from SSC.

    Note I assumed that within 30 days includes the day of the colonoscopy itself and extends to the date 30 days later. If that is not what you intended you will need to modify the numbers given in the -rangejoin- command accordingly.

    Comment


    • #3
      I agree with @Clyde Schechter's general advice. Here's another way to think about it using rangestat from SSC.


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte id float(colonoscopy Perforation)
      1 . .
      1 . .
      1 . .
      2 . .
      2 . .
      2 1 .
      2 . .
      2 . 1
      2 . 1
      2 . .
      2 . .
      2 . .
      2 . .
      2 . .
      2 . .
      2 . .
      2 . .
      1 . .
      3 . .
      3 . .
      end
      
      gen time = 20000 + 11 * _n 
      
      mvencode colon Perf, mv(0)
      
      gen when = time if colon 
      bysort id (time) :replace when = when[_n-1] if missing(when)
      
      rangestat (sum) Perf, int(time 0 30) by(id when)
      
      list if id == 2, sepby(when)
      
           +-----------------------------------------------------+
           | id   colono~y   Perfor~n    time    when   Perfor~m |
           |-----------------------------------------------------|
        5. |  2          0          0   20044       .          0 |
        6. |  2          0          0   20055       .          0 |
           |-----------------------------------------------------|
        7. |  2          1          0   20066   20066          1 |
        8. |  2          0          0   20077   20066          2 |
        9. |  2          0          1   20088   20066          2 |
       10. |  2          0          1   20099   20066          1 |
       11. |  2          0          0   20110   20066          0 |
       12. |  2          0          0   20121   20066          0 |
       13. |  2          0          0   20132   20066          0 |
       14. |  2          0          0   20143   20066          0 |
       15. |  2          0          0   20154   20066          0 |
       16. |  2          0          0   20165   20066          0 |
       17. |  2          0          0   20176   20066          0 |
       18. |  2          0          0   20187   20066          0 |
           +-----------------------------------------------------+

      Comment


      • #4
        OMG! This worked right away. This is like LOTR Gandalf stuff.

        Thank you soo much!
        Bjarki

        Comment

        Working...
        X