在SAS中重新编码多维数组变量

时间:2019-05-14 00:46:41

标签: multidimensional-array sas do-loops

我正在尝试根据其他变量条件重新编码SAS数组变量。我收到数组下标超出范围错误

      datasets Chk1 and Chk2 have the following data

      Chk1
       id Var1 Var2
       1   Y    N
       2   N    Y
       3   Y    Y
      Chk2
      id Var3 Var4
       1   N    Y
       2   Y    N
       3   Y    Y

      My desired output is
      Chk3
      id  Var1 Var2 Var3 Var4 a1 a2 a3 b1 b2 b3
       1    Y   N    N    Y    1           1 
       2    N   Y    Y    N       1     1
       3    Y   Y    Y    Y          1        1


      Here is the data merge with recoding variables

在调整带有增量值的do循环时,我需要您的帮助。

      data Chk3;
       merge Chk1(in=a) Chk2(in=b);
       by id;
       if a and b;
       array wv(*) var1 var2;
       array wv2(*) var3 var4;
       array wv3(*) a1 a2 a3 b1 b2 b3;
       do i=1 to dim(wv3) by 3;
        if wv(i)='Y' and wv2(i)='N' then wv3(i)=1;
        if wv(i)='N' and wv2(i)='Y' then wv3(i+1)=1;
        if wv(i)=wv2(i) then wv3(i+2)=1;
       end;
       run;

2 个答案:

答案 0 :(得分:0)

下面应该可以工作:

data Chk3;
    merge Chk1(in=a) Chk2(in=b);
    by id;

    if a and b;
    array wv(*) var1 var2;
    array wv2(*) var3 var4;
    array wv3(*) a1 a2 a3 b1 b2 b3;

    do i=1 to dim(wv2);

        if wv(i)='Y' and wv2(i)='N' then
            wv3((i-1)*3+1)=1;

        if wv(i)='N' and wv2(i)='Y' then
            wv3((i-1)*3+2)=1;

        if wv(i)=wv2(i) then
            wv3((i-1)*3+3)=1;
    end;
run;



Obs id var1 var2 var3 var4 a1 a2 a3 b1 b2 b3 i
1 1 Y N N Y 1 . . . 1 . 3
2 2 N Y Y N . 1 . 1 . . 3
3 3 Y Y Y Y . . 1 . . 1 3

答案 1 :(得分:0)

您正在标记一个按位组合的断言。 BORBLSHIFT按位函数将极大地帮助您。

data have1; input
id Var1 $ Var2 $; datalines;
1   Y    N
2   N    Y
3   Y    Y
data have2; input
id Var3 $ Var4 $; datalines;
 1   N    Y
 2   Y    N
 3   Y    Y
run;

data want;
  merge have1 have2;
  by id;

  array have1bitCombo a1-a3;
  array have2bitCombo b1-b3;

  have1bitCombo[bor(var1='Y', blshift(var2='Y',1))] = 1;
  have2bitCombo[bor(var3='Y', blshift(var4='Y',1))] = 1;
run;