如何为不同的组填充空值。 我的数据如下:
id visit status var reason
1 1 Done x1
1 1 Done x2
1 1 Done x3
1 2 Not Done x1 text1
1 2 Not Done x2
1 2 Not Done x3
1 3 Done x1
1 3 Done x2
1 3 Done x3
2 1 Not Done x1 text2
2 1 Not Done x2
2 1 Not Done x3
2 2 Done x1
2 2 Done x2
2 2 Done x3
2 3 Done x1
2 3 Done x2
2 3 Done x3
输出应该是这样的
id visit status var reason
1 1 Done x1
1 1 Done x2
1 1 Done x3
1 2 Not Done x1 text1
1 2 Not Done x2 text1
1 2 Not Done x3 text1
1 3 Done x1
1 3 Done x2
1 3 Done x3
2 1 Not Done x1 text2
2 1 Not Done x2 text2
2 1 Not Done x3 text2
2 2 Done x1
2 2 Done x2
2 2 Done x3
2 3 Done x1
2 3 Done x2
2 3 Done x3
我认为这是一个非常简单的问题,但就目前而言,我还没能解决它。任何帮助将不胜感激!
答案 0 :(得分:1)
使用first.variable
语句时,使用by
功能时,这是一项简单的任务。
基本上我创建了一个新变量,只要遇到新访问,就会赋予Reason值。 retain
语句确保为Id和Visit不更改的所有后续行复制新变量值。然后我只删除原始的Reason变量并重命名新变量。
data have;
infile datalines dsd;
input id visit status &$ var $ reason $;
datalines;
1, 1, Done, x1,,
1, 1, Done, x2,,
1, 1, Done, x3,,
1, 2, Not Done, x1, text1,
1, 2, Not Done, x2,,
1, 2, Not Done, x3,,
1, 3, Done, x1,,
1, 3, Done, x2,,
1, 3, Done, x3,,
2, 1, Not Done, x1, text2,
2, 1, Not Done, x2,,
2, 1, Not Done, x3,,
2, 2, Done, x1,,
2, 2, Done, x2,,
2, 2, Done, x3,,
2, 3, Done, x1,,
2, 3, Done, x2,,
2, 3, Done, x3
;
run;
data want;
set have;
retain reason_new;
by id visit;
if first.visit then reason_new=reason;
drop reason;
rename reason_new = reason;
run;