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 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;
Share Share Tweet