Announcement

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

  • Creating a new variable using 'time windows'

    Hello Statalist community,
    I am using Stata v 16.1 for mac and would sincerely appreciate help with the following code. For context, I have supplied a dataex example - the data is long format with multiple observations per day (re: chart records (healthcare information) at specific times throughout the day x multiple days). The sdate and time2 variables are formatted as stata dates/time. I then created a "cares" variable using the [generate][replace] commands. I am getting a 'type mismatch" error with this code (showing two examples).
    gen cares=.
    replace cares=1 if time2=="00:00-02:59 (also tried, if time2 == "00:00/02:59")

    This is the decision rule I want to apply to the "cares" variable.
    00:00 - 02:59 = 1
    03:00 - 05:59 = 2
    06:00 - 08:59 = 3
    09:00 - 11:59 = 4
    12:00 - 14:59 = 5
    15:00 - 17:59 = 6
    18:00 - 20:59 = 7
    21:00 - 23:59 = 8
    so for any row that has information within these 3 hour windows, I would like to assign a value 1 - 8 and this will repeat for each unique day (e.g, each sdate will have values 1 - 8 if there was information charted within the 3 hour window.).

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 response_date float sdate str8 response_time float(time2 cares)
    "6-Jan-21" 22286 "17:19:00" 62340000 .
    "6-Jan-21" 22286 "17:19:00" 62340000 .
    "6-Jan-21" 22286 "17:19:00" 62340000 .
    "6-Jan-21" 22286 "17:19:00" 62340000 .
    "6-Jan-21" 22286 "17:19:00" 62340000 .
    "6-Jan-21" 22286 "17:53:00" 64380000 .
    "6-Jan-21" 22286 "21:00:00" 7.56e+07 .
    "6-Jan-21" 22286 "21:00:00" 7.56e+07 .
    "6-Jan-21" 22286 "21:00:00" 7.56e+07 .
    "6-Jan-21" 22286 "21:00:00" 7.56e+07 .
    "6-Jan-21" 22286 "21:00:00" 7.56e+07 .
    "6-Jan-21" 22286 "21:00:00" 7.56e+07 .
    "6-Jan-21" 22286 "22:00:00" 7.92e+07 .
    "7-Jan-21" 22287 "0:47:00"   2820000 .
    "7-Jan-21" 22287 "0:48:00"   2880000 .
    "7-Jan-21" 22287 "0:48:00"   2880000 .
    "7-Jan-21" 22287 "0:48:00"   2880000 .
    "7-Jan-21" 22287 "0:48:00"   2880000 .
    "7-Jan-21" 22287 "1:34:00"   5640000 .
    "7-Jan-21" 22287 "3:00:00"  1.08e+07 .
    "7-Jan-21" 22287 "3:00:00"  1.08e+07 .
    "7-Jan-21" 22287 "3:00:00"  1.08e+07 .
    "7-Jan-21" 22287 "3:00:00"  1.08e+07 .
    "7-Jan-21" 22287 "3:00:00"  1.08e+07 .
    "7-Jan-21" 22287 "4:00:00"  1.44e+07 .
    "7-Jan-21" 22287 "4:00:00"  1.44e+07 .
    "7-Jan-21" 22287 "4:00:00"  1.44e+07 .
    "7-Jan-21" 22287 "4:00:00"  1.44e+07 .
    "7-Jan-21" 22287 "6:00:00"  2.16e+07 .
    "7-Jan-21" 22287 "6:00:00"  2.16e+07 .
    end
    format %td sdate
    format %tcHH:MM time2
    Thank you for your time and consideration
    Ash

  • #2
    Something like this should work, I've provided how to code levels 1 and 2, you can repeat the replace command to finish the rest:

    Code:
    generate cares2 = 1 if time2 >= clock("00:00", "hm") & time < clock("03:00", "hm")
    replace  cares2 = 2 if time2 >= clock("03:00", "hm") & time < clock("06:00", "hm")
    Last edited by Ken Chui; 03 Apr 2023, 13:19.

    Comment


    • #3
      Thank you very much Ken! This worked splendidly

      Comment

      Working...
      X