BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
_maldini_
Barite | Level 11

How can I create a subset comprised of observations that have certain values for certain variables? My inclination would be to use OR and WHERE statements like this (this syntax doesn't work. It's provided simply to communicate what I'm trying to do): 

DATA work.rx_meds_2009_2010;
	SET work.rxq_drug;

	where RXDDCI1A = 40;
	or where RXDDCI2A = 40;
run; 

My understanding is that the IN operator wouldn't work here because there are multiple variables, not just multiple values. IF statements also didn't seem to solve the problem correctly.

 

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

WHERE is statement. The statement starts with the WHERE keyword and ends with the semicolon after the condition.

OR is not a data step statement, so it cannot immediately follow a semicolon. If you need more than one condition, you have to combine the conditions in one statement.

Your single WHERE statement has to be

where RXDDCI1A = 40 or RXDDCI2A = 40;

View solution in original post

4 REPLIES 4
tarheel13
Rhodochrosite | Level 12

that code does not look correct. I think you should use 

if RXDDCI1A = 40 or  RXDDCI2A = 40;
Reeza
Super User

WHICHC/WHICHN functions are also an option. 

 

In would work in the opposite fashion, I think:

 

if 40 in (list of variables);

@_maldini_ wrote:

How can I create a subset comprised of observations that have certain values for certain variables? My inclination would be to use OR and WHERE statements like this (this syntax doesn't work. It's provided simply to communicate what I'm trying to do): 

DATA work.rx_meds_2009_2010;
	SET work.rxq_drug;

	where RXDDCI1A = 40;
	or where RXDDCI2A = 40;
run; 

My understanding is that the IN operator wouldn't work here because there are multiple variables, not just multiple values. IF statements also didn't seem to solve the problem correctly.

 

Thanks! 


 

Kurt_Bremser
Super User

WHERE is statement. The statement starts with the WHERE keyword and ends with the semicolon after the condition.

OR is not a data step statement, so it cannot immediately follow a semicolon. If you need more than one condition, you have to combine the conditions in one statement.

Your single WHERE statement has to be

where RXDDCI1A = 40 or RXDDCI2A = 40;
FreelanceReinh
Jade | Level 19

In the special case of one value and several variables the IN operator in conjunction with an array is convenient.

 

Example:

data have;
input id rxddci1-rxddci3 rxddci1a rxddci2a rxddci3a;
cards;
1 10 20 30 40 50 60
2 11 21 31 41 51 61
;

data want;
set have;
array rx[*] rxddci:;
if 40 in rx;
run;

The condition "40 in rx" would not work in a WHERE statement and also the array name in the condition cannot be replaced by a variable list of any kind.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 429 views
  • 6 likes
  • 5 in conversation