计数器变量不打算重置

问题描述

这是另一个post I made before.

的后续

事实证明,在创建“计数器”变量时,我想包含一个条件,因此如果名为“结果”的变量采用“超时”值,则计数器为 0,但不应重置该 Id 的计数在那一天。

这是我现在的代码

DATA want;
SET have;
BY  ID date time;
RETAIN attempt;

IF first.Date then RealAttempts = 1;
ELSE RealAttempts = min(3,RealAttempts+1);
IF Outcome ="Out Of Time" then RealAttempts=0;
run;

它应该是这样的:

data have;
infile datalines dsd delimiter=',';
input date :ddmmyy10. ID  time :time8.  Outcome :$40. Attempt  ;
format date ddmmyy10.;
format time time8.;
datalines;
05/11/2020,1000,8:15:23,"Answered",1,05/11/2020,8:20:10,"Out Of Time",0
05/11/2020,8:21:10,2
05/11/2020,9:05:15,0
;

但这就是它的外观(在第 3 行它重置为 1 而不是 2)

data have;
infile datalines dsd delimiter=',1
05/11/2020,0
;

有什么想法吗?

解决方法

如果我明白您想要什么,只需添加第二个变量,该变量具有您想要实际报告的值,而不要管“计数器”。因此,您可以根据自己的条件将此报告字段设置为零,否则将返回截断的计数。

DATA want;
  SET have;
  BY  ID date time;
  RETAIN RealAttempts ;

  IF first.Date then RealAttempts = 1;
  ELSE RealAttempts = RealAttempts+1;

  IF Outcome ="Out Of Time" then ReportedAttempts=0;
  ELSE ReportedAttempts=min(3,RealAttempts);
run;

如果“超时”观察也不应该增加计数器,那么进一步改变逻辑。

DATA want;
  SET have;
  BY  ID date time;
  RETAIN RealAttempts ;

  IF first.Date then RealAttempts = 0;

  IF Outcome ="Out Of Time" then ReportedAttempts=0;
  ELSE do ;
    RealAttempts = RealAttempts+1;
    ReportedAttempts=min(3,RealAttempts);
  end;
run;