Given Dataset:
data hello;
input id value;
cards;
101 22
101 44
103 22
104 22
104 55
106 22
106 .
;
run;
I am trying to create a nvariable and Dtype by Id variable as follows:
Id value Nvalue Dtype
101 22
101 44
33 Average
103 22
104 22
104 55
38.5 Average
106 22
106 .
22 LOCF
Is there any possible way to get the output as mentioned above.
Here is my attempt. I added a few more observations to the example to show you the result when missing values occur in a more unpredictable pattern.
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 $10;
if last.id then
do;
if missing=1 then
dtype="LOCF";
else dtype="Average";
end;
run;