Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause? 2

Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?

The SAS IN operator is a useful tool in PROC SQL for simplifying WHERE clauses. This operator allows for multiple values to be specified in a single statement, eliminating the need for multiple OR conditions. By using the IN operator, the code becomes more concise and easier to read, improving efficiency and reducing the chances of errors. This feature is especially beneficial when dealing with large datasets or complex queries. Overall, the use of the SAS IN operator in PROC SQL can greatly simplify the WHERE clause and enhance the overall functionality of the code.

SAS: Use the IN Operator in PROC SQL


You can use the IN operator in the PROC SQL statement in SAS to only return rows where a variable in a dataset contains a value in a list.

The following example shows how to use the IN operator in practice.

Example: Using IN Operator in PROC SQL in SAS

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create dataset*/
data my_data;
    input team $ points;
    datalines;
A 12
A 14
A 15
A 18
B 31
B 32
C 35
C 36
C 40
D 28
E 20
E 21
;
run;

/*view dataset*/
proc printdata=my_data;

We can use the IN operator in PROC SQL to select only the rows where the team is equal to A, B, or E:

/*select all rows where team is A, B, or E*/
proc sql;
   select *
   from my_data
   where team in ('A', 'B', 'E');
quit;

Notice that only the rows where the team is equal to A, B, or E are returned.

The opposite of the IN operator in PROC SQL is NOT IN, which selects rows where some variable in a dataset does not contain a value in a list.

The following code shows how to use the NOT IN operator to select all rows where the team is not equal to A, B, or E:

/*select all rows where team is not A, B, or E*/
proc sql;
   select *
   from my_data
   where team not in ('A', 'B', 'E');
quit;

Notice that only the rows where the team is not equal to A, B, or E are returned.

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/can-i-use-the-sas-in-operator-in-proc-sql-to-simplify-my-where-clause/

stats writer. "Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/can-i-use-the-sas-in-operator-in-proc-sql-to-simplify-my-where-clause/.

stats writer. "Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/can-i-use-the-sas-in-operator-in-proc-sql-to-simplify-my-where-clause/.

stats writer (2024) 'Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/can-i-use-the-sas-in-operator-in-proc-sql-to-simplify-my-where-clause/.

[1] stats writer, "Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. Can I use the SAS IN operator in PROC SQL to simplify my WHERE clause?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top