Hi, can I have an if statement with multiple condition ? my code is below
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C' then
lob='PA';
else if test1='P' then
lob='PA';
else if test1='Q' then
lob='PA';
else if test1='R' then
lob='PA';
For the last four if statement the output is actually the same, can I combine them together instead of list down them one by one like above?
I tried to run the code like this but it does not work. Is it anything wrong?
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C',test1='P',test1='Q',test1='R' then
lob='PA';
You can use IN or OR here. IN is much more useful:
OR
else if test1='C' or test1 = 'P' or test1 = 'Q' or test1 = 'R'
then lob='PA';
IN
else if test1 in ('C', 'P', 'Q', 'R') then lob='PA';
@Kayla_Tan222 wrote:
Hi, can I have an if statement with multiple condition ? my code is below
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C' then
lob='PA';
else if test1='P' then
lob='PA';
else if test1='Q' then
lob='PA';
else if test1='R' then
lob='PA';
For the last four if statement the output is actually the same, can I combine them together instead of list down them one by one like above?
I tried to run the code like this but it does not work. Is it anything wrong?
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C',test1='P',test1='Q',test1='R' then
lob='PA';
You can use IN or OR here. IN is much more useful:
OR
else if test1='C' or test1 = 'P' or test1 = 'Q' or test1 = 'R'
then lob='PA';
IN
else if test1 in ('C', 'P', 'Q', 'R') then lob='PA';
@Kayla_Tan222 wrote:
Hi, can I have an if statement with multiple condition ? my code is below
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C' then
lob='PA';
else if test1='P' then
lob='PA';
else if test1='Q' then
lob='PA';
else if test1='R' then
lob='PA';
For the last four if statement the output is actually the same, can I combine them together instead of list down them one by one like above?
I tried to run the code like this but it does not work. Is it anything wrong?
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C',test1='P',test1='Q',test1='R' then
lob='PA';
PLEASE use the "little running man" for posting code; it is specifically implemented for this, and the code then looks like this:
if test1='F' then
lob='Fire';
else if test1='G' then
lob='others';
else if test1='C' then
lob='PA';
else if test1='P' then
lob='PA';
else if test1='Q' then
lob='PA';
else if test1='R' then
lob='PA';
with consistent typeface and -size, and indentation kept, and coloring like the enhanced editor does.
(assuming you had some visual formatting in the first place, see Maxim 12)
Every time you have such a if/then/else if chain, think of using the data step select statement:
select (test1);
when ('F') lob='Fire';
when ('G') lob='others';
when ('C','P','Q','R') lob='PA';
otherwise;
end;
You can see how simple and clean the code looks now.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.