How to Use the BETWEEN Operator in SAS (With Examples)

The BETWEEN operator in SAS is a logical operator used to compare two values and check if a value lies between them. It can be used in WHERE and IF statements to filter data and decide which rows of a SAS dataset should be used for further processing. It is commonly used in the form of “variable BETWEEN a and b” to determine if the value of the variable is greater than or equal to a and less than or equal to b. This operator is also available in other programming languages such as SQL, Python, and R.


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 between 15 and 35;
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 print data=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 between 15 and 35;
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 between 15 and 35) 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:

x