proc sort data=sashelp.class out=avgd;
by sex;
run;
proc sql;
create table avg as select avg(age)as avrg, sex from sashelp.class
group by sex;
quit;
data bb;
SET AVGD avg;
BY SEX;
if missing (age)THEN DO
SEX= "" ;
age=avrg;
END;
DROP AVRG;
run;
average
data base;
input id visit$ aval;
cards;
101 scr 20
101 base 25
101 v1 25
101 v2 26
102 scr 25
102 base 30
102 v1 25
102 v2 30
103 scr 23
103 base 33
;
run;
create seq
data gg;
set base;
by id;
if first.id=1 then seq=1;
else seq+1;
run;
data ds avg1;
set gg;
by id;
if seq > 2 then output avg1;
else output ds;
run;
proc sql;
create table dos as select avg(aval) as avg,id from avg1
group by id;
quit;
data final;
set ds avg1 dos;
by id;
if missing (aval) then aval=avg;
/*drop seq avg;*/
run;
by sex;
run;
proc sql;
create table avg as select avg(age)as avrg, sex from sashelp.class
group by sex;
quit;
data bb;
SET AVGD avg;
BY SEX;
if missing (age)THEN DO
SEX= "" ;
age=avrg;
END;
DROP AVRG;
run;
average
data base;
input id visit$ aval;
cards;
101 scr 20
101 base 25
101 v1 25
101 v2 26
102 scr 25
102 base 30
102 v1 25
102 v2 30
103 scr 23
103 base 33
;
run;
create seq
data gg;
set base;
by id;
if first.id=1 then seq=1;
else seq+1;
run;
data ds avg1;
set gg;
by id;
if seq > 2 then output avg1;
else output ds;
run;
proc sql;
create table dos as select avg(aval) as avg,id from avg1
group by id;
quit;
data final;
set ds avg1 dos;
by id;
if missing (aval) then aval=avg;
/*drop seq avg;*/
run;
No comments:
Post a Comment