## Simple if then else statement

Solved
Occasional Contributor
Posts: 9

# Simple if then else statement

Hello Experts,

I have i doubt in if then else statement execution.Below is the query :

Data levels;
input name\$ level;
cards;
Frank 1
Joan 2
Sui 2
Jose 3
Burt 4
Kelly .
Juan 1
run;
data work.expertise;
set work.levels;
put level;
if level = . then
expertise = 'Unknown';
else if level = 1 then
expertise = 'Low';
else if level = 2 or 3 then
expertise = 'Medium';
else
expertise = 'High';
run;

The above SAS program shows that the Variable EXPERTISE contains : Low, Medium, and Unknown only.

in the ouput after print.

I don't undersrtand how does "expertise = 'High'; " in the last else statement is not considered for level=4???

As Currently it shows that for level=4 it considers "expertise = 'Medium".

Accepted Solutions
Solution
‎08-18-2017 01:31 PM
Super User
Posts: 23,683

## Re: Simple if then else statement

[ Edited ]

Or use IN

If level in (2,3) then do ...;

SAS considers any positive number above 1 as true. So 'level=2 or 3' will always evaluate to TRUE because its

(level=2) or 3 -> TRUE/FALSE or TRUE -> always TRUE.

rajvir wrote:

Hello Experts,

I have i doubt in if then else statement execution.Below is the query :

Data levels;
input name\$ level;
cards;
Frank 1
Joan 2
Sui 2
Jose 3
Burt 4
Kelly .
Juan 1
run;
data work.expertise;
set work.levels;
put level;
if level = . then
expertise = 'Unknown';
else if level = 1 then
expertise = 'Low';
else if level = 2 or 3 then
expertise = 'Medium';
else
expertise = 'High';
run;

The above SAS program shows that the Variable EXPERTISE contains : Low, Medium, and Unknown only.

in the ouput after print.

I don't undersrtand how does "expertise = 'High'; " in the last else statement is not considered for level=4???

As Currently it shows that for level=4 it considers "expertise = 'Medium".

All Replies
PROC Star
Posts: 1,772

## Re: Simple if then else statement

Data levels;

input name\$ level;

cards;

Frank 1

Joan 2

Sui 2

Jose 3

Burt 4

Kelly .

Juan 1

run;

data work.expertise;

set work.levels;

put level;

if level = . then

expertise = 'Unknown';

else if level = 1 then

expertise = 'Low';

else if level = 2 or level= 3 then /*notice here*/

expertise = 'Medium';

else

expertise = 'High';

run;

Solution
‎08-18-2017 01:31 PM
Super User
Posts: 23,683

## Re: Simple if then else statement

[ Edited ]

Or use IN

If level in (2,3) then do ...;

SAS considers any positive number above 1 as true. So 'level=2 or 3' will always evaluate to TRUE because its

(level=2) or 3 -> TRUE/FALSE or TRUE -> always TRUE.

rajvir wrote:

Hello Experts,

I have i doubt in if then else statement execution.Below is the query :

Data levels;
input name\$ level;
cards;
Frank 1
Joan 2
Sui 2
Jose 3
Burt 4
Kelly .
Juan 1
run;
data work.expertise;
set work.levels;
put level;
if level = . then
expertise = 'Unknown';
else if level = 1 then
expertise = 'Low';
else if level = 2 or 3 then
expertise = 'Medium';
else
expertise = 'High';
run;

The above SAS program shows that the Variable EXPERTISE contains : Low, Medium, and Unknown only.

in the ouput after print.

I don't undersrtand how does "expertise = 'High'; " in the last else statement is not considered for level=4???

As Currently it shows that for level=4 it considers "expertise = 'Medium".

Super User
Posts: 13,508

## Re: Simple if then else statement

Many nested if/then/else can get hard to track. There is another structure that often works better when you have 3 or more categories based on a single value. Please see:

```data work.expertise;
set work.levels;
length expertise \$ 8;
select (level);
when (1) expertise='Low' ;
when (2,3) expertise='Medium' ;
when (4) expertise='High' ;
otherwise expertise='Unknown';
end;
run;```

Note that if sometime in the future you add another category or value you may only have to insert the value (such as 5 is also high when(4,5) ... Or if you add a 5 for "Very High" then only need to insert when (5) expetise='Very High'.

However a value based on single variable may be better to have a custom format.

```proc format library=work;
value expertise
1     ='Low'
2,3   ='Medium'
4     ='High'
other ='Unknown'
;
run;

proc print data=work.levels;
format level expertise.;
run;```

Note that such a format works when used with analysis procedures:

```proc freq data=work.levels;
tables level /missing;
format level expertise.;
run;```

Which has an added benefit of creating a new format and using in the procedure to create different groups without having to add any additional variables.

☑ This topic is solved.

Discussion stats
• 3 replies
• 171 views
• 2 likes
• 4 in conversation