Selecting Observations

By default, many SAS procedures process all the observations in a data set. You can subset observations for processing by adding a WHERE statement to your PROC step. The WHERE statement remains in effect only for the PROC step in which it appears.


General form, WHERE statement:
WHERE where-expression;

where where-expression specifies a condition for selecting observations. The where-expression can be any valid SAS expression.


For example, the WHERE statement in the PROC PRINT step below selects only observations where the value of Age is greater than 30.
     proc print data=clinic.admit;
        var age height weight fee;
        where age>30;
     run;

Specifying WHERE Expressions

A variable specified in the WHERE statement can be any variable in the SAS data set, not necessarily one of the variables specified in the PROC step. The WHERE statement works for both character and numeric variables. To specify a condition based on the value of a character variable, you must

  • enclose the value in quotes        
  • write the value with lower and uppercase letters exactly as it appears in the data set.

You use the following comparison operators to express a condition in the WHERE statement.

Symbol Meaning Example
= or eq equal to where name='Jones, C.';
^= or ne not equal to where temp ne 212;
> or gt greater than where income>20000;
< or lt less than where partno lt "BG05";
>= or ge greater than or equal to where id>='1543';
<= or le less than or equal to where pulse le 85;


Specifying Compound WHERE Expressions

You can also use WHERE statements to select a subset of observations based on multiple conditions. To link a sequence of expressions into compound expressions, you use logical operators, including the following:


Operator Meaning
AND ( & ) and, both. If both expressions are true, then the compound expression is true.
OR ( | ) or, either. If either expression is true, then the compound expression is true.


Examples of WHERE Statements
  • You can use compound expressions like these in your WHERE statements :
      where age<=55 and pulse>75;
      where area='A' or region='S'; 
      where empnum>1050 and state='NC';
  • When you test for multiple values of the same variable, you specify the variable name in each expression:
      where actlevel='LOW' or actlevel='MOD';
      where fee=124.80 or fee=178.20; 
  • You can use the IN operator as a convenient alternative:
      where actlevel in ('LOW','MOD'); 
      where fee in (124.80,178.20); 
  • To control the way compound expressions are evaluated, you can use parentheses:
      where (age<=55 and pulse>75) or area='A';
      where age<=55 and (pulse>75 or area='A');








Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement