这里是一个示例数据集。下面的场景表示帐户事务历史记录的列表:
有些帐户的开户日期缺失,但对于相同的帐户,开放日期应该是相同的。我要做的是用帐户的前一个/以后的值替换丢失的日期(如果有的话);否则,保持它为空。
data want;
input Customer $ Account_id Bank_id $ Year;
datalines;
A 1 BA .
A 2 UB .
A 3 UB 2012
A 3 UB 2012
A 4 UB 2013
A 4 UB 2013
A 5 UB .
B 1 WF 2014
B 1 WF 2014
B 6 WF .
;我看了这个例子:How to write first non-missing value to first missing observations很有帮助,但我不能调整它来处理我的案例,因为它有多个组。
发布于 2016-07-20 23:43:25
这应该可以做到:
proc sort data=have;
by Customer
Bank_id
Account_id
descending Year;
run;
data want;
set have;
by Customer Bank_id Account_id;
retain year_tmp (.);
if not last.Account_id and Year ne . then year_tmp=Year;
else if Year = . then Year = year_tmp;
if last.Account_id then year_tmp = .;
drop year_tmp;
run;我们所做的是声明一个retain变量,也就是说,一个将其值从一行保存到下一行的变量。然后使用last标志,我们要么将现有的年份存储在保留变量中(或者将已经存储的值归因于缺少的年份)。最后,我们在每个Account_id的最后一行重置reset变量。
https://stackoverflow.com/questions/38491049
复制相似问题