我有一个每天上午9点,上午10点,上午11点等的日内价格清单(数字格式如15011 15012等)。
我想仅保留最近5天的观察结果以及从't'开始的接下来的5个可用天数,并删除其他所有内容。 有没有办法做到这一点?
我尝试过使用 如果日期< & t - 5或日期> & t + 5然后删除; 但是,由于有周末/假期,我没有得到我想要的所有观察结果。
提前多多感谢!
答案 0 :(得分:1)
没有太多信息可以继续,但这是一个可能的解决方案:
/* Invent some data */
data have;
do date=15001 to 15020;
do time='09:00't,'10:00't,'11:00't;
price = ranuni(0) * 10;
output;
end;
end;
run;
/* Your macro variable identifying the target "date" */
%let t=15011;
/* Subset for current and following datae*/
proc sort data=have out=temp(where=(date >= &t));
by date;
run;
/* Process to keep only current and following five days */
data current_and_next5;
set temp;
by date;
if first.date then keep_days + 1; /* Set counter for each day */
if keep_days <= 6; /* Days to keep (target and next five) */
drop keep_days; /* Drop this utility variable */
run;
/* Subset for previous and sort by date descending */
proc sort data=have out=temp(where=(date < &t));
by descending date;
run;
/* Process to keep only five previous days */
data prev5;
set temp;
by descending date;
if first.date then keep_days + 1; /* Set counter for each day */
if keep_days <= 5; /* Number of days to keep */
drop keep_days; /* Drop this utility variable */
run;
/* Concatenate together and re-sort by date */
data want;
set current_and_next5
prev5;
run;
proc sort data=want;
by date;
run;
当然,此解决方案表明您的起始数据包含所有有效“交易日”的观察结果,并返回所有内容而不进行日期算术运算。更好的解决方案是要求您创建一个包含所有有效日期的“交易日历”数据集。你可以很容易地处理周末,但假期和其他“非交易日”是非常具体的网站;因此,使用日历几乎总是首选。
更新:Joe的评论让我更仔细地重新阅读了这个问题。这应该总共返回十一(11)天的数据;五天前,五天后和目标日期。但是,更好的解决方案是使用日历参考表。
答案 1 :(得分:0)
试试这个
/* Get distinct dates before and after &T */
proc freq data=mydata noprint ;
table Date /out=before (where=(Date < &T)) ;
table Date /out=after (where=(Date > &T)) ;
run ;
/* Take 5 days before and after */
proc sql outobs=5 ;
create table before2 as
select Date
from before
order by Date descending ;
create table after2 as
select Date
from after
order by Date ;
quit ;
/* Subset to 5 available days before & after */
proc sql ;
create table final as
select *
from mydata
where Date >= (select min(date) from before2)
and Date <= (select max(date) from after2)
order by Date ;
quit ;