DATA Step, Macro, Functions and more

subsetting a dataset where multiple variables equal the same number

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

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
2 weeks ago
Super User
Posts: 17,928

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;

 

 

View solution in original post


All Replies
Solution
2 weeks ago
Super User
Posts: 17,928

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;

 

 

PROC Star
Posts: 172

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

@BenBradyIf you wanna play:

 

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 109 views
  • 3 likes
  • 3 in conversation