我想计算 t 时的价格减去 t + 1 时的价格,以确保 i <天> < / em>的。特别是, t + 1 定义为价格 t 后至少5分钟的价格。因此,我添加了一个变量来调节间隔,并使用以下代码在 t + 1 时创建一行Price。 以下是输入数据的示例。
data test4;
length _ric$ 25 type$ 5 interval$ 15 time_l_$ 25 ;
input _ric$ date_l_ time_l_ type$ price interval$;
datalines;
AXPA031407800.U 20131212 9:52:56.537 Trade 5.85 09:50:00
AXPA031407800.U 20131212 9:52:56.537 Trade 5.85 09:50:00
AXPA031407800.U 20131212 9:53:13.586 Trade 5.8 09:50:00
AXPA031407800.U 20131212 9:53:13.586 Trade 5.8 09:50:00
AXPA031407800.U 20131212 9:53:13.607 Trade 5.8 09:50:00
AXPA031407800.U 20131212 9:53:13.607 Trade 5.8 09:50:00
AXPA031407800.U 20131212 9:53:34.990 Trade 5.8 09:50:00
AXPA031407800.U 20131212 9:55:12.990 Trade 5.7 09:55:00
AXPA031407800.U 20131212 9:55:12.990 Trade 5.7 09:55:00
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00
AXPA031407800.U 20131212 9:55:13.011 Trade 5.7 09:55:00
AXPA031407900.U 20131205 9:37:58.420 Trade 6.25 09:35:00
AXPA031407900.U 20131205 9:39:04.996 Trade 6.25 09:35:00
AXPA031407900.U 20131205 9:39:04.996 Trade 6.25 09:35:00
AXPA031407900.U 20131205 9:39:04.996 Trade 6.25 09:35:00
AXPA031407900.U 20131205 9:39:04.996 Trade 6.25 09:35:00
AXPA031407900.U 20131205 9:39:04.996 Trade 6.25 09:35:00
;
data test1;
set test nobs=nobs;
do _i = _n_ to nobs until (new_date ne date_l_ or new_time > interval);
set test (rename=(date_l_=new_date price=new_price interval=new_time)) point=_i;
end;
if (date_l_ ne new_date) or (_i > nobs) then call missing(new_price);
run;
输出数据如下所示。但是,_RIC(安全名称),date_l_和time_l_已更改。如输出数据所示,例如,AXPA031407800.U在输入数据中有13个观测值,但在输出数据中有7个观测值。
_ric type interval time_l_ date_l_ price new_date new_time new_price
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.85 20131212 09:55:00 5.7
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.85 20131212 09:55:00 5.7
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.8 20131212 09:55:00 5.7
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.8 20131212 09:55:00 5.7
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.8 20131212 09:55:00 5.7
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.8 20131212 09:55:00 5.7
AXPA031407800.U Trade 09:50:00 9:55:12.990 20131212 5.8 20131212 09:55:00 5.7
AXPA031407900.U Trade 09:55:00 9:37:58.420 20131212 5.7 20131205 09:35:00
AXPA031407900.U Trade 09:55:00 9:37:58.420 20131212 5.7 20131205 09:35:00
AXPA031407900.U Trade 09:55:00 9:37:58.420 20131212 5.7 20131205 09:35:00
AXPA031407900.U Trade 09:55:00 9:37:58.420 20131212 5.7 20131205 09:35:00
AXPA031407900.U Trade 09:55:00 9:37:58.420 20131212 5.7 20131205 09:35:00
AXPA031407900.U Trade 09:55:00 9:37:58.420 20131212 5.7 20131205 09:35:00
AXPA031407900.U Trade 09:35:00 9:39:04.996 20131205 6.25 20131205 09:35:00
AXPA031407900.U Trade 09:35:00 9:39:04.996 20131205 6.25 20131205 09:35:00
AXPA031407900.U Trade 09:35:00 9:39:04.996 20131205 6.25 20131205 09:35:00
AXPA031407900.U Trade 09:35:00 9:39:04.996 20131205 6.25 20131205 09:35:00
AXPA031407900.U Trade 09:35:00 9:39:04.996 20131205 6.25 20131205 09:35:00
AXPA031407900.U Trade 09:35:00 9:39:04.996 20131205 6.25 20131205 09:35:00
这是我的目标结果,它创建了一个新变量Price_next_interval。新变量代表同一天下一个时间间隔内的价格。
_RIC Date_l_ time_l_ type Price interval Price_next_interval
AXPA031407800.U 20131212 9:52:56.537 Trade 5.85 09:50:00 5.7
AXPA031407800.U 20131212 9:52:56.537 Trade 5.85 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.586 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.586 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.607 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.607 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:34.990 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:55:12.990 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:12.990 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.011 Trade 5.7 09:55:00 .
AXPA031407900.U 20131205 9:37:58.420 Trade 6.25 09:35:00 6.28
AXPA031407900.U 20131205 9:45:04.996 Trade 6.28 09:45:00 6.29
AXPA031407900.U 20131205 9:45:04.996 Trade 6.28 09:45:00 6.29
AXPA031407900.U 20131205 9:55:04.996 Trade 6.29 09:55:00 .
AXPA031407900.U 20131205 9:55:04.996 Trade 6.29 09:55:00 .
AXPA031407900.U 20131205 9:55:04.996 Trade 6.29 09:55:00 .
答案 0 :(得分:2)
在我看来,通过分组处理和retain
而不是point
更容易做到这一点:
data test4;
length _ric$ 25 type$ 5;
input _ric $ date_l_ :yymmdd8. time_l_ :time. type$ price interval :time. price_next_interval_goal;
format date_l_ yymmddn8. time_l_ interval time.;
datalines;
AXPA031407800.U 20131212 9:52:56.537 Trade 5.85 09:50:00 5.7
AXPA031407800.U 20131212 9:52:56.537 Trade 5.85 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.586 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.586 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.607 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:13.607 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:53:34.990 Trade 5.8 09:50:00 5.7
AXPA031407800.U 20131212 9:55:12.990 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:12.990 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.002 Trade 5.7 09:55:00 .
AXPA031407800.U 20131212 9:55:13.011 Trade 5.7 09:55:00 .
AXPA031407900.U 20131205 9:37:58.420 Trade 6.25 09:35:00 6.28
AXPA031407900.U 20131205 9:45:04.996 Trade 6.28 09:45:00 6.29
AXPA031407900.U 20131205 9:45:04.996 Trade 6.28 09:45:00 6.29
AXPA031407900.U 20131205 9:55:04.996 Trade 6.29 09:55:00 .
AXPA031407900.U 20131205 9:55:04.996 Trade 6.29 09:55:00 .
AXPA031407900.U 20131205 9:55:04.996 Trade 6.29 09:55:00 .
;
/* Sort into reverse order */
proc sort data = v_want out = want;
by descending date_l_ interval;
run;
/* Carry the price forward via retain if we've got to the last row for this interval */
/* N.B. do not populate retained figure until after the row has been output*/
/* Clear the carried-forward figure at the start of each date*/
data want2;
set test4;
by descending date_l_ descending interval;
if first.date_l_ then call missing(price_next_interval);
output;
retain price_next_interval;
if last.interval then price_next_interval = price;
run;
/*Sort back into original order*/
proc sort data = want2;
by descending date_l_ interval;
run;