SAS-如何计算具有一定滞后的观测值

SAS-How to calculate observations with certain lags

假设数据集有两列

Date   Time     Price  
01-03  11:00      1
01-03  11:02      2
01-03  11:02      3
01-03  11:03      4
01-03  11:07      5
01-04  11:00      4
01-04  11:01      6
01-04  11:01      7 

我需要添加一个新列,该列等于同一天最近几分钟的发布价格。如果这个分钟有多个价格,它应该 select 第一个。例如

Date   Time     Price   New
01-03  11:00      1      2
01-03  11:02      2      4
01-03  11:02      3      4
01-03  11:03      4      5
01-03  11:07      5      . 
01-04  11:00      4      6
01-04  11:01      6      .
01-04  11:01      7      .

我用代码(by date; if first.time;)解决了多日期的问题。由于时间间隔不固定,在这种情况下我不能使用滞后功能。所以我不知道如何 select 下一分钟的第一个价格。谁能给我一些解决方案?谢谢

仅使用基本 SAS,这并不是特别困难,尽管如果您有非常大的数据集,这不是最有效的方法,但总体上应该可以正常执行。

我们从下一行开始遍历数据集,直到找到具有不同时间或不同日期的行。如果时间不同,则将该价格保存为新价格,如果日期不同(或 EOF),则清除新价格变量。

data have;
input Date :. Time :time5. Price;
format time time5.;
datalines;
01-03  11:00      1
01-03  11:02      2
01-03  11:02      3
01-03  11:03      4
01-03  11:07      5
01-04  11:00      4
01-04  11:01      6
01-04  11:01      7 
;;;;
run;

data want;
  set have nobs=nobs;
  do _i = _n_ to nobs until (new_date ne date or new_time > time);
    set have(rename=(date=new_date price=new_price time=new_time)) point=_i;
  end;
  if (date ne new_date) or (_i > nobs) then call missing(new_price);  *times that it searched beyond limits;
run;