BookmarkSubscribeRSS Feed
rinugour
Fluorite | Level 6

Under what circumstances would you code a select construct instead Of If Statements?

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

That's really up to you. 

For binary choices, then use ifc() or ifn() functions.

For simple if constructs with three or four branches then if statements.

For over 4 consider select.

 

There are no rules on this, if you prefer if statements, then use them.   

s_lassen
Meteorite | Level 14

Whenever I have a calculated construct that is reused in simple cases, e.g.

select(month(date3));
  when(12,1,2) season="Winter";
  when(3,4,5) season="Spring";
  

A very simple example, but with an IF statement you would have to repeat the month calculation, or insert an extra line to assign it to a variable (and one more line to drop the unneeded month variable). 

 

It can also be a nice way to throw an error if something unexpected shows up, just omit the OTHERWISE statement.

 

Kurt_Bremser
Super User

@rinugour wrote:

Under what circumstances would you code a select construct instead Of If Statements?


As soon as it makes the code more readable. If in doubt, put both versions side-by-side and see which one looks better to you.

A potential performance gain has to be determined through testing (in most cases, there won't be a noticeable difference).

Satish_Parida
Lapis Lazuli | Level 10
If more number of variables are dependent on some similar nested conditions, it is better to go with if rather than case.
anjita
Fluorite | Level 6

The select statement is used when you are using one condition to compare with several conditions like.

  1. Data exam;
  2. Set exam;
  3. select (pass);
  4. when Physics gt 60;
  5. when math gt 100;
  6. when English eq 50;
  7. otherwise fail;
  8. run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1734 views
  • 2 likes
  • 6 in conversation