0

I have a group of variables , say b1-b30 and i need to test same condition on same variables among themselves like, if b1='a' or b2='a' or b3='a'.. and so on. is there any solution in SAS macros or using of sas arrays to do this task to avoid writing this explicitly. sample code is like this:

data test;
    input d1 d2 d3 d4 d5 ;
cards;
1 2 1 1 0
2 3 1 0 0 
0 0 0 1 0
0 2 1 0 2
0 4 0 2 2
0 0 0 0 3
;
run;

data want;
    set test;
    if d1=1 or d2=1 or d3=1 or d4=1 then flag=1;
    else flag=0;
run;

so i have around 50 variable , to test same condition and flag it .

ved_null_
  • 71
  • 2
  • 10

2 Answers2

2

You could use WHICHN() or WHICHC() functions.

flag1 = not 0=whichn(1,of d1-d4);
Tom
  • 47,574
  • 2
  • 16
  • 29
1

The IN operator with array-name will work but you would need to make an array for each variable group. Using WHICHN would be easier.

array d[5];
if 1 in d then flag2=1; else flag2=0;  
data _null_
  • 8,534
  • 12
  • 14