我想枚举SAS中的群组。我有以下数据集,并附加了我正在寻找的2种可能的方案。 locationnurseunitcodedesc transaction_start_dt_tm transaction_stop_dt_tm这就是我想要的更好
STATION 1 8/31/16 10:33 10/3/16 9:54 1 1
STATION 2 10/3/16 9:54 10/3/16 9:54 1 1
STATION 3 10/3/16 9:54 10/3/16 9:54 1 0
STATION 3 10/3/16 9:54 10/3/16 9:54 2 0
STATION 3 10/3/16 9:54 10/3/16 12:11 3 1
STATION 4 10/3/16 12:11 10/3/16 18:39 1 1
STATION 3 10/3/16 18:39 10/4/16 12:26 1 0
STATION 3 10/4/16 12:26 10/13/16 10:43 2 0
STATION 3 10/13/16 10:43 10/13/16 10:43 3 0
STATION 3 10/13/16 10:43 10/4/16 12:25 4 1
当我使用以下代码时,seq编号永远不会高于2
data wantONETWO;
set WANTONE;
RETAIN SEQ 0;
SEQ=1;
PRIOR_UNIT = LAG(locationnurseunitcodedesc);
IF locationnurseunitcodedesc = PRIOR_UNIT THEN DO;
SEQ + 1;
END;
run;
最佳
亚伦
答案 0 :(得分:0)
你的行动倒退了。始终递增,但在启动新组时重置为1。我不打算输入你的长变量名。我们只是使用STATION。
data want ;
set have ;
seq+1;
if station ne lag(station) then seq=1 ;
run;
您也可以使用BY组处理,但由于您的组未排序,因此notsorted
语句中需要BY
关键字。
data want ;
set have ;
by station notsorted;
seq+1;
if first.station then seq=1 ;
run;