繁体   English   中英

最近5天的SAS返回数据

[英]SAS Return data from the last available 5 days

我有每天上午9点,上午10点,上午11点等的日内价格列表(格式为15011 15012等)。

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

我尝试使用日期<&t-5或日期>&t + 5然后删除; 但是,由于有周末/节假日,所以我没有得到我想要的所有观察结果。

在此先多谢!

信息不多,但是下面是一个可能的解决方案:

/* 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)天的数据; 前五天,后五天和目标日期。 但是,更好的解决方案是使用日历参考表。

尝试这个

/* 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM