SAS 中的枚举
Enumeration in SAS
我想枚举 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
当我使用下面的代码时,序列号永远不会超过 2
data wantONETWO;
set WANTONE;
RETAIN SEQ 0;
SEQ=1;
PRIOR_UNIT = LAG(locationnurseunitcodedesc);
IF locationnurseunitcodedesc = PRIOR_UNIT THEN DO;
SEQ + 1;
END;
run;
最佳
亚伦
你的操作倒退了。始终递增,但在开始新组时重置为 1。我不会输入你的长变量名。让我们改用 STATION。
data want ;
set have ;
seq+1;
if station ne lag(station) then seq=1 ;
run;
您也可以使用 BY 组处理,但由于您的组未排序,因此您需要在 BY
语句中使用 notsorted
关键字。
data want ;
set have ;
by station notsorted;
seq+1;
if first.station then seq=1 ;
run;
我想枚举 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
当我使用下面的代码时,序列号永远不会超过 2
data wantONETWO;
set WANTONE;
RETAIN SEQ 0;
SEQ=1;
PRIOR_UNIT = LAG(locationnurseunitcodedesc);
IF locationnurseunitcodedesc = PRIOR_UNIT THEN DO;
SEQ + 1;
END;
run;
最佳
亚伦
你的操作倒退了。始终递增,但在开始新组时重置为 1。我不会输入你的长变量名。让我们改用 STATION。
data want ;
set have ;
seq+1;
if station ne lag(station) then seq=1 ;
run;
您也可以使用 BY 组处理,但由于您的组未排序,因此您需要在 BY
语句中使用 notsorted
关键字。
data want ;
set have ;
by station notsorted;
seq+1;
if first.station then seq=1 ;
run;