BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kayla_Tan222
Calcite | Level 5

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User

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

 

 


 

Kurt_Bremser
Super User

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.

 

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1787 views
  • 0 likes
  • 3 in conversation