SAS: How to Use the IN Operator

Deepanshu Bhalla Add Comment

The IN operator in SAS is used to compare a value against a list of values. It allows you to select multiple values from a SAS dataset. It is an alternative of multiple OR conditions.

The syntax for using the IN operator in SAS is as follows:

variable IN (value1, value2, ..., valueN)

Here values of the variable will be compared against the list of values - (value1, value2, ..., valueN)

How to use IN Operator in DATA STEP in SAS

The following code filters the dataset based on the values of the MAKE variable. It selects cars of these 3 brands 'Audi', 'Acura', 'BMW' and puts them into a new dataset named "newdata".

data newdata;
set sashelp.cars;
where make in('Audi', 'Acura', 'BMW');
run;

To check if the above code filters dataset correctly, we can use the PROC FREQ procedure to see the unique values of the column MAKE.

proc freq data=newdata;
table make;
run;
How to Use the IN Operator in SAS

How to use IN Operator in PROC SQL in SAS

proc sql;
select *
from sashelp.cars
where make in('Audi', 'Acura', 'BMW');
quit;

How to use NOT IN Operator in SAS

The NOT IN operator is used in SAS to filter rows based on the condition that a column does not match any of the values. The following code returns all cars that have makers not included in the list - 'Audi', 'Acura', 'BMW'.

proc sql;
select *
from sashelp.cars
where make not in('Audi', 'Acura', 'BMW');
quit;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS: How to Use the IN Operator"
Next → ← Prev