• 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 select when – Evaluating Character Values for Conditional Processing

SAS select when – Evaluating Character Values for Conditional Processing

January 25, 2022 Leave a Comment

With the SAS select statement, we can use a character variable and perform conditional processing on it’s values.

data data_new;
    set data;
    select(age_indicator);
       when('Child')   price = 5;
       when('Senior')  price = 10;
       otherwise       price = 12;
    end;
run;

When working with data, conditional processing is very useful for defining new variables with logic. In SAS, one useful way is to use the SAS select statement.

If you’d like to learn how to do conditional processing in data steps or the SAS Macro Language, check out this article about if then else statements in SAS.

If you are looking for how to do conditional processing with PROC SQL, check out this article for case when statements in PROC SQL.

Using select when in a SAS Data Step

We can do conditional processing in a SAS data step is with the SAS select statement. If you have many conditions, using select when can be more beneficial, easier to read and more reusable.

We can use a select when statement to conditionally evaluate statements based on the value of a character variable.

Let’s say we have a dataset which contains information about some people. We can use select when to create new columns based a character value.

data data_new;
    set data;
    select(pant_size);
       when("30")  price = 10;
       when("32")  price = 15;
       when("34")  price = 20;
       when("36")  price = 25;
       otherwise   price = 50;
    end;
run;

If you have multiple actions you’d like to take if after evaluating the value of a character variable, we can add do after when.

data data_new;
    set data;
    select(pant_size);
       when("30") do;
           price = 10;
           taxes = 1;
       end;
       when("32") do;
           price = 15;
           taxes = 1.50;
       end;
       when("34") do;
           price = 20;
           taxes = 2;
       end;
       when("36") do;
           price = 25;
           taxes = 2.50;
       end;
       otherwise do;
           price = 50;
           taxes = 5;
       end;
    end;
run;

Finally, we can use the select when statement to evaluate the multiple values of a character variable.

data data_new;
    set data;
    select(pant_size);
       when("30","32")  price = 10;
       when("34","36")  price = 15;
       otherwise        price = 50;
    end;
run;

Hopefully this article has been helpful for you to learn how to do conditional processing with the SAS select statement.

Other Articles You'll Also Like:

  • 1.  yyyy-mm-dd Date Format in SAS
  • 2.  SAS Remove Formats from Dataset with PROC DATASETS
  • 3.  SAS scan Function – Return nth Word from Character String
  • 4.  Using SAS to Find Mean by Group with PROC MEANS
  • 5.  mod Function in SAS – Find Remainder of 2 Numbers After Division
  • 6.  Set Multiple Datasets in SAS Data Step
  • 7.  SAS sum() Function – Find Sum Across Columns in Data Step
  • 8.  Round Number to Nearest Integer in SAS
  • 9.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 10.  SAS compress – Remove Whitespace and Characters from String

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