SAS EG 滞后计算问题,不将 las 计算为 t - t-1 [英] SAS EG lagcalculation issue, not calculating las as t - t-1

查看:44
本文介绍了SAS EG 滞后计算问题,不将 las 计算为 t - t-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解决那里的问题吗:

can some one resolve the issue there:

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,第二行是 .):

And what I got result is as (0 in first line but . in second):

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) )为什么????代码有问题??

(here When machine changed, lag is taken as t - (t-2) and not as t - (t-1) ) why???? something wrong in the code ??

   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

推荐答案

我能够这样做...

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;

这给了我想要的结果..

Which was giving me the result I wanted..

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

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

But Now I need to do one more thing... Total = 1158 + 356 for 17JUL09 and so forth. i.e. add a column in which we will have cummulative sum values grouped by Date.

请问有什么建议吗?

这篇关于SAS EG 滞后计算问题,不将 las 计算为 t - t-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆