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.