Announcement

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

  • Composite date variable generation

    Hi good people of Statalist

    I have a dataset where people can get hospitalized up to 4 times. These are logged with dates for each hospitalization. The variables are called dahosp1, dahosp2… (date of hospitalization first, second… time) and are in DD-NN-CCYY format.

    I need to make a new variable for hospitalization in a specific time range (01/04/2014 - 01/04/2015). This new variable should contain the date of the first hospitalization in this period. As my data is for a long period of time the 1sthospitalization (dahosp1) could be before (or after) this time range so the 3rd hospitalization could be in this time range. I am not interested in them being hospitalized multiple times in the time range as they will be censored after first hospitalization.

    I do not know how to solve this. Sorry for the complicated explanation.

  • #2
    Here is code for the first and also the last. You don't give a real(istic) data example so I made one up.

    Code:
    clear
    set obs 3
    generate id = _n
    set seed 2803 
    gen dahosp1 = mdy(1,1,2013)
    gen dahosp2 = mdy(5,1,2014) + runiformint(-10, 10)
    gen dahosp3 = mdy(2,1,2015) + runiformint(-10, 10)
    gen dahosp4 = mdy(5,1,2015) + runiformint(-10, 10)
    
    format dahosp* %td
    dataex
    
    gen first = .
    gen last = .
    quietly forval j = 1/4 {
    replace first = min(first, dahosp`j') if inrange(dahosp`j', mdy(4,1,2014), mdy(4,1,2015))
    replace last = max(last, dahosp`j') if inrange(dahosp`j', mdy(4,1,2014), mdy(4,1,2015))
    }
    
    format first last %td
    l
    
    
         +----------------------------------------------------------------------------+
         | id     dahosp1     dahosp2     dahosp3     dahosp4       first        last |
         |----------------------------------------------------------------------------|
      1. |  1   01jan2013   01may2014   22jan2015   03may2015   01may2014   22jan2015 |
      2. |  2   01jan2013   24apr2014   27jan2015   03may2015   24apr2014   27jan2015 |
      3. |  3   01jan2013   01may2014   11feb2015   11may2015   01may2014   11feb2015 |
         +----------------------------------------------------------------------------+

    Comment


    • #3
      Hi Nick

      Thanks a lot. I am only interested in the date of their first hospitalization in the period - the 2nd, 15th or last is not relevant to me.
      I don't quite grasp all of the code you wrote, would you care to explain it a bit?

      quietly forval j = 1/4 (I do not grasp this)
      format first last %td l (I am unsure what the 1 means/does in this situation)?

      Comment


      • #4
        I went beyond your question in the hope that an answer might interest other people. Threads here are more than a helpline for individuals who ask a question.

        Working backwards

        l means list and is the next command, nothing to do with format

        You need to read up on loops in Stata, it seems. See the references suggested yesterday at #2 in https://www.statalist.org/forums/for...different-code for a suggestion.

        Comment


        • #5
          No problem, I have made the code work for me so thanks a lot for that.

          Comment

          Working...
          X