Dear statalisters,

I am working with a paneldata, time-series dataset. I am modelling variables over time. First I want to calculate the total amount of Norepinephrine (NE) (a drug used to raise blood pressure on septic patients) given per 3 hour interval during the first 24 hours, thus in total 8 categories of time (0-3h, 3-6h, 6-9h ... 21-24h).

I have the following variables:

patientid: just a number for each patient
AdmissionTime: time admitted (start time for calculation of dose for the first 3 hour category)
datetime: timestamp for a change in infusion ( i.e change in rate (not shown), start of new infusion)
ordernumber: the unique code for each infusion started
hour_3_cat: by me created variable that places datetime in the corresponding time category
no_split: by me created variable that is 1 if the registration overlaps time categories otherwise is missing
NEekvivalenter_mg: Given dose of NE between datetime[_n-1] and datetime
difftime_ordernumber_ms: time in ms between datetime[_n-1] and datetime

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input long patientid double(AdmissionTime datetime) long ordernumber float hour_3_cat byte no_split double(NEekvivalenter_mg difftime_ordernumber_ms)
776 1432477369430  1.432476e+12 27570 . .                     0        .
776 1432477369430 1432485367140 27570 1 .    1.0407777777777778  9367140
776 1432477369430 1432489865170 27570 2 1    .49944444444444447  4498030
776 1432477369430 1432490343630 27570 2 1    .05311111111111111   478460
776 1432477369430 1432491667270 27570 2 1    .17626666666666665  1323640
776 1432477369430 1432491923900 27570 2 1    .03413333333333333   256630
776 1432477369430 1432491938070 27570 2 1  .0021777777777777776    14170
776 1432477369430 1432491944910 27570 2 .                     0     6840
776 1432477369430 1432506995110 27570 3 1    2.3400222222222222 15050200
776 1432477369430 1432507577860 27570 3 1    .09053333333333333   582750
776 1432477369430 1432507623220 27570 3 1                     0    45360
776 1432477369430 1432507633640 27570 3 1  .0015555555555555557    10420
776 1432477369430 1432507648630 27570 3 1                     0    14990
776 1432477369430 1432509699420 27570 3 .     .3188888888888889  2050790
776 1432477369430 1432509975510 27570 4 1    .04293333333333333   276090
776 1432477369430 1432511495480 27570 4 1    .25316666666666665  1519970
776 1432477369430 1432512397660 27570 4 1    .15033333333333332   902180
776 1432477369430 1432512787590 27570 4 1    .06483333333333334   389930
776 1432477369430 1432517804310 27570 4 1     .8913777777777778  5016720
776 1432477369430 1432518264000 27570 4 1                 .0816   459690
776 1432477369430 1432518706640 27570 4 1    .08839999999999999   442640
776 1432477369430 1432519320950 27570 4 1    .12279999999999999   614310
776 1432477369430 1432519617440 27570 4 1    .06577777777777777   296490
776 1432477369430 1432520464050 27570 4 1    .18799999999999997   846610
776 1432477369430 1432520470910 27570 4 1                     0     6860
776 1432477369430 1432520512810 27570 4 .   .009111111111111111    41900
776 1432477369430 1432520580000 27570 5 1   .013315555555555747    67190
776 1432477369430 1432520778170 27570 5 1   .044000000000000004   198170
776 1432477369430 1432520786720 27570 5 1  .0022222222222222222     8550
776 1432477369430 1432524121240 27570 5 1     .8515111111111111  3334520
776 1432477369430 1432524191040 27570 5 1    .01763333333333333    69800
776 1432477369430 1432524435580 27570 5 .    .06777777777777778   244540
776 1432477369430 1432533571910 27570 6 1                1.8262  9136330
776 1432477369430 1432533639360 27570 6 1   .013399999999999999    67450
776 1432477369430 1432533907790 27570 6 1   .053599999999999995   268430
776 1432477369430 1432539448460 27570 6 1     .9226666666666667  5540670
776 1432477369430 1432539588020 27570 6 1   .023166666666666665   139560
776 1432477369430 1432539614380 27570 6 1                     0    26360
776 1432477369430 1432540349470 27570 6 1                 .1225   735090
776 1432477369430 1432541249790 27570 6 1                   .15   900320
776 1432477369430 1432541332930 27570 6 .   .009222222222222222    83140
776 1432477369430 1432552969429 27570 7 .                     .        .
776 1432477369430 1432558388850 27570 8 1     2.272933333333334 17055920
776 1432477369430 1432558580000 27570 8 1    .02546666666666667   191150
776 1432477369430 1432559279920 27570 8 1    .10873333333333333   699920
776 1432477369430 1432559391900 27570 8 .   .017266666666666666   111980
776 1432477369430 1432570131580 27570 . .    1.4311999999999998 10739680
777 1432477969130 1432483560000 27395 1 .                     0        .
777 1432477969130 1432496460000 27395 2 1                     0 12900000
777 1432477969130 1432496520000 27395 2 .                     0    60000
777 1432477969130 1432510369129 27395 3 .                     .        .
777 1432477969130 1.4325138e+12 27395 4 1                     0 17280000
777 1432477969130 1432513920000 27395 4 .                     0   120000
777 1432477969130 1432531969129 27395 5 .                     .        .
777 1432477969130 1432539420000 27395 6 1    1.1333199999999999 25500000
777 1432477969130 1432539994810 27395 6 .   .012755555555555555   574810
777 1432477969130 1432552612090 27395 7 1    .28022222222222226 12617280
777 1432477969130 1432553183890 27395 7 .    .01268888888888889   571800
777 1432477969130 1432556218720 27395 8 1   .033711111111111115  3034830
777 1432477969130 1432556693410 27395 8 1   .005266666666666666   474690
777 1432477969130 1432558923010 27395 8 1                     0  2229600
777 1432477969130 1432558980000 27395 8 . .00003555555555556644    56990
778 1432474851220 1432485651219 27458 1 .                     .        .
778 1432474851220 1432486080000 27458 2 1                     0        .
778 1432474851220 1432486154290 27458 2 1                 .0148    74290
778 1432474851220 1432486161060 27458 2 1                     0     6770
778 1432474851220 1432486400060 27458 2 1    .04248888888888889   239000
778 1432474851220 1432486474490 27458 2 1   .013155555555555556    74430
778 1432474851220 1432486486790 27458 2 1                     0    12300
778 1432474851220 1432487306550 27458 2 1                 .1274   819760
778 1432474851220 1432487787370 27458 2 1    .07466666666666667   480820
778 1432474851220 1432487798580 27458 2 1                     0    11210
778 1432474851220 1432488207490 27458 2 1                 .0544   408910
778 1432474851220 1432488480150 27458 2 .    .03626666666666667   272660
778 1432474851220 1432499920480 27458 3 1    1.2703333333333333 11440330
778 1432474851220 1432500251780 27458 3 1    .03677777777777778   331300
778 1432474851220 1432500272570 27458 3 1                     0    20790
778 1432474851220 1432500286950 27458 3 1  .0015555555555555557    14380
778 1432474851220 1432500293850 27458 3 .                     0     6900
778 1432474851220 1432511630920 27458 4 1                 1.259 11337070
778 1432474851220 1432517960640 27458 4 .                  .703  6329720
778 1432474851220 1432518069800 27458 5 1   .012111111111111112   109160
778 1432474851220 1432518092660 27458 5 1                     0    22860
778 1432474851220 1432518863220 27458 5 1    .08555555555555555   770560
778 1432474851220 1432518960000 27458 5 1   .013328888888889168    96780
778 1432474851220 1432519020000 27458 5 1                     0    60000
778 1432474851220 1432519752360 27458 5 1    .08133333333333333   732360
778 1432474851220 1432521548280 27458 5 1    .19944444444444445  1795920
778 1432474851220 1432521566420 27458 5 .                  .002    18140
778 1432474851220 1432533587620 27458 6 1     1.068088888888889 12021200
778 1432474851220 1432533631080 27458 6 1  .0038222222222222225    43460
778 1432474851220 1432539573370 27458 6 .     .5279111111111112  5942290
778 1432474851220 1432545897770 27458 7 1     .5618666666666666  6324400
778 1432474851220 1432546523760 27458 7 1    .05555555555555555   625990
778 1432474851220 1432546534740 27458 7 .                     0    10980
778 1432474851220 1432554900630 27458 8 1     .6504555555555556  8365890
778 1432474851220 1432555541140 27458 8 1    .04977777777777778   640510
778 1432474851220 1432555795340 27458 8 1   .016933333333333335   254200
778 1432474851220 1432556330980 27458 8 1   .035666666666666666   535640
778 1432474851220 1432556337750 27458 8 1                     0     6770
end
format %tcCCYY-NN-DD_HH:MM:SS.sss AdmissionTime
format %tcCCYY-NN-DD_HH:MM:SS.sss datetime
label values hour_3_cat hour_3_cat
label def hour_3_cat 1 "0-3 post adm", modify
label def hour_3_cat 2 "3-6h post adm", modify
label def hour_3_cat 3 "6-9h post adm", modify
label def hour_3_cat 4 "9-12h post adm", modify
label def hour_3_cat 5 "12-15h post adm", modify
label def hour_3_cat 6 "15-18h post adm", modify
label def hour_3_cat 7 "18-21h post adm", modify
label def hour_3_cat 8 "21-24h post adm", modify

The issue is that the patients can have several infusions (ordernumber) at the same time, the infusion does not always start at time zero (i.e. it can start before AdmissionTime, as in patientid 776 above, or 12 hours after admission) and of course does the time between registration overlap my arbitrary created timecategories of 3h intervals. Further, some infusions are unchanged (i.e no registrations) for longer than 3 hours, sometimes over 12 hours. Meaning I have to divide those doses between several time categories. Some infusions (ordernumber) does not contain all time categories (since they were started late or they were unchanged for a long time, meaning no timestamps were created).

I want to calculate the amount of NE given per 3 hour interval, per patient and ordernumber.

One solution is to sum all NEekvivalenter_mg per patientid and ordernumber and hour_3_cat if no_split==1. That way i have the total dose given for each 3h-time interval per patient and ordernumber that does not overlap my time categories. Now. To calculate the doses that do overlap my 3h categories (hour_3_cat) I could calculate a ratio between time "belonging" to one category and time belonging to the next time category, then divide NEekvivalenter_mg according to this ratio. However this gets complicated since all hour_3_cat does not exist for each ordernumber and there are hour_3_cat that does not exist depending on how long the infusion were unchanged.

I am starting to think it would be easier if I somehow could calculate an AUC (i am not talking about ROC here) for the whole 0-24 hour period per ordernumber and patientid and then extract 3 hour intervals later. But I can not just use the total AUC for the 0-24 hours since the givendose varies over this time period. For example two patients could have received the same amount of NE but the first received all the first 4 hours and the second patient received all evenly distributed over the 24 hour period. I need the AUC / given dose for the 3 hour intervals.

Is there a way to "plot" the fluctuations/variations in NE given over the first 24 hours, then extract the dose given per 3 hour time interval?

I tried to keep the intro question short, I can elaborate if need be.

Thanks in advance.

Jesper Eriksson