使用 SAS 计算非零值

Calculating non-zero values using SAS

我有以下数据。限额变量仅适用于第一条记录

如果 Limit 为 0 则 PostBal 应与 Balance 相同,否则为 Limit+Balance

   Id        date              Limit          Balance
   101      4/1/2019        0         50
   101      4/1/2019                 120
   101      4/2/2019                 150
   102      4/1/2019        100   100
   102      4/1/2019                    50
   102      4/2/2019                    25

想要的结果:

   Id        date           Limit      Balance  PostBal
   101      4/1/2019        0            50             50
   101      4/1/2019                    120          120   
   101      4/2/2019                    150          150
   102      4/1/2019       100       100           200
   102      4/1/2019                      50          250
   102      4/2/2019                      25          275

我的代码:

    Data want;
     Set have;
    By id date;
    if first.date and not first.id then Limit=PostBal;
    If first.date then do;
     PostBal=Limit;
   End;
   PostBal+Balance;
 Run;

如果 Limit >0 但 Limit=0 则不行。提前感谢您的帮助。

您需要跟踪 LIMIT 为零的事实,以便您可以使用该事实来更改 PostBal 的计算方式。因此,为此创建一个新变量。

data want;
  set have;
  by id ;
  if first.id then limit_copy =limit;
  retain limit_copy;
  if first.id then PostBal=limit;
  if limit_copy ne 0 then PostBal+Balance;
  else postbal=balance;
run;