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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 820 views
  • 0 likes
  • 3 in conversation