How to Use SELECT-WHEN in SAS (With Example)


You can use a SELECT-WHEN statement in SAS to assign values to a new variable based on the values of an existing categorical variable in a dataset.

This statement uses the following basic syntax:

data new_data;
set my_data;
select (Existing_Column);
   when ('value1')    New_Column=1;
   when ('value2')    New_Column=2;
   when ('value3')    New_Column=3;
   otherwise          New_Column=4;
end;
run;

This syntax produces a new column called New_Column whose values are dependent on the values in Existing_Column.

The following example shows how to use a SELECT-WHEN statement in practice.

Example: SELECT-WHEN in SAS

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create dataset*/
data my_data;
    input team $ rating $ points;
    datalines;
Mavs Great 22
Mavs Good 29
Mavs OK 15
Mavs Bad 8
Spurs Good 30
Spurs OK 15
Spurs OK 20
Spurs Bad 7
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following SELECT-WHEN statement to create a new variable called Player_Status whose values depend on the value in the rating column:

/*create new dataset with Player_Status column*/
data new_data;
set my_data;
select (rating);
   when ('Great')    Player_Status=1;
   when ('Good')     Player_Status=2;
   when ('OK')       Player_Status=3;
   otherwise         Player_Status=4;
end;
run;

/*view new dataset*/
proc print data=new_data;

Here is how the values were generated in the new Player_Status column:

  • If rating was equal to “Great” then Player_Status was assigned 1.
  • If rating was equal to “Good” then Player_Status was assigned 2.
  • If rating was equal to “OK” then Player_Status was assigned 3.
  • If rating was not equal to any of the previously specified values then Player_Status was assigned 4.

Note: You can find the complete documentation for the SELECT statement in SAS here.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

The Complete Guide to DO Loops in SAS
How to Use IF-THEN-DO in SAS

Leave a Reply

Your email address will not be published.