Using IN operator in SAS

shreyansh

Member
Staff member
The IN operator in SAS is used to make a comparison between an individual value and a collection of values. It allows one to choose several values within a SAS dataset. It works as a substitute of several OR statements.

The IN operator's syntax in SAS is as follows:
variable IN (val1, val2, ..., valN)

Here a set of discrete values will be compared against the variable values - (val1, val2, ..., valN)

Using IN Operator in DATA STEP in SAS
The following code matches the values of MAKE against the dataset; it selects cars of these 3 makes 'Buick', 'Cadillac', 'Chevrolet' into a new dataset called "intellectsage_data".

data intellectsage_data;
set sashelp.cars;
where make in ('Buick', 'Cadillac', 'Chevrolet');
run;

To check that the code above correctly filtered the dataset, we can use PROC FREQ to check the unique values of MAKE column.
proc freq data=intellectsage_data;
table make;
run;

Output:
1744701203577.png


Using the NOT IN Operator in SAS
The NOT IN operator in SAS is used to narrow down the rows based on the condition that a column does not equal any one of the values. The code below returns all cars whose makers are not 'Audi', 'Acura', or 'BMW'.
data intellectsage_data;
set sashelp.cars;
where make not in ('Buick', 'Cadillac', 'Chevrolet');
run;
 
Back
Top