• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS case when – Conditional Logic with case Expression in PROC SQL

SAS case when – Conditional Logic with case Expression in PROC SQL

January 25, 2022 Leave a Comment

If we want to do conditional processing in a PROC SQL procedure, we can use the SAS case expression. For the conditions, we use when.

proc sql;
    select name, age, 
        case age 
        when < 18 then "Can't Drink or Smoke"
        when > 21  then "Can Drink and Smoke"
        else "Can't Smoke, but Can Smoke"
        end as legal_description
        from data;
quit;

When working with data, conditional processing is very useful for defining new variables and performing queries on our datasets.

When using the PROC SQL procedure, conditional processing is very useful for creating new columns.

The correct conditional logic in PROC SQL is using a case expression with when clauses. Below is a template for the correct conditional logic in PROC SQL.

proc sql;
...
  case <column-name>
    when when-condition then result-expression
    <when when-condition then result-expression>
    ...
   <else result-expression>
  end
...
quit;

Let’s go through a few examples.

Using case when in PROC SQL for Conditional Processing

In our SAS code, we can use case when expressions in PROC SQL for simple conditional processing, or complex conditional processing.

Let’s say we have a dataset of information for a group of people.

We can use PROC SQL to perform conditional processing to create a new column “Height Category” in the following way.

proc sql;
    select name, height, 
    case height
        when < 60 then "Short"
        when > 72 then "Tall"
        else "Average"
    end as height_category
    from people_dataset;
quit;

In the third line of the above PROC SQL statement, you don’t need to put “height” after “case”. You can also put “height” in the when condition.

proc sql;
    select name, height, 
    case 
        when height < 60 then "Short"
        when height > 72 then "Tall"
        else "Average"
    end as height_category
    from people_dataset;
quit;

Using Operators for Complex when Conditions in PROC SQL

Like with any conditional statement, we can use the logical operators “and” and “or” to define more complex conditional when expressions.

Let’s say we have the same dataset as the last example. We can define a new column called “size” with a conditional expression using the variables “height” and “weight”.

proc sql;
    select name, height, 
    case 
        when height < 60 and weight < 100 then "Small"
        when height > 72 and weight > 200 then "Big"
        else "Average"
    end as size
    from people_dataset;
quit;

case can take any number of when conditions. One final example has some more complex logical statements.

proc sql;
    select name, height, 
    case 
        when height < 60 and weight < 100 then "Smallest"
        when height between 60 and 66 and weight between 100 and 125 then "Small"
        when height between 66 and 72 and weight between 175 and 200 then "Big"
        when height > 72 and weight > 200 then "Biggest"
        else "Average" /* weight between 125 and 175 */
    end as size
    from people_dataset;
quit;

If you are looking for how to do conditional processing in a SAS data step or the SAS Macro Language, read this article on using if then else statements in SAS.

Hopefully this article has been useful for you to understand how to use case when conditional expressions in PROC SQL.

Other Articles You'll Also Like:

  • 1.  intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
  • 2.  SAS right() Function – Right Align Character Variables in Data Step
  • 3.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 4.  SAS nodupkey – How to Remove Duplicates with PROC SORT by Key
  • 5.  Get Last Observation of SAS Dataset with end=
  • 6.  SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step
  • 7.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 8.  SAS min() Function – Find Minimum Value Across Columns in Data Step
  • 9.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 10.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy