SAS DATA STEP:基于前一行/下一行的计算

时间:2014-02-23 17:27:03

标签: sas

我有以下示例数据:

data have;
input username $  betdate : datetime. stake;
dateOnly = datepart(betdate) ;
format betdate DATETIME.;
format dateOnly ddmmyy8.;
datalines; 
player1 12NOV2008:12:04:01 90
player1 04NOV2008:09:03:44 30
player2 07NOV2008:14:03:33 120
player1 05NOV2008:09:00:00 50
player1 05NOV2008:09:05:00 30
player1 05NOV2008:09:00:05 20
player2 09NOV2008:10:05:10 10
player2 15NOV2008:15:05:33 35
player1 15NOV2008:15:05:33 35
player1 15NOV2008:15:05:33 35
run;
proc print;run;

proc sort data=have; by username dateonly betdate; run;
data want;
set have;
by username dateonly betdate;   
if first.username then calendarTime = 0;
if first.dateonly then calendarTime + 1;
if first.username then eventTime = 0;
if first.betdate then eventTime + 1;
previousBetdateForPlayer = .;
nextBetdateForPlayer = .;
run;
proc print;run;

如何填写变量'previousBetdateForPlayer'和'nextBetdateForPlayer'的值。我知道如何使用PROC SQL,但希望有一种更有效的方法来使用DATA STEP。

需要为每个用户名计算这两个变量,因此对于每个用户名的第一次下注,'previousBetdateForPlayer'的值将为NULL。对于每个用户名的最终赌注,'nextBetdateForPlayer'的值将为NULL。

1 个答案:

答案 0 :(得分:2)

data want; 
  set have; 

  /*join right the same dataset shifted 1 row up*/
  if not eof then do;
    set have(firstobs=2
                    keep=betdate 
                     rename=(betdate=nextBetdateForPlayer)) end=eof;

  end;

    by username dateonly betdate;
    if first.username then calendarTime = 0; 
    if first.dateonly then calendarTime + 1;
    if first.username then eventTime = 0;
    if first.betdate then eventTime + 1; 

   /*add previous and nullify it if first bet*/
    previousBetdateForPlayer = lag(previousBetdateForPlayer); 
    if first.username then previousBetdateForPlayer=.;

    /*nullify next if  last bet*/
    if last.username then nextBetdateForPlayer = .;
 run;