CASE WHEN Statement and Some Functions in SQL SAS

Patrizia Castagno
3 min readApr 20, 2024

Learn with examples

This is a continuation of the previous blog post titled “Introduction to SAS”, “JOINS IN PROC SQL” and “Getting Started with PROC SQL in SAS: A Beginner’s Guide”. In this segment, we delve into the concept of “Case When,” which assists in selectively determining result values for each row/observation within a table. It also aids in creating variables based on specific conditions.

Syntax:

CASE <case-operand> /*this is useful when we have 1 variable */
when when-condition Then result expresion
<when when-condition then result expresion>
<ELSE result-expression>
END

Let’s see an example. For that, we use the table sashelp.shoes, in the libraries SAS

Imagen by the Author

Suppose that we want to generate a new column named “price category” where shoes priced above 2000 are labeled as “high,” those priced between 500 and 1999 are categorized as “medium,” and the rest are designated as “low.”

 
PROC SQL;
select *,
case when Sales > 2000 then 'hight'
when Sales between 500 and 1999 then 'medium'
else 'low'
end as category_price
from SASHELP.SHOES;
QUIT;

Now, we will see some function in SAS. You can find all the list of the function in SAS Help Center.

--

--

Patrizia Castagno
Patrizia Castagno

Written by Patrizia Castagno

Physics and Data Science.Eagerly share insights and learn collaboratively in this growth-focused space.LinkedIn:www.linkedin.com/in/patrizia-castagno-diserafino

No responses yet