Study guide
4.3 Conditional Processing: IF-THEN/ELSE Statements & WHERE Subsetting
SAS Certified Specialist: Base Programming Using SAS 9.4 A00-231
Track your progress as you read
Finished sections get a check mark.
1 Chapter 1: SAS Base Programming Exam Overview
1.1 A00-231 Exam Format, Scoring & Test-Day Logistics 1.2 Official Content Areas, Weights & Study Plan
2 Chapter 2: Accessing & Creating SAS Data Structures
2.1 SAS Environment, Syntax Rules, & Data Step Basics 2.2 SAS Data Libraries & the LIBNAME Statement 2.3 Investigating Libraries with PROC CONTENTS & PROC DATASETS 2.4 Importing Delimited & Excel Data with PROC IMPORT 2.5 Raw Data Ingestion with INFILE and INPUT Statements
3 Chapter 3: Managing & Combining SAS Data Sets
3.1 Reading SAS Data Sets with the SET Statement 3.2 Combining Data Sets: Concatenating & Interleaving 3.3 Match-Merging with BY Statements & FIRST./LAST. Processing 3.4 Controlling Data Output & Variable Selection (KEEP, DROP, RENAME, OBS=)
4 Chapter 4: Data Transformation & Conditional Logic
4.1 Creating & Modifying Variables with Assignment Statements 4.2 Modifying Variable Attributes with LENGTH, LABEL, FORMAT & RENAME= 4.3 Conditional Processing: IF-THEN/ELSE Statements & WHERE Subsetting 4.4 Accumulating Totals & Subtotals (RETAIN & Sum Statement) 4.5 Processing Data Iteratively with DO Loops
5 Chapter 5: SAS Functions & Data Type Conversions
5.1 Character Functions for String Manipulation 5.2 Numeric & Mathematical Functions 5.3 Working with SAS Dates, Times, & Intervals 5.4 Reading & Displaying Dates with Informats and Formats 5.5 Explicit & Implicit Data Type Conversions (INPUT & PUT)
6 Chapter 6: Restructuring Data & Program Maintenance
6.1 SAS Array Processing for Multi-Variable Transformations 6.2 Sorting Data Sets with PROC SORT 6.3 Restructuring Data Sets with PROC TRANSPOSE 6.4 Creating & Applying Custom Formats with PROC FORMAT 6.5 Simplifying Programs with Macro Variables (%LET)
7 Chapter 7: Error Handling & Debugging SAS Programs
7.1 SAS Log Analysis & Diagnostic Messages 7.2 Syntax vs. Logic Errors & Phasing 7.3 Debugging Techniques with _ERROR_, _N_, & PUT Statements 7.4 Identifying Logic Errors with the PUTLOG Statement 7.5 Handling Missing Values & Preventing Calculation Propagations
8 Chapter 8: Summary & Detail Data Reporting
8.1 Detail Reporting with PROC PRINT 8.2 Descriptive Statistics with PROC MEANS & PROC SUMMARY 8.3 Frequency & Categorical Analysis with PROC FREQ 8.4 Screening Extreme & Missing Values with PROC UNIVARIATE 8.5 Custom Tabular Reports with PROC REPORT & PROC TABULATE
9 Chapter 9: ODS Output Delivery & Data Export
9.1 Output Delivery System (ODS) Architecture Basics 9.2 Generating PDF, HTML, & RTF Output Destinations 9.3 Exporting Reports to Excel with ODS EXCEL & XLSX 9.4 Generating CSV, PowerPoint & Other ODS Destinations 9.5 Customizing ODS Titles, Footnotes, & Global Options 9.6 Exporting SAS Data Sets with PROC EXPORT & the XLSX Engine
4.3 Conditional Processing: IF-THEN/ELSE Statements & WHERE Subsetting
Key Takeaways Explain This Topic AI
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:
Operation Mnemonic Operator Symbolic Operator Example
Equal to EQ = if Age eq 18 then ...
Not equal to NE ^= or ~= if Status ne 'Closed' then ...
Greater than GT > if Sales gt 10000 then ...
Less than LT < if Margin lt 0.05 then ...
Greater than or equal GE >= if Score ge 70 then ...
Less than or equal LE <= if Tenure le 5 then ...
Value list matching IN IN if 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 / Capability Subsetting IF Statement WHERE Statement
- Valid Step Types DATA steps ONLY DATA steps AND PROC steps
- Execution Phase DATA 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 ? YES NO (Requires an existing SAS dataset)
- Works with FIRST. and LAST. variables? YES NO
- Indexing & Engine Optimization Cannot use dataset indexes Leverages SAS dataset indexes for fast access
Multiple Statements Syntax Last IF overrides or combines logic Multiple 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;