Announcement

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

  • Counting Events within Time Period: Seeking Code Assistance

    Hi all,

    I am a very new Stata user and would like to seek your help with a code. I need to create a new variable that includes the count of presentations in the previous 4 hours of the current presentation. Below is a description of my data:

    Each record represents a patient presentation. For each presentation, the presentation date and time are captured under the variable `pre_d_t`, already in the correct Stata date format and sorted. I need to count the number of presentations prior to the current record. To illustrate, below is an example where I manually did the calculation. Note: It does not matter if the presentation is for the same or different individuals.

    fake_ID pre pre_d_t previ_pres
    34887 1 20/01/2015 14:53 0
    26689 1 28/01/2015 10:47 0
    13665 1 28/01/2015 10:47 1
    2856 1 30/01/2015 10:53 0
    86893 1 30/01/2015 10:57 1
    35085 1 3/02/2015 7:04 0
    7111 1 3/02/2015 7:08 1
    32337 1 3/02/2015 7:13 2
    55510 1 3/02/2015 7:21 3
    87599 1 3/02/2015 7:50 4

    Below is also an example generated by Dataex:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(fake_ID pre pre_d_t)
    34887 1 1.7373848e+12
    26689 1 1.7380613e+12
    13665 1 1.7380613e+12
     2856 1 1.7382344e+12
    86893 1 1.7382347e+12
    35085 1 1.7385663e+12
     7111 1 1.7385665e+12
    32337 1 1.7385668e+12
    55510 1 1.7385673e+12
    87599 1  1.738569e+12
    20471 1 1.7385695e+12
    89276 1   1.73857e+12
    58447 1 1.7385703e+12
    36978 1 1.7385703e+12
    85063 1 1.7385707e+12
    39138 1  1.738571e+12
    11966 1 1.7385714e+12
    75424 1 1.7385716e+12
    69502 1 1.7385727e+12
    68662 1 1.7385727e+12
    93193 1  1.738573e+12
    45489 1  1.738573e+12
     6740 1 1.7385732e+12
    33799 1 1.7385737e+12
    97488 1 1.7385737e+12
    72644 1  1.738574e+12
     4542 1  1.738574e+12
    74597 1 1.7385757e+12
    49613 1 1.7385765e+12
    71672 1 1.7385768e+12
    85974 1 1.7385773e+12
    13408 1  1.738579e+12
    48844 1 1.7385794e+12
    87122 1 1.7385798e+12
    76647 1   1.73858e+12
    25126 1   1.73858e+12
    16636 1 1.7385804e+12
    74380 1  1.738581e+12
    98051 1  1.738581e+12
    72958 1  1.738581e+12
    90110 1 1.7385815e+12
    26436 1 1.7385815e+12
    88565 1 1.7385817e+12
    88211 1  1.738582e+12
    74893 1 1.7385823e+12
    91963 1 1.7385823e+12
    69345 1 1.7385827e+12
    21540 1  1.738583e+12
    82859 1  1.738583e+12
     4422 1  1.738583e+12
    86304 1 1.7385837e+12
    35260 1 1.7385844e+12
    77204 1 1.7385845e+12
    58612 1 1.7385848e+12
    32278 1  1.738585e+12
    17293 1  1.738585e+12
    80536 1 1.7385857e+12
    30600 1 1.7385865e+12
    21910 1  1.738587e+12
    72473 1  1.738587e+12
    69649 1 1.7385872e+12
    91193 1 1.7385874e+12
    67956 1  1.738588e+12
    35494 1 1.7385887e+12
    73897 1 1.7385887e+12
    18740 1 1.7385892e+12
    31461 1 1.7385895e+12
    13757 1 1.7385897e+12
    65377 1   1.73859e+12
    27013 1   1.73859e+12
    89984 1 1.7385908e+12
    57342 1 1.7385908e+12
    11147 1 1.7385908e+12
    41452 1  1.738591e+12
      305 1  1.738591e+12
    66600 1 1.7385914e+12
    34629 1 1.7385916e+12
     7802 1  1.738592e+12
    12758 1 1.7385922e+12
    22970 1 1.7385927e+12
    32955 1  1.738593e+12
    41441 1 1.7385943e+12
     3608 1 1.7385947e+12
     8438 1  1.738595e+12
      988 1  1.738595e+12
    32004 1  1.738595e+12
      520 1 1.7385952e+12
    22754 1 1.7385956e+12
    85147 1  1.738596e+12
    98201 1 1.7385965e+12
     3248 1 1.7385965e+12
    98748 1  1.738597e+12
    89411 1 1.7385972e+12
    96847 1 1.7385973e+12
    23922 1 1.7385976e+12
    69273 1  1.738598e+12
    48844 1 1.7385983e+12
    43765 1 1.7385988e+12
    58580 1  1.738599e+12
    37871 1 1.7385994e+12
    end
    format %tc pre_d_t



    Any help, advice, or reply would be greatly appreciated.


    Regards,
    Fouziah

  • #2
    Date-times should always be stored as doubles. In your case, results are just in minutes.

    Consider rangestat (from SSC, and cited in many threads here.

    .

    Code:
    rangestat (sum) pre, int(pre_d_t -14400000 -1)



    Comment


    • #3
      The code gave me exactly what I needed. Thanks, Nick, for introducing me to rangestat and for pointing out the date format issue.

      Comment

      Working...
      X