SAS EG滞后计算问题,而不是将las计算为t-t-1

时间:2016-11-08 10:57:39

标签: sas

可以解决那里的问题:

data want;
 set have;
 by mac;

 if first.mac then do; DayDif=0; KmDif=0; end; 
       else do;
          DayDif = Date - lag(Date); /* calculate the difference between two dates */
          KmDiff = Kms - lag(Kms);
      end;
run;

我得到的结果是(第一行为0但第二行为。)

Mac          Date   Kms           DayDif    KmDif
SP0001      10DEC07 1885462.00000   0        0
SP0001      12DEC07 1885462.00000   .        .
SP0001      30APR09 1885462.00000   505      0
SP0001      15JUL09 1886577.00000   76       1115
SP0001      16JUL09 1887667.00000   1        1090
SP0001      17JUL09 1889181.00000   1        1514
SP0001      17JUL09 1888825.00000   0       -356

.
. 

(这里当机器改变时,滞后被视为t - (t-2)而不是t - (t-1))为什么????代码中出错了什么?

   Machine  Date    Kms          DayDif KmDif
   SP0001   01OCT14 2898108.00000   1   1059
   SP0001   02OCT14 2899148.00000   1   1040
   HP0001   03OCT14 2900334.00000   1   1186
   HP0002   17JAN08 926384.00000    0   0
   HP0002   18JAN08 926384.00000    -2450   -1973950
   HP0002   28APR09 1237332.00000   466 310948
   HP0002   29APR09 1238599.00000   1   1267

2 个答案:

答案 0 :(得分:3)

lag()函数的值不是从先前的观察中得到的(一个常见的误解).. 每次执行函数时它们都存储在内存中(参见{{3 }})。

在您的示例中,当机器更改时,由于条件逻辑(if first.mac then do / else)而不执行滞后函数 - 因此“t-2”值只是上一次迭代的结果。请尝试以下方法:

data want;
  set have;
  by mac;
  if first.mac then do; 
    DayDif=lag(Date); /* executing the lag for subsequent iteration */
    DayDif=0; 
    KmDif=0; 
  end; 
  else do;
    DayDif = Date - lag(Date); /* calculate the difference between two dates */
    KmDiff = Kms - lag(Kms);
  end;
run;

答案 1 :(得分:0)

我能够这样做......

data want;
 set have;
    by mac;
      RETAIN lag_date lag_kms;
      DROP   lag_date lag_kms; 
       if first.Mac
          then do; 
                  DayDif=0;
                  KmDif=0;
          end; 
          else do;
              DayDif = abs(Date - lag_date);
              KmDif = abs(Kms - lag_kms);
      end;
      lag_date = Date;
      lag_kms = Kms;
run;

这给了我想要的结果..

Mac             Date    Kms        DayDif   KmDif
SP0001  10DEC07 1885462.00000   0         0
SP0001  12DEC07 1885462.00000   2         0
SP0001  30APR09 1885462.00000   505       0
SP0001  15JUL09 1886577.00000   76        1115
SP0001  16JUL09 1887667.00000   1         1090
SP0001  17JUL09 1888825.00000   1         1158
SP0001  17JUL09 1889181.00000   0         356

但是现在我需要再做一件事...... 17JUL09的总数= 1158 + 356,依此类推。即添加一列我们将按日期分组的累计和值。

有什么建议吗?