Solved
Contributor
Posts: 23

subsetting a dataset where multiple variables equal the same number

i want to subset my dataset by where multiple variables are equal to the number 1. I have been doing this by using the following code:

data want;

set have;

where group=1 or fish-=1 or net=1 or food=1;

run;

I was wandering if there was a shorter way to write this, as it would be much longer if there were a lot of variables. i.e something along the lines of writing all the variables out and only having to write one '=1' in the code.

Cheers

Accepted Solutions
Solution
‎08-10-2017 09:15 PM
Super User
Posts: 24,012

Re: subsetting a dataset where multiple variables equal the same number

WHICHN()

``where whichn(1, var1, var2, var3, ... );``

If you can use IF instead variable lists can be used:

``if whichn(1, of var:)>0;``

All Replies
Solution
‎08-10-2017 09:15 PM
Super User
Posts: 24,012

Re: subsetting a dataset where multiple variables equal the same number

WHICHN()

``where whichn(1, var1, var2, var3, ... );``

If you can use IF instead variable lists can be used:

``if whichn(1, of var:)>0;``

Super User
Posts: 2,061

Re: subsetting a dataset where multiple variables equal the same number

data have;

a=1;

b=0;

c=0;

output;

a=0;

b=0;

c=0;

output;

run;

data want;

set have;

array n _numeric_;

if 1 in n;

run;

☑ This topic is solved.