Announcement

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

  • Loop- overlaps of dates

    Dear users,

    I have put together a code in Stata which identefies overlaps between dates in my dataset (see below). Unfortanately the code doesnt run for all observation. Can someone guide me on how to change my codes so that it workf for all patients.

    Many thanks,
    Ivy


    bysort id: gen nnum=_n
    bysort id: gen nnum2=_N

    format adm %d
    format dis %d

    gen curadm=adm
    format curadm %d
    gen curdis=dis
    format curdis %d


    forvalues i=2/`=nnum2' {
    local lastobs=`i'-1
    *if id[`i']!=id[`i'-1] {
    * replace curadm=adm in `i'
    * replace curdis=dis in `i'
    *replace curadm=curadm[`i'-1] in `lastobs'
    *replace curdis=curdis[`i'-1] in `lastobs'
    *}

    //if the ith discharge date is less than the previous discharge date
    //then replace the ith discharge date with the previous discharge date

    else {
    if dis[`i']<curdis[`i'-1] {
    replace curdis=curdis[`i'-1] in `i'
    }

    // if this is not the case then keep the current discharge

    else {
    replace curdis=dis in `i'
    }
    }

    // if the admission date for the ith discharge is less than or
    // equal to the previous discharge date
    //then replaces the ith admission date with the previous admission date
    //replace all discharge dates with a missing value except the final discharge
    //in the overlap set

    if adm[`i']<=curdis[`i'-1] {
    replace curadm=curadm[`i'-1] in `i'
    replace curdis=. in `lastobs'

    // if this is not true then keep the current admission date and
    //current discharge

    else{
    replace curadm=curadm in `i'
    replace curdis=curdis in `i'

    }
    }
    }

    This is what I get
    id adm dis nnum nnum2 curadm curdis
    x 01/01/2012 03/02/2012 1 4 01/01/2012 03/02/2012
    x 02/01/2012 18/03/2012 2 4 01/01/2012 03/02/2012
    x 09/01/2012 25/03/2012 3 4 09/01/2012 03/02/2012
    x 28/03/2012 03/04/2012 4 4
    y 01/03/2012 03/03/2012 1 7
    y 03/03/2012 06/03/2012 2 7
    y 06/03/2012 08/03/2012 3 7
    y 10/03/2012 13/03/2012 4 7
    y 02/04/2012 03/04/2012 5 7
    y 03/04/2012 05/04/2012 6 7
    y 04/04/2012 04/04/2012 7 7
    this is what I want
    id adm dis nnum nnum2 curadm curdis
    x 01/01/2012 03/02/2012 1 4 01/01/2012 25/01/2012
    x 02/01/2012 18/03/2012 2 4 - -
    x 09/01/2012 25/03/2012 3 4 - -
    x 28/03/2012 03/04/2012 4 4 28/03/2012 03/04/2012
    y 01/03/2012 03/03/2012 1 7 01/03/2012 08/03/2012
    y 03/03/2012 06/03/2012 2 7 - -
    y 06/03/2012 08/03/2012 3 7 - -
    y 10/03/2012 13/03/2012 4 7 10/03/2012 13/03/2012
    y 02/04/2012 03/04/2012 5 7 02/04/2012 05/04/2012
    y 03/04/2012 05/04/2012 6 7 - -
    y 04/04/2012 04/04/2012 7 7 - -

  • #2
    How about:

    gen curadm=adm
    // carry admission date forward if last discharge if after current admission
    bys id (adm dis) : replace curadm = curadm[_n-1] if adm<=dis[_n-1]

    // discharge is largest discharge date with current admission.
    bys id curadm (dis) : gen curdis=curdis[_N]

    // set -curdis- to missing if not first -adm- in -curadm-
    bys id (curadm adm) : replace curdis= . if _n>1

    // set -curadm- to missing if -curdis- is missing
    replace curadm=. if mi(curdis)
    Last edited by Jeph Herrin; 15 Apr 2014, 10:08.

    Comment


    • #3
      So... why did my formatting change when I posted? The process strips blanks out of my code. Even when I try to edit it.

      Comment


      • #4
        Jeph: I copied your code and put it within code mark-up. (Toggle A to get Advanced settings, and use # for mark-up. I added some spaces around operators.

        Code:
        gen curadm = adm
        bys id (adm dis) : replace curadm = curadm[_n-1] if adm <= dis[_n-1] // carry admission date forward
        // if last discharge if after current admission
        bys id curadm (dis) : gen curdis = curdis[_N] // discharge is largest discharge date with current admission.
        bys id (curadm adm) : replace curdis = . if _n > 1 // set -curdis- to missing if not first -adm- in -curadm-
        replace curadm = . if mi(curdis) // set -curadm- to missing if -curdis- is missing

        Comment


        • #5
          Thanks Nick, but yours has the same problem as mine. I'd like the comments lined up on the right (all the //'s in the same column), but when posting the spacing disappears. Further poking around suggets that all posted text is assumed to be HTML, and of course there is no HTML tag for <tab>. So short of making a table with a row for each line of code, how do I do this - by posting an image?



          that seems a bit limiting.
          Attached Files

          Comment


          • #6
            I don't have a solution. I do want to underline what will be obvious to you: how helpful it is to present code in a form you can copy and paste. Many answers are aimed at giving people code they can use directly, or at least modify easily. Posting an image prohibits that so far as I can see.

            Comment


            • #7
              Originally posted by Jeph Herrin View Post
              Thanks Nick, but yours has the same problem as mine. I'd like the comments lined up on the right (all the //'s in the same column), but when posting the spacing disappears. Further poking around suggets that all posted text is assumed to be HTML, and of course there is no HTML tag for <tab>. So short of making a table with a row for each line of code, how do I do this - by posting an image?



              that seems a bit limiting.
              Jeph,

              Post your code using the BBCode "code" tags:


              [code]
              ...your code or output here...
              [/code]



              Comment


              • #8
                Thank you for your code Jeph. I also ran your code and got the following:
                id adm dis nnum nnum2 curadm curdis
                x 01-Jan-12 03-Feb-12 2 5 01-Jan-12 25-Mar-12
                x 02-Jan-12 18-Mar-12 3 5
                x 09-Jan-12 25-Mar-12 4 5
                x 28-Mar-12 03-Apr-12 5 5
                x 03-Mar-11 04-Mar-11 1 5
                y 01-Mar-12 03-Mar-12 2 8 01-Mar-12 08-Mar-12
                y 03-Mar-12 06-Mar-12 3 8
                y 06-Mar-12 08-Mar-12 4 8
                y 10-Mar-12 13-Mar-12 5 8
                y 02-Apr-12 03-Apr-12 6 8
                y 03-Apr-12 04-Apr-12 7 8
                y 04-Apr-12 05-Apr-12 8 8
                y 03-Mar-11 04-Mar-11 1 8

                Comment


                • #9
                  I adapted your codes (see below) and I think its working. I will try the code for different overlaps to see if it works. Thnak you for your help.

                  format adm %d
                  format dis %d
                  gen curadm=adm
                  format curadm %d
                  // carry admission date forward if last discharge if after current admission
                  bys id (adm dis) : replace curadm = curadm[_n-1] if adm<=dis[_n-1]
                  // discharge is largest discharge date with current admission.
                  bys id curadm (dis) : gen curdis=dis[_N]
                  format curdis %d
                  // set missing curadm to adm///
                  replace curadm=adm if curadm==.

                  //tag each date first adm//
                  bysort id curadm : gen adm_tag=_n

                  //gen new admin field///
                  gen curadm2=curadm
                  format curadm2 %d

                  replace curadm2=. if adm_tag!=1

                  // gen corresponding dis date//
                  gen curdis2=curdis if adm_tag==1
                  format curdis2 %d




                  THIS GIVES ME THE FOLLOWING TABLE:
                  id adm dis curadm curdis curadm2 curdis2
                  x 03-Mar-11 04-Mar-11 03-Mar-11 04-Mar-11 03-Mar-11 04-Mar-11
                  x 01-Jan-12 03-Feb-12 01-Jan-12 25-Mar-12
                  x 02-Jan-12 18-Mar-12 01-Jan-12 25-Mar-12
                  x 09-Jan-12 25-Mar-12 01-Jan-12 25-Mar-12 01-Jan-12 25-Mar-12
                  x 28-Mar-12 03-Apr-12 28-Mar-12 03-Apr-12 28-Mar-12 03-Apr-12
                  y 03-Mar-11 04-Mar-11 03-Mar-11 04-Mar-11 03-Mar-11 04-Mar-11
                  y 01-Mar-12 03-Mar-12 01-Mar-12 08-Mar-12
                  y 03-Mar-12 06-Mar-12 01-Mar-12 08-Mar-12 01-Mar-12 08-Mar-12
                  y 06-Mar-12 08-Mar-12 01-Mar-12 08-Mar-12
                  y 10-Mar-12 13-Mar-12 10-Mar-12 13-Mar-12 10-Mar-12 13-Mar-12

                  Comment

                  Working...
                  X