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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.