[英]SAS Return data from the last available 5 days
I have a list of intra-day prices at 9am, 10am, 11am etc. for each day (which are in number formats such as 15011 15012 etc.) 我有每天上午9点,上午10点,上午11点等的日内价格列表(格式为15011 15012等)。
I want to only keep the observations from the last available 5 days and the next 5 available days from the date 't' and delete everything else. 我只想保留从“ t”日期开始的最近5天和接下来的5天的观测值,并删除其他所有内容。 Is there a way to do this?
有没有办法做到这一点?
I tried using if date < &t - 5 or date > &t + 5 then delete; 我尝试使用日期<&t-5或日期>&t + 5然后删除; However, since there are weekends/holidays I don't get all the observations I want.
但是,由于有周末/节假日,所以我没有得到我想要的所有观察结果。
Thanks a lot in advance! 在此先多谢!
Not much info to go on, but here is a possible solution: 信息不多,但是下面是一个可能的解决方案:
/* 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;
Of course, this solution suggests that your starting data contains observations for all valid "trading days" and returns everything without doing date arithmetic. 当然,此解决方案建议您的起始数据包含对所有有效“交易日”的观察值,并且不进行日期算术就返回所有内容。 A much better solution would require that you create a "trading calendar" dataset with all valid dates.
更好的解决方案将要求您创建一个包含所有有效日期的“交易日历”数据集。 You can easily deal with weekends, but holidays and other "non-trading days" are very site specific;
您可以轻松地度过周末,但是假期和其他“非交易日”是特定于站点的。 hence using a calendar is almost always preferred.
因此,几乎总是首选使用日历。
UPDATE: Joe's comment made me re-read the question more carefully. 更新:乔的评论使我更仔细地重新阅读了问题。 This should return a total of eleven (11) days of data;
这应该返回总共十一(11)天的数据; five days prior, five days following, and the target date.
前五天,后五天和目标日期。 But still, a better solution would use a calendar reference table.
但是,更好的解决方案是使用日历参考表。
Try this 尝试这个
/* 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 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.