Hi,
I am writing an array for splitting the data set based on their values of several variables, like I have a data set which has 20 variables and their values are 0 and 1 and now I want to split the data set in two, one data set which has at least one or more value of 1 in any of the 20 variables for example
ID | D_1 | D_2 | D_3 | D_4 | D_5 | D_6 | D_7 | D_8 | D_9 | D_10 | D_11 | D_12 | D_13 | D_14 | D_15 | D_16 | D_17 | D_18 | D_19 | D_20 |
807469 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
and second one which has value having 0 across all 20 variables for example
ID | D_1 | D_2 | D_3 | D_4 | D_5 | D_6 | D_7 | D_8 | D_9 | D_10 | D_11 | D_12 | D_13 | D_14 | D_15 | D_16 | D_17 | D_18 | D_19 | D_20 |
807569 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
I am trying this method but it not giving the desired result
.
Data data1 rest;
set data;
array D {*} D_1-D_20;
do I = 1 to 20;
if D(I) > 0 then output data1;
else output rest;
end;
run;
Or
Data data1 rest;
set data;
array D {*} D_1-D_20;
if 1 in D then output data1;
else output rest;
run;
You can do something like below. I simply made up some data
data data;
array D {*} D_1-D_20;
do i=1 to 20;
D[i]=0;
end;
output;
D[1]=1;
output;
run;
Data data1 rest;
set data;
array D {*} D_1-D_20;
if sum(of D[*]) > 0 then output data1;
else output rest;
run;
Or
Data data1 rest;
set data;
array D {*} D_1-D_20;
if 1 in D then output data1;
else output rest;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.