假设数据集有三列
Date Region Price
01-03 A 1
01-03 A 2
01-03 B 3
01-03 B 4
01-03 A 5
01-04 B 4
01-04 B 6
01-04 B 7
我尝试通过以下代码按日期和地区获取领先价格。
data want;
set have;
by _ric date_l_;
do until (eof);
set have(firstobs=2 keep=price rename=(price=lagprice)) end=eof;
end;
if last.date_l_ then call missing(lagprice);
run;
然而,WANT 只有一个观察结果。然后我创建new_date=date
并尝试另一个代码:
data want;
set have nobs=nobs;
do _i = _n_ to nobs until (new_date ne Date);
if eof1=0 then
set have (firstobs=2 keep=price rename=(price=leadprice)) end=eof1;
else leadprice=.;
end;
run;
使用此代码,SAS 工作缓慢。所以我认为这段代码也不合适。任何人都可以提出一些建议吗?谢谢