4.3 Conditional Processing: IF-THEN/ELSE Statements & WHERE Subsetting

Key Takeaways

  • IF-THEN/ELSE statements execute conditionally during the DATA step execution phase for each observation loaded into the Program Data Vector (PDV).
  • DO groups (IF condition THEN DO; ... END;) permit multiple SAS statements to execute conditionally under a single evaluated condition.
  • The WHERE statement filters observations during the compilation/input phase before they enter the PDV, significantly reducing I/O and processing overhead.
  • WHERE statements are valid in both DATA steps and PROC steps, whereas IF-THEN statements are executable statements restricted exclusively to DATA steps.
  • WHERE processing cannot evaluate newly created DATA step variables or raw data read via INPUT statements because filtering occurs prior to execution.
Last updated: August 2026

4.3 Conditional Processing: IF-THEN/ELSE Statements & WHERE Subsetting

Quick Answer: Conditional processing allows SAS programs to execute specific statements or filter observations based on whether a logical expression evaluates to true (non-zero and non-missing) or false (zero or missing). SAS provides two major mechanisms for conditional handling: IF-THEN/ELSE statements (executable statements used inside DATA steps) and WHERE statements (subsetting directives valid in both DATA and PROC steps).


IF-THEN / ELSE Statement Mechanics

An IF-THEN statement evaluates a logical condition for the current observation in the Program Data Vector (PDV). If the condition is true, SAS executes the statement following THEN. If false, SAS skips the THEN clause and proceeds to the next statement.

Basic Syntax and Comparison Operators

if condition then statement;
else if condition then statement;
else statement;

SAS supports both symbolic and mnemonic comparison operators:

OperationMnemonic OperatorSymbolic OperatorExample
Equal toEQ=if Age eq 18 then ...
Not equal toNE^= or ~=if Status ne 'Closed' then ...
Greater thanGT>if Sales gt 10000 then ...
Less thanLT<if Margin lt 0.05 then ...
Greater than or equalGE>=if Score ge 70 then ...
Less than or equalLE<=if Tenure le 5 then ...
Value list matchingININif State in ('NC', 'SC', 'VA') then ...

Exam Trap: <> is not a not-equal operator in SAS. <> is the MAX operator (a <> b returns the larger of the two values) and >< is the MIN operator. The valid not-equal operators are NE, ^=, and ~=. Writing if Status <> 'Closed' therefore compares nothing — it evaluates the larger of the two operands, which is almost never what the programmer intended.

Mutually Exclusive Logic with ELSE IF

Using ELSE IF creates efficient, mutually exclusive decision trees. When an IF condition succeeds, SAS skips all remaining ELSE IF and ELSE clauses for that observation.

/* EFFICIENT: SAS stops checking conditions once a match is found */
if CreditScore >= 750 then Rating = 'Excellent';
else if CreditScore >= 700 then Rating = 'Good';
else if CreditScore >= 600 then Rating = 'Fair';
else Rating = 'Poor';

/* INEFFICIENT & BUG-PRONE: SAS evaluates ALL four IF statements for EVERY row */
if CreditScore >= 750 then Rating = 'Excellent';
if CreditScore >= 700 then Rating = 'Good';
if CreditScore >= 600 then Rating = 'Fair';
if CreditScore < 600 then Rating = 'Poor';

In the inefficient example, an observation with CreditScore = 780 first sets Rating = 'Excellent', then gets overwritten by Rating = 'Good', and finally gets overwritten by Rating = 'Fair'! Using ELSE IF prevents overwrites and reduces CPU execution time.


Conditional Execution of Multiple Statements: DO Groups

By default, a THEN or ELSE clause executes only a single SAS statement. To execute multiple statements conditionally, enclose them in a DO ... END block:

data work.bonus_processing;
   set sashelp.empdata;
   
   if Sales > 100000 then do;
      Bonus = Sales * 0.10;
      Tier = 'Top Performer';
      ExecutiveReview = 'Approved';
   end;
   else do;
      Bonus = Sales * 0.02;
      Tier = 'Standard';
      ExecutiveReview = 'Not Required';
   end;
run;

Subsetting IF vs. WHERE Statements

Subsetting is the process of selecting a specific subset of observations from an input dataset. SAS offers two ways to subset data in a DATA step: the subsetting IF statement (if condition;) and the WHERE statement (where condition;).

While they often produce identical output datasets, their underlying operational mechanics in the SAS engine are vastly different.

Subsetting IF Statement (if condition;)

  • Operates during the execution phase.
  • Reads the row from the input dataset into the PDV first, computes any new variables, and then evaluates the condition.
  • If the condition is false, SAS immediately stops processing the current observation, discards it from the PDV, and returns to the top of the DATA step loop.

WHERE Statement (where condition;)

  • Operates during the compilation/input phase.
  • Acts as an index/input filter at the data engine level before observations are brought into the PDV.
  • If an observation does not meet the WHERE condition, it is never read into memory, saving I/O and processing time.
  • Can be used in PROC steps (e.g., proc print data=sashelp.class; where Age > 12; run;).

Detailed Comparison: IF vs. WHERE

Feature / CapabilitySubsetting IF StatementWHERE Statement
Valid Step TypesDATA steps ONLYDATA steps AND PROC steps
Execution PhaseDATA step execution phase (in PDV)Input engine / pre-PDV compilation phase
Can evaluate newly calculated variables?YES (Total = Q1+Q2; if Total > 100;)NO (Causes Variable not on file error)
Can filter raw data read with INPUT?YESNO (Requires an existing SAS dataset)
Works with FIRST. and LAST. variables?YESNO
Indexing & Engine OptimizationCannot use dataset indexesLeverages SAS dataset indexes for fast access
Multiple Statements SyntaxLast IF overrides or combines logicMultiple WHERE statements override unless WHERE SAME-AND is used

Special WHERE Operators

The WHERE statement supports specialized operators not available in standard IF statements:

/* Range checking */
where Salary between 50000 and 80000;

/* Substring matching */
where LastName contains 'SON';

/* Pattern matching with wildcard (% for multiple chars, _ for single char) */
where Phone like '919-%';

/* Missing value test */
where Commission is missing; /* Or IS NULL */

/* Combining multiple WHERE conditions incrementally */
where State = 'NC';
where same-and Sales > 5000; /* Combines as State='NC' AND Sales>5000 */

Code Demonstration: Subsetting IF vs. WHERE

/* EXAMPLE 1: Valid WHERE statement in PROC PRINT */
proc print data=sashelp.shoes;
   where Region = 'Canada' and Sales > 10000;
run;

/* EXAMPLE 2: Valid Subsetting IF evaluating a newly created variable */
data work.heavy_returns;
   set sashelp.shoes;
   NetSales = Sales - Returns;
   /* MUST use IF here because NetSales does not exist in input dataset sashelp.shoes */
   if NetSales < 1000;
run;

/* EXAMPLE 3: INVALID WHERE statement attempt (WILL ERROR) */
data work.invalid_where;
   set sashelp.shoes;
   NetSales = Sales - Returns;
   /* ERROR: Variable NetSales is not on file SASHELP.SHOES */
   where NetSales < 1000;
run;
Test Your Knowledge

Which statement correctly describes a major operational distinction between a subsetting IF statement and a WHERE statement in SAS?

A
B
C
D
Test Your Knowledge

A developer writes the following SAS DATA step:

data work.high_sales;
   set sashelp.shoes;
   TotalSales = Sales + Returns;
   where TotalSales > 50000;
run;
What happens when this program is submitted?

A
B
C
D
Test Your Knowledge

Consider the following SAS DATA step:

data work.tiers;
   set sashelp.class;
   if Age < 13 then Tier = 'Junior';
   if Age >= 13 then Tier = 'Teen';
   if Age > 15 then Tier = 'Senior';
run;
If an observation has Age = 16, what is the final value of Tier and how many IF conditions were evaluated for this observation?

A
B
C
D
Test Your Knowledge

Which WHERE statement clause correctly selects observations where the character variable State matches 'NC', 'SC', or 'VA'?

A
B
C
D