これが私の試みです。まず、使用するサンプル データをいくつか作成します。
**
** CREATE ~3 YEARS DAYS OF HOURLY TEMPERATURE DATA
** THIS IS UGLY - IM SURE THERES A BETTER WAY TO DO IT BUT WHATEVER
*;
data tmp;
pi = constant('pi');
do year=1 to 3;
linear_trend = 0.1 * year;
day = 0;
do yearly_progress=0 to (pi*2) by (pi/182.5);
day = day + 1;
yearly_seasonality = (1 + sin(yearly_progress)) / 2;
hour = 0;
day_mod = (ranuni(0)*10);
do hourly_progress=0 to (pi*2) by (pi/12);
hourly_seasonality = (1 + sin(hourly_progress)) / 2;
if hour ne 24 then do;
temperature = 60*(1+linear_trend) + (20 * yearly_seasonality) + (30 * hourly_seasonality) - day_mod;
output;
end;
hour = hour + 1;
end;
end;
end;
run;
**
** ADD SOME MISSING VALS
** ~ 10% MISSING
** ~ 10 IN A ROW MISSING EVERY 700 OR SO HOURS
*;
data sample_data;
set tmp;
if (ranuni(0) < 0.1) or (mod(_n_,710) > 700) then do;
temperature = .;
end;
run;
次に、要件が満たされている場合、温度の移動平均を計算します。
**
** I DONT NORMALLY LIKE USING THE LAG FUNCTION BUT IN THIS CASE ITS IDEAL
*;
data results;
set sample_data;
**
** POPULATE AN ARRAY WITH THE 24 CURRENT VALUES
** BECAUSE WE ARE USING LAG FUNCTION MAKE SURE IT IS NOT WITHIN ANY
** CONDITIONAL IF STATEMENTS
*;
array arr [0:23] temperature0-temperature23;
temperature0 = lag0(temperature);
temperature1 = lag1(temperature);
temperature2 = lag2(temperature);
temperature3 = lag3(temperature);
temperature4 = lag4(temperature);
temperature5 = lag5(temperature);
temperature6 = lag6(temperature);
temperature7 = lag7(temperature);
temperature8 = lag8(temperature);
temperature9 = lag9(temperature);
temperature10 = lag10(temperature);
temperature11 = lag11(temperature);
temperature12 = lag12(temperature);
temperature13 = lag13(temperature);
temperature14 = lag14(temperature);
temperature15 = lag15(temperature);
temperature16 = lag16(temperature);
temperature17 = lag17(temperature);
temperature18 = lag18(temperature);
temperature19 = lag19(temperature);
temperature20 = lag20(temperature);
temperature21 = lag21(temperature);
temperature22 = lag22(temperature);
temperature23 = lag23(temperature);
**
** ITERATE OVER THE ARRAY VARIABLES TO MAKE SURE WE MEET THE REQUIREMENTS
*;
available_observations = 0;
missing_observations = 0;
max_consecutive_missing = 0;
tmp_consecutive_missing = 0;
do i=0 to 23;
if arr[i] eq . then do;
missing_observations = missing_observations + 1;
tmp_consecutive_missing = tmp_consecutive_missing + 1;
max_consecutive_missing = max(max_consecutive_missing, tmp_consecutive_missing);
end;
else do;
available_observations = available_observations + 1;
tmp_consecutive_missing = 0;
end;
end;
if tmp_consecutive_missing <= 6 and available_observations >= 17 then do;
moving_avg = mean(of temperature0-temperature23);
end;
run;