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

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".

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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".


 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

I have corrected your program:

 

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;

Reeza
Super User

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".


 

ballardw
Super User

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.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register 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
  • 3 replies
  • 1596 views
  • 2 likes
  • 4 in conversation