通过执行平均和 LOCF 运算得出 new_variable?
Deriving a new_variable by performing average and LOCF operations?
给定的数据集:
data hello;
input id value;
cards;
101 22
101 44
103 22
104 22
104 55
106 22
106 .
;
run;
我正在尝试通过 Id 变量创建一个 nvariable 和 Dtype,如下所示:
Id value Nvalue Dtype
101 22
101 44
33 Average
103 22
104 22
104 55
38.5 Average
106 22
106 .
22 LOCF
有没有办法得到上面提到的输出。
这是我的尝试。我在示例中添加了更多观察结果,以向您展示当缺失值以更不可预测的模式出现时的结果。
data have;
input id value;
cards;
101 22
101 44
103 22
104 22
104 55
106 22
106 .
107 25
107 .
107 22
108 .
108 .
109 10
109 12
;
run;
proc sql;
create table averages as
select id, avg(value) as nvalue
from have
group by id;
quit;
data want (drop=missing);
set have averages;
by id;
retain missing;
if first.id then
missing=.;
if not last.id and value=. then
missing=1;
length dtype ;
if last.id then
do;
if missing=1 then
dtype="LOCF";
else dtype="Average";
end;
run;
给定的数据集:
data hello;
input id value;
cards;
101 22
101 44
103 22
104 22
104 55
106 22
106 .
;
run;
我正在尝试通过 Id 变量创建一个 nvariable 和 Dtype,如下所示:
Id value Nvalue Dtype
101 22
101 44
33 Average
103 22
104 22
104 55
38.5 Average
106 22
106 .
22 LOCF
有没有办法得到上面提到的输出。
这是我的尝试。我在示例中添加了更多观察结果,以向您展示当缺失值以更不可预测的模式出现时的结果。
data have;
input id value;
cards;
101 22
101 44
103 22
104 22
104 55
106 22
106 .
107 25
107 .
107 22
108 .
108 .
109 10
109 12
;
run;
proc sql;
create table averages as
select id, avg(value) as nvalue
from have
group by id;
quit;
data want (drop=missing);
set have averages;
by id;
retain missing;
if first.id then
missing=.;
if not last.id and value=. then
missing=1;
length dtype ;
if last.id then
do;
if missing=1 then
dtype="LOCF";
else dtype="Average";
end;
run;