楼主: SAS新手
2919 13

如何建立回归方程??替换缺失值!!! [推广有奖]

11
SAS新手 发表于 2013-12-12 14:17:11
jingju11 发表于 2013-12-11 11:32
what does it mean?
对不起,这个说的有点乱!我重新回复了个贴。

12
jingju11 发表于 2013-12-13 23:46:29
  1. %macro impute(VarList);       
  2. data _null_;
  3.         call symputx('nobs', nobs);
  4.         if 0 then set have nobs =nobs;
  5.         stop;
  6.         run;
  7.         %do i =1 %to %sysfunc(countw(&varList));
  8.                 %let mvar =%scan(&varList, &i);
  9.                 data have2;
  10.                         do _n_=1 by 1 until(last.&mvar.);
  11.                                 set have(keep=&mvar);
  12.                                 by &mvar. notsorted;
  13.                                 imp ++(&mvar. =0);
  14.                                 end;
  15.                         do _n_=1 by 1 until(last.&mvar.);
  16.                                 set have(keep=&mvar);
  17.                                 by &mvar. notsorted;
  18.                                 &mvar.i =(imp >3);
  19.                                 output;
  20.                                 end;
  21.                         imp =0;
  22.                         keep &mvar. &mvar.i;
  23.                         run;

  24.                 data have3;
  25.                         call streaminit(1234);
  26.                         set have2 end=eof;
  27.                         array xx[&nobs.,2] _temporary_;
  28.                   xx[_n_,1] =&mvar.;
  29.                         xx[_n_,2] =&mvar.i;
  30.                   if eof then do;
  31.                     if xx[1,1] =0 then do;
  32.                                         %*Fetch 1st non-missing value;
  33.                       do i =1 to dim1(xx) by 1;
  34.                         if xx[i,1] ^=0 then leave;
  35.                         end;
  36.                       xx[1,1] =xx[i,1];
  37.                       end;
  38.                     if xx[&nobs.,1] =0 then do;
  39.                                         %*Fetch last non-missing value;
  40.                       do i =dim1(xx) to 1 by -1;
  41.                         if xx[i,1] ^=0 then leave;
  42.                         end;
  43.                       xx[&nobs.,1] =xx[i,1];
  44.                       end;
  45.                           do i =1 to dim1(xx);
  46.                             if xx[i,1] ^=0 then PreMissing =0;
  47.                             if xx[i,1] =0 then do;
  48.                               if PreMissing =0 then do;
  49.                                       PreMissing =1;
  50.                                       xL=xx[i-1,1];
  51.                                                         %*Fetch next non-missing value;
  52.                                       do j =i+1 to dim(xx);
  53.                                         if xx[j,1] ^=0 then leave;
  54.                                         end;
  55.                                       xR =xx[j,1];
  56.                                       xx[i,1] =ifn(rand('table', 0.5) =1, xL, xR);
  57.                                       end;
  58.                                                 %*impute missing when previous one imputed;
  59.                               if PreMissing =1 then xx[i,1] =ifn(rand('table', 0.5) =1, xL, xR);
  60.                               end;
  61.                                         %*Wont impute those >3 consecutive missings;
  62.                             &mvar._imp =ifn(xx[i,2]=1, 0, xx[i,1]);
  63.                             output;
  64.                             end;   
  65.                                 end;
  66.                         keep &mvar._imp;
  67.                         run;
  68.                 %if &i =1 %then%do;
  69.                         data have_f;
  70.                                 merge have;
  71.                                 run;
  72.                         %end;
  73.                 data have_f;
  74.                         merge have_f have3;
  75.                         run;
  76.         %end;
  77. %mend impute;


  78. %impute(A1 A2 A3 B1 B2 B3 C1 C2 C3 D1 D2 D3 E1 E2 E3)
  79. ;
复制代码

13
SAS新手 发表于 2013-12-16 15:00:28
jingju11 发表于 2013-12-13 23:46
太厉害了~~~谢谢!大侠,交个朋友吧!我Q:869470467

14
heperwong 在职认证  发表于 2013-12-16 15:56:18
make a mark.

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

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