Table of Contents
A CASE WHEN statement in SAS is a conditional logic statement that allows you to specify different actions based on specified conditions. It follows the general syntax of “CASE WHEN condition1 THEN action1; WHEN condition2 THEN action2; ELSE default action;” This statement is commonly used in data manipulation tasks, such as data cleaning and transformation, to programmatically handle different scenarios and perform the appropriate actions. It provides a more efficient and simplified way to write conditional statements compared to traditional IF-THEN-ELSE statements. Additionally, the CASE WHEN statement can be nested within other SAS statements, making it a versatile tool for data analysis and manipulation.
Use a CASE WHEN Statement in SAS (With Examples)
We can use the CASE statement in SAS to create a new variable that uses case-when logic to determine the values to assign to the new variable.
This statement uses the following basic syntax:
proc sql;
select var1, casewhen var2 = 'A' then 'North'
when var2 = 'B' then 'South'
when var2 = 'C' then 'East'
else 'West'
end as variable_name
from my_data;
quit;
The following example shows how to use the CASE statement in practice.
Example: Using the CASE Statement in SAS
Suppose we have the following dataset in SAS:
/*create dataset*/
data original_data;
input team $ points rebounds;
datalines;
A 25 8
A 18 12
A 22 6
B 24 11
B 27 14
C 33 19
C 31 20
D 30 17
D 18 22
;
run;
/*view dataset*/
proc printdata=original_data;
We can use the following CASE statement to create a new variable called Division whose values are based on the values of the team variable:
/*create dataset*/
proc sql;
select team, points, casewhen team = 'A' then 'North'
when team = 'B' then 'South'
when team = 'C' then 'East'
else 'West'
endas Division
from original_data;
quit;
Note that a new variable Division was created whose values are based on the values for the team variable.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I use a CASE WHEN statement in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-a-case-when-statement-in-sas/
stats writer. "How can I use a CASE WHEN statement in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-a-case-when-statement-in-sas/.
stats writer. "How can I use a CASE WHEN statement in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-a-case-when-statement-in-sas/.
stats writer (2024) 'How can I use a CASE WHEN statement in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-a-case-when-statement-in-sas/.
[1] stats writer, "How can I use a CASE WHEN statement in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I use a CASE WHEN statement in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
