How can the SAS SELECT-WHEN statement be used in a SAS program? 2

How can the SAS SELECT-WHEN statement be used in a SAS program?

The SAS SELECT-WHEN statement is a conditional statement that can be used in a SAS program to execute specific actions based on a series of conditions. It works by evaluating a series of logical expressions and executing the corresponding action when a condition is met. This statement is often used in conjunction with the SAS DO statement to create more complex logic in a SAS program.

For example, suppose we have a dataset containing information about employees, including their salary and job title. We want to create a new variable called “Salary Bracket” that categorizes each employee’s salary into three groups: “Low”, “Medium”, and “High”. We can use the SAS SELECT-WHEN statement to achieve this.

Example SAS code:

data employees;
set employees;
select;
when (salary = 50000 and salary = 100000) bracket = ‘High’;
end;
run;

In this example, the SELECT-WHEN statement evaluates the value of the “salary” variable for each employee and assigns the appropriate value to the “bracket” variable based on the conditions specified. This allows for efficient and concise code, making it easier to manage and maintain the program.

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 printdata=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 printdata=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 .

Additional Resources

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

Cite this article

stats writer (2024). How can the SAS SELECT-WHEN statement be used in a SAS program?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-sas-select-when-statement-be-used-in-a-sas-program-provide-an-example/

stats writer. "How can the SAS SELECT-WHEN statement be used in a SAS program?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-sas-select-when-statement-be-used-in-a-sas-program-provide-an-example/.

stats writer. "How can the SAS SELECT-WHEN statement be used in a SAS program?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-sas-select-when-statement-be-used-in-a-sas-program-provide-an-example/.

stats writer (2024) 'How can the SAS SELECT-WHEN statement be used in a SAS program?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-sas-select-when-statement-be-used-in-a-sas-program-provide-an-example/.

[1] stats writer, "How can the SAS SELECT-WHEN statement be used in a SAS program?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the SAS SELECT-WHEN statement be used in a SAS program?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top