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.