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