• 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 if then else – Write Conditional Expressions for Multiple Conditions

SAS if then else – Write Conditional Expressions for Multiple Conditions

January 25, 2022 Leave a Comment

To do conditional processing in a SAS Data Step, the easiest way is with if then else statements.

data data_new;
    set data;
    length legal_description $ 50.;
    if age < 18 then legal_description = "Can't Drink or Smoke";
    else if age < 21 then legal_description = "Can't Drink, but Can Smoke";
    else legal_description = "Can Drink and Smoke";
run;

data data_new;
    set data;
    length legal_description $ 50.;
    if age < 18 then do;
        legal_description = "Can't Drink or Smoke";
    end;
    else if age < 21 then do;
        legal_description = "Can't Drink, but Can Smoke";
    end;
    else do;
        legal_description = "Can Drink and Smoke";
    end;
run;

We can also use select when statements to evaluate conditional expressions on character variables.

data data_new;
    set data;
    length legal_description $ 50.;
    select(age_indicator);
       when('<18')    legal_description = "Can't Drink or Smoke";
       when('18-21')  legal_description = "Can't Drink, but Can Smoke";
       otherwise      legal_description = "Can Drink and Smoke";
    end;
run;

Finally, we can easily perform conditional processing with SAS Macro language with if then else statements.

%macro conditional;
    /* if you only need 1 line for the "then" expression */
    %if &var = 1 %then /* do something */; 
    %else %if &var = 2 %then /* do something else */;
    %else /* do something else */;

    /* if you have multiple lines for the "then" expression */
    %if &var = 1 %then %do;
        /* do something */
    %end;
    %else %if &var = 2 %then %do;
        /* do something else */
    %end;
    %else %do;
       /* do something else */
    %end;    
%mend;

When working with data, conditional processing is very useful for defining new variables with logic. In SAS, there are a few different ways we can do conditional processing.

In this article, we will cover how to use if then else and select when statements in a SAS data step, as well as how to use conditional processing with the SAS Macro Language.

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 If Then Else in a SAS Data Step

Conditional processing in a SAS data step is easy to do. We can use if then else statements to use conditional logic to create new columns.

There are two ways we can use if then else statements to create new columns in a SAS data step.

Let’s say we have a dataset with information about people.

The first way we can use if then else statements is a simple if then statement. If we only have one code expression after an if statement, we can do the following.

data data_new;
    set data;
    length height_category $ 10.;
    if height < 60 then height_category = "Short";
    else if height > 72 then height_category = "Tall";
    else height_category = "Average";
run;

As you can see, we can put the if expression as well as the resulting code expression all on the same line with the simple if then expression.

If we have more than one code action we’d like to perform, we use if followed by then do. Then, you need to use an end statement.

data data_new;
    set data;
    length height_category $ 10. height_string $ 5.;
    if height < 60 then do;
        height_category = "Short";
        height_string = '<60';
    end;
    else if height > 72 then do;
        height_category = "Tall";
        height_string = '>72';
    end;
    else do;
        height_category = "Average";
        height_string = '60-72';
    end;
run;

Using select when in a SAS Data Step

Another way we can do conditional processing in a SAS data step is with the SAS select< statement. If you have a lot of 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.

data data_new;
    set data;
    length height_category $ 10. bed_size $ 12.;
    if height < 60 then height_category = "Short";
    else if height > 72 then height_category = "Tall";
    else height_category = "Average";
    select(height_category);
       when("Tall")  bed_size = "King";
       otherwise     bed_size = "Queen";
    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;
    length height_category $ 10. bed_size $ 12. sheet_size $ 12.;
    if height < 60 then height_category = "Short";
    else if height > 72 then height_category = "Tall";
    else height_category = "Average";
    select(height_category);
       when("Tall") do;
           bed_size = "King";
           sheet_size = "King";
       end;
       otherwise do;
           bed_size = "Queen";
           sheet_size = "Queen";
       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;
    length height_category $ 10. bed_size $ 12. sheet_size $ 12.;
    if height < 60 then height_category = "Short";
    else if height > 72 then height_category = "Tall";
    else height_category = "Average";
    select(height_category);
       when("Average", "Tall") do;
           bed_size = "King";
           sheet_size = "King";
       end;
       otherwise do;
           bed_size = "Queen";
           sheet_size = "Queen";
       end;
    end;
run;

If Then Else with SAS Macro Language

In my experience, using conditional processing within the SAS Macro Language has allowed me to create complex programs which are dynamic and efficient.

Creating if then else statements with multiple conditions in the SAS Macro Language is easy.

You can use if then else statements in a SAS macro just like in a SAS data step.

If you only need 1 line for the then expression, we can do the following:

%macro conditional;
    /* if you only need 1 line for the "then" expression */
    %if &var = 1 %then /* do something */; 
    %else %if &var = 2 %then /* do something else */;
    %else /* do something else */;
%mend;

If you need multiple lines for your then expression, we need to add “%do” and “%end” to the if block.


    /* if you have multiple lines for the "then" expression */
    %if &var = 1 %then %do;
        /* do something */
    %end;
    %else %if &var = 2 %then %do;
        /* do something else */
    %end;
    %else %do;
       /* do something else */
    %end;    
%mend;

Hopefully this article has been beneficial for you to learn how to use if then else statements in your SAS code.

Other Articles You'll Also Like:

  • 1.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 2.  SAS nodupkey – How to Remove Duplicates with PROC SORT by Key
  • 3.  SAS If Then Statements with Multiple Variables
  • 4.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 5.  countw SAS – Count Number of Words in a String
  • 6.  SAS include – Execute Code from Other Files in SAS with %include
  • 7.  SAS Remove Labels from Dataset with PROC DATASETS
  • 8.  How to Select First 100 Observations of SAS Dataset
  • 9.  SAS call symput – Create Macro Variables in Data Step
  • 10.  SAS weekday function – Get Day of Week from Date Variable

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