Table of Contents
The BETWEEN operator in SAS is a logical operator that allows for the selection of data within a specified range. It is used in conjunction with the WHERE statement in SAS to filter data based on certain criteria. The BETWEEN operator is written as “BETWEEN x AND y”, where x and y are the lower and upper limits of the range.
For example, if we have a dataset containing the sales data for a company and we want to only select the sales that fall between $500 and $1000, we can use the BETWEEN operator as follows:
data sales;
set company_sales;
where sales_amount between 500 and 1000;
run;
This will only select the sales data that falls within the specified range and exclude any data that does not meet this criteria.
In addition to numerical values, the BETWEEN operator can also be used for character strings and dates. For instance, if we have a dataset containing customer information and we want to select customers whose last names fall between “Kumar” and “Patel”, we can use the BETWEEN operator as follows:
data customers;
set customer_info;
where last_name between “Kumar” and “Patel”;
run;
This will only select customers with last names starting from Kumar up to Patel.
In summary, the BETWEEN operator in SAS is a useful tool for filtering data within a given range and can be used for both numerical and character values. It provides a convenient and efficient way to select specific data based on specified criteria.
Use the BETWEEN Operator in SAS (With Examples)
You can use the BETWEEN operator in SAS to select rows where column values are between two particular values.
Often the BETWEEN operator is used within a PROC SQL statement in the following manner:
proc sql; select * from my_data where points between15and35;quit;
This particular example selects all rows from the dataset called my_data where the value in the points column is between 15 and 35.
The following example shows how to use the BETWEEN operator in practice.
Example: How to Use BETWEEN Operator 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;
Cavs 12
Cavs 14
Warriors 15
Hawks 18
Mavs 31
Mavs 32
Mavs 35
Celtics 36
Celtics 40
;
run;
/*view dataset*/
proc printdata=my_data;

We can use the BETWEEN operator in PROC SQL to select only the rows where the value in the points column is between 15 and 35:
/*select all rows where value in points column is between 15 and 35*/ proc sql; select * from my_data where points between15and35;quit;

Notice that the only rows returned are the ones where the value in the points column is between 15 and 35.
Also note that you can use the BETWEEN operator with additional conditions in the WHERE statement.
For example, you can use the following syntax to only return rows where the value in the points column is between 15 and 35 and the value in the team column is ‘Mavs’:
/*select rows where points is between 15 and 35 and team is Mavs*/ proc sql; select * from my_data where (points between15and35) and team='Mavs';quit;

Note: You can find the complete documentation for the BETWEEN operator in SAS .
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I use the BETWEEN operator in SAS, and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-between-operator-in-sas-and-what-are-some-examples-of-its-usage/
stats writer. "How can I use the BETWEEN operator in SAS, and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-between-operator-in-sas-and-what-are-some-examples-of-its-usage/.
stats writer. "How can I use the BETWEEN operator in SAS, and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-between-operator-in-sas-and-what-are-some-examples-of-its-usage/.
stats writer (2024) 'How can I use the BETWEEN operator in SAS, and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-between-operator-in-sas-and-what-are-some-examples-of-its-usage/.
[1] stats writer, "How can I use the BETWEEN operator in SAS, and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the BETWEEN operator in SAS, and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Comments are closed.