2

我有一个每天上午 9 点、上午 10 点、上午 11 点等的日内价格列表(采用数字格式,例如 15011 15012 等)

我只想保留从日期“t”开始的最后 5 天和接下来 5 天的观察结果,并删除其他所有内容。有没有办法做到这一点?

我尝试使用 if date < &t - 5 or date > &t + 5 then delete; 但是,由于有周末/节假日,我没有得到我想要的所有观察结果。

提前非常感谢!

4

2 回答 2

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;

当然,此解决方案建议您的起始数据包含所有有效“交易日”的观察结果,并在不进行日期运算的情况下返回所有内容。一个更好的解决方案需要您创建一个包含所有有效日期的“交易日历”数据集。您可以轻松应对周末,但节假日和其他“非交易日”是特定于站点的;因此,几乎总是首选使用日历。

更新:乔的评论让我更仔细地重新阅读了这个问题。这应该返回总共十一 (11) 天的数据;五天前、五天后和目标日期。但是,更好的解决方案仍然是使用日历参考表。

于 2013-09-28T15:37:44.190 回答
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 ;
于 2013-09-30T12:06:44.333 回答