Here is a better version that takes care of missing intervals:
/* Generate some random times with some missing values */
data test;
call streaminit(798687);
timedate = '20APR2016:00:00:00'dt;
format timedate datetime17.;
do obs = 1 to 100;
timedate + rand("Exponential") * '00:03:00't;
if rand("uniform") > 0.3
then value = obs;
else call missing(value);
output;
end;
run;
/* Assign values to 5 minute intervals. Add missing intervals */
data test2;
set test(rename=value=thisValue);
t = intnx("minute5", timedate, 1, "beginning");
do timedate5 = intnx("minute5", coalesce(lag(t),t), 1)
to intnx("minute5", t, -1)
by '00:05:00't;
output;
end;
timedate5 = t;
value = thisValue;
output;
format timedate5 datetime17.;
drop thisValue t;
run;
/* Keep last value in each interval. Carry values over to fill missing values */
data want;
set test2; by timedate5;
retain lastValue;
if last.timedate5 then do;
if missing(value)
then value = lastValue;
else lastValue = value;
output;
end;
drop lastValue;
run;
... View more