I have the following dataset:
Test1 Test2 Test3 Test4 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1
Is there a way to create a new variable that will contain index "2" when "0" is in all variables Test* ? If there is "1" in at least one of the variables Test* the index in New_Var should be 0. Thank you in advance.
Desired output:
Test1 Test2 Test3 Test4 New_Var 0 0 0 0 2 0 0 0 0 2 1 0 1 0 0 0 0 0 0 2 0 1 0 1 0
You can check for this conditionally by summing all the variables in test1-test4
. Since they're named this way, you can use the sum(of var1-varn)
shortcut. If you use sum(of test1-test4)
, SAS automatically translates this to sum(test1, test2, test3, test4)
. This also works for arrays as well. For example, sum(of arr[*])
will sum all the variables in the array arr
.
data want;
set have;
if(sum(of test1-test4) = 0) then New_Var = 2;
else New_Var = 0;
run;
Test1 Test2 Test3 Test4 New_Var
0 0 0 0 2
0 0 0 0 2
1 0 1 0 0
0 0 0 0 2
0 1 0 1 0