0

我正在尝试保存一个名为ezproduct 的向量,该向量基于名为ID_bloque. 对于每个ID_bloque值,我的代码计算一个称为向量ez并将其附加到具有相同名称的矩阵中。但是,如果zk是缺失的数据向量(这不是错误),代码将停止并且不进行其他计算,这对其他组来说是个问题,因为没有为他们进行任何计算。有没有办法强制append条款保留这些数据以及其他组的结果?太感谢了

proc iml;
/*Maximo= maximum ID_bloque value*/
do i=1 to %EVAL(&MAXIMO);
     use T6; /*Dataset*/
     /*Variables*/
     read all var{E1S1 E1S2 E2S1 E2S2 E3S1 E3S2 E4S1 E4S2} 
                                     into XK where (ID_bloque=i) ;
     read all var{FEX_P} into dk where (ID_bloque=i) ;
     read all var{VK} into vk where (ID_bloque=i) ;
     read all var{z} into zk where (ID_bloque=i) ;
     /* Matrix computations */
     MAT=J(NCOL(XK),NCOL(XK),0);
     do j=1 to Nrow(XK);
          MAT= MAT + dk[j]*vk[j]*((XK[j,]`)*XK[j,]);
     end;
     /* ez values depending on missing information in zk*/
     if all(zk)=. then do;
          ez=zk;
     end;
     else do;
          Brz=(Ginv(MAT))*((XK`)*(dk#vk#zk));
          ez=zk-XK*Brz;
     end;
     /* Vectors appending (error source) */
     if i=1 then do;
          create ez var{ez}; APPEND;
     end;else do; APPEND var{ez} ;end;
end;
close ez;
quit;
4

1 回答 1

0

要检查向量是否全部丢失,您应该使用 if all(zk = .) then...

在循环中处理追加到数据集的最佳方法是在循环之前打开数据集,在循环内追加,然后在循环之后关闭。SAS需要知道要写入哪些数据类型和长度,所以我通常在打开数据集时使用假数据(例如缺失值)只是为了提供变量类型。

最后,您正在执行的 MAT 计算是矩阵乘法,因此您可以简化该计算:

proc iml;
/*Maximo= maximum ID_bloque value*/
ez = .;             /* tell IML that ez is a numeric vector */
create ez var {ez}; /* open the data set */

do i=1 to %EVAL(&MAXIMO);
     use T6; /*Dataset*/
     /*Variables*/
     read all var{E1S1 E1S2 E2S1 E2S2 E3S1 E3S2 E4S1 E4S2} 
                                     into XK where (ID_bloque=i) ;
     read all var{FEX_P} into dk where (ID_bloque=i) ;
     read all var{VK} into vk where (ID_bloque=i) ;
     read all var{z} into zk where (ID_bloque=i) ;
     /* Matrix computations */
     /*
     MAT=J(NCOL(XK),NCOL(XK),0);
     do j=1 to Nrow(XK);
          MAT= MAT + dk[j]*vk[j]*((XK[j,]`)*XK[j,]);
     end;
     */
     MAT = (dk#vk#XK)` * XK;
     /* ez values depending on missing information in zk*/
     if all(zk=.) then do;
          ez=repeat(1, Nrow(XK));
     end;
     else do;
          Brz=(Ginv(MAT))*((XK`)*(dk#vk#zk));
          ez=zk-XK*Brz;
     end;
     /* Vectors appending (error source) */
     append;
end;
close ez;
quit;
于 2019-04-13T12:10:55.723 回答