首先填充空白。在SAS

时间:2017-08-25 20:19:33

标签: sas

我有一个看起来像这样的简单数据

+----------+------+--------+
| cis_code | yymm | sector |
+----------+------+--------+
| CTAGBTH  | 1707 | BANK   |
| CTAGBTH  | 1706 |        |
| CTAGBTH  | 1705 |        |
| CTAGBTH  | 1703 |        |
| CTAGBTH  | 1702 | RETAIL |
| CTAGBTH  | 1701 |        |
| CTAGBTH  | 1612 |        |
| CTAGBTG  | 1703 |        |
| CTAGBTG  | 1702 | AGRI   |
| CTAGBTG  | 1701 |        |
| CTAGBTG  | 1612 | RETAIL |
| CTAGBET  | 1705 |        |
| CTAGBET  | 1510 | FSI    |
| CTAGBET  | 1504 |        |
+----------+------+--------+

在这个问题中,我想创建一个新的变量sector_summary,其中空格应该由扇区替换,并且cid_code会发生变化。我尝试过使用first.yymm和first.sector,但遗憾的是没有任何工作。我也尝试了一些if语句,但不确定if语句是否适用于所有条件。数据应如下所示

+-----------+-------+---------+---------------+
| cis_code  | yymm  |  sector |  Sector_Group |
+-----------+-------+---------+---------------+
| CTAGBET   |  1705 |         |               |
| CTAGBET   |  1510 |  FSI    |  FSI          |
| CTAGBET   | 1504  |         |  FSI          |
| CTAGBTG   |  1703 |         |               |
| CTAGBTG   | 1702  | AGRI    |  AGRI         |
| CTAGBTG   |  1701 |         |  AGRI         |
| CTAGBTG   | 1612  | RETAIL  | RETAIL        |
| CTAGBTH   | 1707  | BANK    |  BANK         |
| CTAGBTH   |  1706 |         |  BANK         |
| CTAGBTH   | 1705  |  BANK   |               |
| CTAGBTH   | 1703  |  BANK   |               |
| CTAGBTH   | 1702  | RETAIL  | RETAIL        |
| CTAGBTH   |  1701 |         |  RETAIL       |
| CTAGBTH   |  1612 |         |  RETAIL       |
+-----------+-------+---------+---------------+

所以基本上如果给定的yymm有一个空白的部门,那么行业组在随后的几个月内也将是空白的,除非有另一个部门被替换,并且将在未来几个月进一步发展。 /> 请让我知道如何去做它

1 个答案:

答案 0 :(得分:2)

您只需要保留这些值。看起来您还需要对数据进行排序(或使用NOTSORTED语句中的BY选项。

data want ;
  set have ;
  by cis_code descending yymm ;
  if first.cis_code or not missing(sector) then sector_group=sector;
  retain sector_group;
run;