仅使用非缺失观察值填充特定范围内的缺失值

Fill Missing Values for a certain range with only Non-Missing Observation

您好,我想知道如何将其放入 SAS 代码中:

我的数据是这样的:

Balance | Month | Outstanding
--------+-------+-------------- 
1000    | 0     |
200     | 1     |   
300     | 2     |
400     | 3     |
500     | 4     |
800     | 5     |
750     | 6     |
650     | 7     |
740     | 8     |
580     | 9     |
650     | 10    |
523     | 11    | 7093
654     | 12    |
458     | 0     |
789     | 1     |
852     | 2     |
236     | 3     |
1258    | 4     |
4528    | 5     |
78520   | 6     |
1258    | 7     |
4821    | 8     |
15870   | 9     |
1587    | 10    |
1599    | 11    | 111776
6520    | 12    |   

我希望它看起来像这样:

Balance | Month | Outstanding
--------+-------+------------
1000    | 0     | 7093
200     | 1     | 7093
300     | 2     | 7093
400     | 3     | 7093
500     | 4     | 7093
800     | 5     | 7093
750     | 6     | 7093
650     | 7     | 7093
740     | 8     | 7093
580     | 9     | 7093
650     | 10    | 7093
523     | 11    | 7093
654     | 12    | 7093
458     | 0     | 111776
789     | 1     | 111776
852     | 2     | 111776
236     | 3     | 111776
1258    | 4     | 111776
4528    | 5     | 111776
78520   | 6     | 111776
1258    | 7     | 111776
4821    | 8     | 111776
15870   | 9     | 111776
1587    | 10    | 111776
1599    | 11    | 111776
6520    | 12    | 111776

基本上我想用 0-11 月的余额总和填充整个 0-12 月范围内的空单元格,然后为下一组 0-12 月做。

在Excel中我可以使用这个公式来做到这一点。这里假设balance的列号是A,Month是B,outstanding是C:

=IF(B2=0,SUM(A2:A13),C1)

然后将移动总和的公式向下拖动

我只是不确定如何在 SAS 代码中创建等效项。

移动求和可以用PROC EXPAND:

proc expand data=have
            out=want;
   by <by variables>;
   convert balance = outstanding / transform=(movsum 12);
run;

您的问题不完全清楚,需要重新格式化,但 proc expand 会按照您的要求进行。您可以阅读 SAS website.

上的语法

我已将输入数据设置为:

data have;
  input balance month outstanding;
  datalines;
1000     0      .
200      1      .   
300      2      .
400      3      .
500      4      .
800      5      .
750      6      .
650      7      .
740      8      .
580      9      .
650      10     .
523      11     7093
654      12     .
458      0      .
789      1      .
852      2      .
236      3      .
1258     4      .
4528     5      .
78520    6      .
1258     7      .
4821     8      .
15870    9      .
1587     10     .
1599     11     111776
6520     12     .  
;

您可以将数据与自身组合以获得所需的结果,如下所示:

data want;
  set have(where=(outstanding ne .));

  do until (month=12);
    set have(drop=outstanding);
    output;
  end;
run;