How to 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 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;

Only the rows where the value in the points column is between 15 and 35 and the value in the team column is ‘Mavs’ are returned.

Note: You can find the complete documentation for the BETWEEN operator in SAS here.

Additional Resources

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

How to Use the NOT EQUAL Operator in SAS
How to Use CONTAINS Operator in SAS
How to Use a “NOT IN” Operator in SAS

Leave a Reply

Your email address will not be published. Required fields are marked *