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;

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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