BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hariharan1409
Calcite | Level 5

While learning sas tutorial from PSU. @https://online.stat.psu.edu/stat480/lesson/welcome-stat-480.

I Came across these sentences:

Note that, in general, using ELSE statements with IF-THEN statements can save resources:

Using IF-THEN statements without the ELSE statement causes SAS to evaluate all IF-THEN statements. Using IF-THEN statements with the ELSE statement causes SAS to execute IF-THEN statements until it encounters the first true statement. Subsequent IF-THEN statements are not evaluated.

can someone explain me the above lesson with an example. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Suppose you have a variable x, which holds numerical values.

Both of these codes will yield the same result:

if x = 1 then y = 'A';
if x = 2 then y = 'B';
if x = 3 then y = 'C';
if x = 1 then y = 'A';
else if x = 2 then y = 'B';
else if x = 3 then y = 'C';

In the first code, all conditions will always be evaluated; in the second, if x equals 1, the second and third condition will not be evaluated, speeding up execution.

By evaluating for the most frequent value first, and the least frequent last, you can further optimize your code.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Suppose you have a variable x, which holds numerical values.

Both of these codes will yield the same result:

if x = 1 then y = 'A';
if x = 2 then y = 'B';
if x = 3 then y = 'C';
if x = 1 then y = 'A';
else if x = 2 then y = 'B';
else if x = 3 then y = 'C';

In the first code, all conditions will always be evaluated; in the second, if x equals 1, the second and third condition will not be evaluated, speeding up execution.

By evaluating for the most frequent value first, and the least frequent last, you can further optimize your code.

Reeza
Super User

This means that if you write your IF/THEN/ELSE statements in the order of what you expect to happen most frequently it will be faster than if you ordered them in some other order. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 671 views
  • 0 likes
  • 3 in conversation