Announcement

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

  • Double precision - date and time variables

    Hello
    I desperately need help with double precision variables.

    I have a dataset of approximately 2000 hospital admissions.
    In this dataset I have two variables of dates (m/d/yyyy) and time points (hour and minutes h:m). One variable of date and time of hospital admission and another variable of date and time of hospital discharge. The variables have the format numeric (double).

    An example of my dataset and the mentioned variables:
    Patient-ID 4289 AND hospital admission 1/10/2015 9:30 AND hospital discharge 2/10/2015 12.50
    Patient-ID 387 AND hospital admission 12/3/2015 10:43 AND hospital discharge 12/3/2015 20:05
    • I would very much like to estimate mean time of length of hospital stay for this group of patients
    • I would also like to look at which time of day the patients are hospitalized, which means I need to look at the time point separately
    • Is it possible get information on whether admission is on a weekday or weekend from a date variable?
    I have tried to find the answer in "An introduction to Stata for health researchers" in section 5.5 Date variables but I don't think the examples are applicable in my case.
    Can anybody help me?

  • #2
    Double is a storage type, not a format. Please give a data example using dataex (SSC) as requested in http://www.statalist.org/forums/help#stata

    Comment


    • #3
      All of these can be done. At the command line, type
      Code:
      help datetime
      for further information.

      .ÿversionÿ14.2

      .ÿ
      .ÿclearÿ*

      .ÿsetÿmoreÿoff

      .ÿ
      .ÿinputÿlongÿpidÿstr14(adm_dateÿdis_date)

      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿpidÿÿÿÿÿÿÿÿadm_dateÿÿÿÿÿÿÿÿdis_date
      ÿÿ1.ÿ4289ÿ"1/10/2015ÿ9:30"ÿ"2/10/2015ÿ12:50"
      ÿÿ2.ÿ387ÿ"12/3/2015ÿ10:43"ÿ"12/3/2015ÿ20:05"
      ÿÿ3.ÿend

      .ÿ
      .ÿforeachÿvarÿinÿadmÿdisÿ{
      ÿÿ2.ÿÿÿÿÿÿÿÿÿgenerateÿdoubleÿ`var'_dtÿ=ÿclock(`var'_date,ÿ"MDYhm")
      ÿÿ3.ÿÿÿÿÿÿÿÿÿdropÿ`var'_date
      ÿÿ4.ÿ}

      .ÿ
      .ÿ//ÿestimateÿmeanÿtimeÿofÿlengthÿofÿhospitalÿstayÿforÿthisÿgroupÿofÿpatients
      .ÿgenerateÿintÿlosÿ=ÿdofc(dis_dt)ÿ-ÿdofc(adm_dt)ÿ+ÿ1

      .ÿ
      .ÿ//ÿlookÿatÿwhichÿtimeÿofÿdayÿtheÿpatientsÿareÿhospitalized,ÿwhichÿmeansÿIÿneedÿtoÿlookÿatÿtheÿtimeÿpointÿseparately
      .ÿgenerateÿdoubleÿadm_tmÿ=ÿhh(adm_dt)ÿ+ÿmm(adm_dt)ÿ/ÿ60ÿ//ÿexampleÿfromÿhelpÿfile

      .ÿ
      .ÿ//ÿgetÿinformationÿonÿwhetherÿadmissionÿisÿonÿaÿweekdayÿorÿweekendÿfromÿaÿdateÿvariable?
      .ÿgenerateÿbyteÿweekdayÿ=ÿÿdow(dofc(adm_dt))

      .ÿforvaluesÿiÿ=ÿ1/7ÿ{
      ÿÿ2.ÿÿÿÿÿÿÿÿÿlocalÿWeekDaysÿ`WeekDays'ÿ`=`i'-1'ÿ`:ÿwordÿ`i'ÿofÿ`c(Weekdays)''
      ÿÿ3.ÿ}

      .ÿlabelÿdefineÿWeekDaysÿ`WeekDays'

      .ÿlabelÿvaluesÿweekdayÿWeekDays

      .ÿ
      .ÿformatÿ*_dtÿ%tcCCYY-NN-DD_HH:MM

      .ÿformatÿadm_tmÿ%03.1f

      .ÿ
      .ÿlist,ÿnoobs

      ÿÿ+----------------------------------------------------------------------+
      ÿÿ|ÿÿpidÿÿÿÿÿÿÿÿÿÿÿÿÿadm_dtÿÿÿÿÿÿÿÿÿÿÿÿÿdis_dtÿÿÿlosÿÿÿadm_tmÿÿÿÿweekdayÿ|
      ÿÿ|----------------------------------------------------------------------|
      ÿÿ|ÿ4289ÿÿÿ2015-01-10ÿ09:30ÿÿÿ2015-02-10ÿ12:05ÿÿÿÿ32ÿÿÿÿÿÿ9.5ÿÿÿSaturdayÿ|
      ÿÿ|ÿÿ387ÿÿÿ2015-12-03ÿ10:04ÿÿÿ2015-12-03ÿ20:00ÿÿÿÿÿ1ÿÿÿÿÿ10.1ÿÿÿThursdayÿ|
      ÿÿ+----------------------------------------------------------------------+

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .
      Last edited by Joseph Coveney; 07 Mar 2017, 02:16. Reason: Edited to add: ought to have been str15(adm_date dis_date)

      Comment


      • #4
        Okay.
        The format is numeric. I'm not sure if that answer your question, Nick? The data is imported from an Excell-file?

        Otherwise, I have tried to use your suggestions, Joseph, with some luck. So thank you very much for that.
        I've used these commands:

        *************
        format adm_datetime %tcCCYY-NN-DD_HH:MM
        format dis_datetime %tcCCYY-NN-DD_HH:MM


        //Generate variable to see which day of the week admission to ED is:
        generate byte weekday = dow(dofc(adm_datetime))
        label define dayofweek 0 "Sunday" 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday" 6 "Saturday"
        label values weekday dayofweek
        tab weekday


        //Generate variable number of days at Emergency Department:
        generate los_akut_days = dofc(dis_datetime) - dofc(adm_datetime)
        //I have not added + 1 because I don't see why I should add another day?

        //Estimate hours and minutes at Emergency Department (ED):
        //Generate variables on hours and minutes for admission and discharge, respectively:
        generate double adm_los_hrs = hh( adm_datetime ) + (mm( adm_datetime )/60)
        generate double dis_los_hrs = hh(dis_datetime) + (mm(dis_datetime)/60)

        //Generate variable of subtraction discharge time by admission time:
        generate los_total_hrs = dis_los_hrs - adm_los_hrs

        ******************************

        For some patients it adds up, but for others it is a problem when the patient has been admitted late night, e.g. at 21:11 pm. and discharged after midnight e.g. at 00:46 am. Stata calculates the hours between admission and discharge as 20.4 hours, but it is actually more like 3.5 hours. How can I make up for this?

        Comment


        • #5
          I wasn't asking a question; sorry if that was unclear. I was explaining that storage type and format are different.

          The request for a data example still stands. If these data are confidential, that point is addressed in the linked section given in #2.

          To get sensible answers for stays over one or more days, you will need a combined date and time variable.

          Adding 1 is widely conventional to avoid reporting discharge on the same day as a stay of length 0 days.

          Comment

          Working...
          X