• 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
  • VBA
  • About
You are here: Home / SAS / Multiple Condition If Statements in SAS Macro Language

Multiple Condition If Statements in SAS Macro Language

April 22, 2022 Leave a Comment

In the SAS Macro Language, you can easily create if statements with multiple conditions.

%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;

You can also easily define if then statements in the SAS Macro Language with multiple variables with or and and.

%macro process;
    %let a = 3;
    %let b = 5; 
    %if &a > 3 and &b < 10 %then %do;
        %let c = 10;
    %end;
%mend;

When working with data, conditional processing is very useful for defining new variables with logic, or performing operations based on complicated sets of rules.

You can use conditional processing within the SAS Macro Language to create complex programs which are dynamic and efficient.

Using if then else statements within the SAS Macro Language is easy. With if then else statements, you can easily create multiple condition conditional blocks.

For example, if you only need 1 line for the %then block, you 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, you 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;

Multiple Variable If Then Statements with SAS Macro Language

You can also easily define if then statements with multiple variables with or and and.

The SAS Macro Language is very powerful, and with conditional expressions, you can design code which allows us to handle complicated situations.

Again, depending on the conditions you want to set, you can use the logical operators or and and to design different conditional expressions for multiple macro variables.

Below is an example of how to create an if then statement for multiple variables in the SAS Macro Language.

%macro process;
    %let a = 3;
    %let b = 5; 
    %if &a > 3 and &b < 10 %then %do;
        %let c = 10;
    %end;
%mend;

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

Other Articles You'll Also Like:

  • 1.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 2.  Get the Row Number in a SAS Data Step with _n_
  • 3.  SAS round – Rounding Numbers in a SAS Data Step
  • 4.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 5.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 6.  SAS %eval() Function – Evaluate Expressions in SAS Macro
  • 7.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
  • 8.  SAS year function – Get Year from Date Variable
  • 9.  SAS where in – Subset Data by Multiple Values in Data Step
  • 10.  SAS tranwrd() Function – Replace Characters in Strings in 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy