33# lwien007
谢谢您的code,本以为已经实现我的目标,但在运行我的原数据后,还是发现有错误。我把原数据放在附件test中,运行您的code后,还是发现有些时间段补充的数据有问题:比如原数据给了199409,199410,199503的数据,所以我就希望能让199411,199412,199501,199502这四个缺失的月和199410的数据一样,但运行您的code后,发现这段时间内没有补充199411,199412的数据,而直接到199501了。不知是何原因?哪位大侠指点一下!
data test1;
set test;
by company date;
if first.date then i=0;
i+1;
date=input(compress(date),yymmn6.);
date2=date;
format date yymmn6.;
run;
data test2 (keep=company date product amt);
if 0 then set test1;
if _n_=1 then do;
declare hash h(dataset:'test1');
h.definekey('company','date2','i');
h.definedata(all:'yes');
h.definedone();
call missing(company,date2,product,amt,i);
end;
set test1;
by company date;
retain predate prei;
if first.company then output;
else do;
if first.date then do;
if year(date)=year(predate) then do;
if month(date)>month(predate)+1 then do;
curdate=date;
curproduct=product;
curamt=amt;
do j=month(predate)+1 to month(date)-1;
date2=predate;
do i=1 to prei;
rc=h.find();
if rc=0 then do;
date=mdy(j,01,year(date));
output;
end;
end;
end;
date=curdate;
product=curproduct;
amt=curamt;
output;
end;
else output;
end;
else if month(date)=1 then output;
else do;
curdate=date;
curproduct=product;
curamt=amt;
do j=1 to month(date)-1;
date2=predate;
do i=1 to prei;
rc=h.find();
if rc=0 then do;
date=mdy(j,01,year(curdate));
output;
end;
end;
end;
date=curdate;
product=curproduct;
amt=curamt;
output;
end;
end;
else output;
If last.date then do;
predate=date;
prei=i;
end;
end;
run;