Help using Base SAS procedures

how to select observations with certain values

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

how to select observations with certain values

Hi,

Example input data:

Pat_ID     Pay_code

A1              1001

A2              1002

A3              1003

A3              4549

A4              5567

A5              1001

A6             

A7              XXXX

I need to only keep the entries say with pay_code being 1001, 1002, 1003 and 5567.  I tried to use this but it doesn't seem to work.

data pat_pay;

set pay_info;

if pay_code EQ '1001' or '1002' or '1003' or '5567';
run;


Accepted Solutions
Solution
‎01-26-2015 10:56 AM
Super User
Super User
Posts: 7,942

Re: how to select observations with certain values

Hi,

Replace the if with a Where, or use a then output:

data pat_pay;

     set pay_info;

     where pay_code in ('1001','1002');

run;

You can also put it on the set statement for some performance gain:

data pat_pay;

     set pay_info (where=(pay_code in ('1001','1002')));

run;

View solution in original post


All Replies
Solution
‎01-26-2015 10:56 AM
Super User
Super User
Posts: 7,942

Re: how to select observations with certain values

Hi,

Replace the if with a Where, or use a then output:

data pat_pay;

     set pay_info;

     where pay_code in ('1001','1002');

run;

You can also put it on the set statement for some performance gain:

data pat_pay;

     set pay_info (where=(pay_code in ('1001','1002')));

run;

Contributor
Posts: 28

Re: how to select observations with certain values

Hi,

Thanks!  I was wondernig how to output the observations with the rate codes wanted to one dataset and the remaining to another one using where in?

Contributor
Posts: 53

Re: how to select observations with certain values

The EQ operator can compare a value with a single value. The IN operator checks the current variable for a list of values, please use RW9 's solution.

Regular Contributor
Posts: 180

Re: how to select observations with certain values

I agree with RW9: using where instead of If when possible is better.


Anyway, the correct syntax of the if statement would be:


if pay_code EQ '1001' or pay_code EQ '1002' or pay_code EQ '1003' or pay_code EQ '5567';


CTorres

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 225 views
  • 5 likes
  • 4 in conversation