Define "is efficient".
IF statements can check conditions that WHERE can't. Variables used in a Where expression have to exist in a source data set.
WHERE can use some operations that IF can't. Where is usable in most procedure but IF isn't.
In addition to @ballardw 's comment, there is another important difference between WHERE and IF - namely that where will outsource the filtering to the data engine, while if only filters once the data observation has been passed to the data step. This data engine might be the sas engine for various types of sas datasets, or a database for which sas/access has an engine (see SAS/ACCESS Software Access Engines).
The fact that where-filtering is outsourced is not only why you can use where in your proc freq's, proc reg's, proc sort's, etc., but it also can make the where statement (compared to a subsetting if) a more efficient filter in a data step.
@Pooja98 wrote:
which statement is efficient between where and if statement??
You have to use where instead of subsetting-if, whenever you are using by-group-processing or end=somevar in the set statement. Execute the following steps and have a look at the log:
data girls1;
set sashelp.class end=lastObs;
where Sex = 'F';
if lastObs then put 'Just read ' _n_ 'obs.';
run;
data girls2;
set sashelp.class end=lastObs;
if Sex = 'F';
if lastObs then put 'Just read ' _n_ 'obs.';
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.