楼主: funwin
11879 50

再次求助 如何插入(补充)缺失月份的数据? [推广有奖]

31
游去游来 发表于 2009-12-22 23:04:05
受益匪浅呵呵

32
daewoo_wei 发表于 2009-12-22 23:46:51
这问题也太高深了 看来还得好好学习Microsoft啊

33
lwien007 发表于 2009-12-23 09:17:38
  1. proc import out=tmp
  2.         datafile='e:\data\tmp.xls'
  3.         dbms=excel replace;
  4.         sheet='原始';
  5. run;
  6. data tmp2;
  7.         set tmp;
  8.         by company date;
  9.         if first.date then i=0;
  10.         i+1;
  11.         date=input(compress(date),yymmn6.);
  12.         date2=date;
  13.         format date yymmn6.;
  14. run;
  15. data tmp3(keep=date company product quant);
  16.         if 0 then set tmp2;
  17.         if _n_=1 then do;
  18.                 declare hash h(dataset:'tmp2');
  19.                 h.definekey('company','date2','i');
  20.                 h.definedata(all:'yes');
  21.                 h.definedone();
  22.                 call missing(company,date2,product,quant,i);
  23.         end;
  24.         set tmp2;
  25.         by company date;
  26.         retain predate prei;
  27.         if first.company then output;
  28.         else do;
  29.                 if first.date then do;
  30.                         if year(date)=year(predate) then do;
  31.                                 if month(date)>month(predate)+1 then do;       
  32.                                         curdate=date;
  33.                                         curproduct=product;
  34.                                         curquant=quant;
  35.                                         do j=month(predate)+1 to month(date)-1;
  36.                                                 date2=predate;
  37.                                                 do i=1 to prei;
  38.                                                         rc=h.find();
  39.                                                         if rc=0 then do;
  40.                                                                 date=mdy(j,01,year(date));
  41.                                                                 output;
  42.                                                         end;
  43.                                                 end;
  44.                                         end;
  45.                                         date=curdate;
  46.                                         product=curproduct;
  47.                                         quant=curquant;
  48.                                         output;
  49.                                 end;
  50.                                 else output;
  51.                         end;
  52.                         else if month(date)=1 then output;
  53.                         else do;
  54.                                 curdate=date;
  55.                                 curproduct=product;
  56.                                 curquant=quant;
  57.                                 do j=1 to month(date)-1;
  58.                                         date2=predate;
  59.                                         do i=1 to prei;
  60.                                                 rc=h.find();
  61.                                                 if rc=0 then do;
  62.                                                         date=mdy(j,01,year(curdate));
  63.                                                         output;
  64.                                                 end;
  65.                                         end;
  66.                                 end;
  67.                                 date=curdate;
  68.                                 product=curproduct;
  69.                                 quant=curquant;
  70.                                 output;
  71.                         end;
  72.                 end;
  73.                 else output;
  74.                 if last.date then do;
  75.                         predate=date;
  76.                         prei=i;
  77.                 end;
  78.         end;
  79. run;
复制代码

34
funwin 发表于 2009-12-23 11:05:11
虽然还没有仔细看懂每一步,但初步测试了一下,是可行的!!!!
多谢 高手指点!容我再仔细研读一下!

33# lwien007

35
funwin 发表于 2009-12-23 16:04:56
33# lwien007

高人!多谢指教。
您的codes,好多语句都没有见过,能否详细解释一下?以便我更好的学习!

36
funwin 发表于 2010-1-4 19:35:57
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;

37
losttemple 发表于 2010-1-4 22:03:06
试试,没有check过
proc import out=tmp
  datafile='e:\test.xls' dbms=excel replace;
run;
data tmp2;
  set tmp;
  year=input(substr(strip(date),1,4),??best.);
  month=input(substr(strip(date),5),??best.);
run;
proc sort; by company product year month; run;

data tmp3_1;
  set tmp2;
  by company product year month;

  if first.year and month^=1 and first.product=0 then do;
    obs=_n_-1;
        _year=year;
    do i=1 to month-1;
      set tmp2 point=obs;
          month=i;
          year=_year;
      if _error_ then abort;
      output;
    end;
  end;

data tmp3_2;
  set tmp2;
  by company product year month;
  _lagmonth=lag(month);

  if first.year=0 then do;
    obs=_n_-1;
        do i=_lagmonth+1 to month-1;
          set tmp2 point=obs;
          month=i;
      if _error_ then abort;
      output;
    end;
  end;

data tmp3_3;
  set tmp2;
  by company product year month;

  if last.year=1 and last.product=0 and month^=12 then do;
    obs=_n_;
    do i=month+1 to 12;
          set tmp2 point=obs;
          month=i;
      if _error_ then abort;
      output;
    end;
  end;
run;

data tmp4;
  set tmp2 tmp3_1 tmp3_2 tmp3_3;
  drop i _year _lagmonth;
run;
proc sort; by company product year month; run;

38
funwin 发表于 2010-1-5 04:25:22
37# losttemple

谢谢您,但没有实现我想要的。

39
lwien007 发表于 2010-1-5 10:52:44
看看这个是不是你想要的结果
proc import out=tmp
        datafile='e:\test.xls'
        dbms=excel replace;
        sheet='原始';
run;

data tmp2;
        set tmp(rename=(amt=quant));
        by company date;
        if first.date then i=0;
        i+1;
        date=input(compress(date),yymmn6.);
        date2=date;
        format date yymmn6.;
        label quant='quant';
run;

data tmp3(keep=date company product quant);
        if 0 then set tmp2;
        if _n_=1 then do;
               declare hash h(dataset:'tmp2');
               h.definekey('company','date2','i');
               h.definedata(all:'yes');
               h.definedone();
               call missing(company,date2,product,quant,i);
        end;
        set tmp2;
        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;
                                     curquant=quant;
                                     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;
                                     quant=curquant;
                                     output;
                             end;
                            else output;
                     end;
                     else do;
                                                          if month(predate)<12 then do;
                          curdate=date;
                          curproduct=product;
                          curquant=quant;
                                                                  do j=month(predate)+1 to 12;
                                      date2=predate;
                                      do i=1 to prei;
                                               rc=h.find();
                                               if rc=0 then do;
                                                      date=mdy(j,01,year(predate));
                                                       output;
                                               end;
                                      end;
                                                                  end;
                         date=curdate;
                          product=curproduct;
                          quant=curquant;
                                                                end;
                                                                if month(date)=1 then output;
                             else do;
                                     curdate=date;
                                     curproduct=product;
                                     curquant=quant;
                                    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;
                                     quant=curquant;
                                    output;
                             end;
                                                        end;
             end;
             else output;
             if last.date then do;
                    predate=date;
                    prei=i;
                end;
        end;
        format predate curdate yymmn6.;
run;

40
funwin 发表于 2010-1-6 04:29:43
39# lwien007

初步测试了一下,可行。高手出招 迎刃而解! 佩服!
非常感谢你的帮忙!!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 00:55