BookmarkSubscribeRSS Feed
Pooja98
Fluorite | Level 6
which statement is efficient between where and if statement??
4 REPLIES 4
ballardw
Super User

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.

mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
Why not make a dummy data to test them ?
WHERE could get faster if it is small data.
IF could get faster if it is big data.
andreas_lds
Jade | Level 19

@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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 670 views
  • 4 likes
  • 5 in conversation