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.