1

I'm trying to recode a SAS array variable based on other variables condition. I am getting array subscript out of range error

      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

I need your help in tweaking do loop with incremental value.

      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;
1
  • All the arrays a single dimension. Commented May 14, 2019 at 1:57

2 Answers 2

1

You are flagging an assertion of a bitwise combinatoric. The BOR and BLSHIFT bitwise functions will aid you greatly.

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;
Sign up to request clarification or add additional context in comments.

Comments

1

below should work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.