Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Using the SELECT Statement

The SELECT statement seems to be excluded from many SAS programming courses for some reason, so it's overlooked by a lot of people. It's the SAS implementation of what other languages call the "case" statement – a more efficient and elegant way of handling mutually exclusive possiblities than a long chain of IF/THEN/ELSEs.

There are two forms of SELECT statement. The more common selects according to the value of a variable:

data _null_;
  set sashelp.revhub2;
    when ('London', 'Frankfurt') put "Europe"; 
    when ('London') put "England"; 
    when ('New York', 'San Francisco') put "USA"; 
    when ('Tokyo') put "Asia"; 
    when ('Sydney') 
         put "Australia"; 
         put "The Land Down Under"; 

Notice that:

  • We can handle several values of hub at once e.g. 'New York', 'San Francisco' ;
  • For a given value of hub, SAS processes the first matching branch that it finds and then stops e.g. here 'London' is processed as "Europe", and SAS will never realise that it also matches the "England" branch;
  • "otherwise" is used to catch all unmatched values;
  • Each branch executes precisely one instruction. In practice, this is not really a restriction. The "otherwise" branch above executes a ";" to do nothing at all; the "Sydney" branch executes a "do" instruction, which can contain any amount of code.

The other form of SELECT branches according to conditions rather than the values of a variable:

data _null_;
  set sashelp.revhub2;
    when (type='Indirect') put 'More than one flight involved'; 
    when (hub='Sydney') put 'Kangaroos!'; 
    otherwise ; 

As before, SAS works through the list of WHEN conditions until it finds one that matches; executes the specified single instruction; and then stops.

It is good practice to restrict use of SELECT to cases when the possibilities are mutually exclusive, but with care it can be used in other circumstances too.