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!
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;
that code does not look correct. I think you should use
if RXDDCI1A = 40 or RXDDCI2A = 40;
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!
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.