SAS \​​ SQL group by和count by..finding第一个和最后一个并进行比较

时间:2013-01-26 13:44:01

标签: sql sas

我有一个问题,我被困在试图解决...我基本上有一个看起来像这样的小桌子..

ID    user    operation     date
1     Jon     In         01/01/2013
1     Paul    Out        02/01/2013
1     Paul    In         03/01/2013
2     Jon     Deleted    04/01/2013
2     Paul    In         05/01/2013
2     Jon     Out        06/01/2013
1     Jon     out        07/01/2013
1     Jon     Deleted    08/01/2013
2     Jon     out        09/01/2013 
1     Jon     in         11/01/2013

所以基本上我需要找到Jon和Paul同一ID的位置" In"。因此,关于Jon和Paul的例子都是ID 1,而ID 2我只会找到Paul。因此,必须包含该行的最后一个已知日期,因为它在以后更改时会被忽略。所以简而言之,我需要找到IN存在的ID值,并且以后没有设置为" out"或提前致谢!

2 个答案:

答案 0 :(得分:1)

如果您正确排序数据,请先使用。在数据步骤中应该有效(除非你在同一天有一个id和用户有超过1个条目,如果是这种情况,你需要确定要采取哪一个)。

data have;
input ID user $ operation $ date :mmddyy10.;
format date mmddyy10.;
cards;
1     Jon     In         01/01/2013
1     Paul    Out        02/01/2013
1     Paul    In         03/01/2013
2     Jon     Deleted    04/01/2013
2     Paul    In         05/01/2013
2     Jon     Out        06/01/2013
1     Jon     out        07/01/2013
1     Jon     Deleted    08/01/2013
2     Jon     out        09/01/2013 
1     Jon     in         11/01/2013
;
run;

proc sort data=have;
by id user descending date;
run;

data want;
set have;
by id user;
if first.user and lowcase(operation)='in' then output;
run;

答案 1 :(得分:0)

以下代码是针对您的特定问题和数据的临时代码。使用宏逻辑虽然并非不可能,但是将代码概括为适用于任意数量的用户会更复杂一些。

proc sort data=original_dataset;
   by date id user operation;
run;

data results_dataset;
   set original_dataset;
   retain _jon _paul 0;
   drop _jon _paul;
   if trim(left(lowcase(operation))) eq 'deleted' then delete;
   else if trim(left(lowcase(operation))) eq 'in' then do;
      if trim(left(lowcase(user))) eq 'jon' then _jon = 1;
      else if trim(left(lowcase(user))) eq 'paul' then _paul = 1;
   end;
   else if trim(left(lowcase(operation))) eq 'out' then do;
      if trim(left(lowcase(user))) eq 'jon' then _jon = 0;
      else if trim(left(lowcase(user))) eq 'paul' then _paul = 0;
   end;
   if (_jon eq 1) and (_paul eq 1) then output;
run;