两个方法:
data have;
infile cards missover;
input id $ name $ age pid :$18. tnm $ entdat yymmdd10.;
format entdat yymmdd10.;
cards;
100194 ZTJ 61 . T1N0M0 2010-1-19
100194 ZTJ 61 120104195009182922 . 2010-6-12
100196 YZX . 12010519510913422X T2N1M0 2010-6-28
100878 YLY 50 . T3N1M0 2009-2-1
100890 DML . 120109550216302 T2N1M1 2009-8-18
;
run;
proc sort data=have;
by id entdat;
run;
/*方法1*/
data have1(keep=id pid);
set have;
where pid ne ' ';
run;
data have2;
set have;
by id;
if first.id;
run;
data want;
update have2 have1;
by id;
if pid ne ' ' then do;
if length(pid)< 18 then age=year(entdat)-input('19'||substr(pid,7,2),4.);
else age=year(entdat)-put(substr(pid,7,4),4.);
end;
run;
proc print;
run;
/*方法2*/
proc sql;
create table have3 as select id, name, age, max(pid) as pid,
max(tnm) as tnm,entdat
from have
group by id;
quit;
data want;
set have3;
by id;
if first.id;
if pid ne ' ' then do;
if length(pid)< 18 then age=year(entdat)-input('19'||substr(pid,7,2),4.);
else age=year(entdat)-put(substr(pid,7,4),4.);
end;
run;
proc print;run;


雷达卡




京公网安备 11010802022788号







