Hello,
I've a dataset test1 where i want to write expertise column based on some condition . when i gave the condition for Medium (else if level = 2 or 3 then) then else condition is not working, however I have a value for else. Can anybody please explain why Else condition is not working and how SAS considering given condition (else if level = 2 or 3).
data test1;
input NAME $ 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';
CARDS;
Frank 1
Joan 2
Sui 2
Jose 3
Burt 4
Kelly .
Juan 1
;
run;
Thank you all in advance !!
Hello @singhsahab,
The condition level = 2 or 3 is true regardless of the value of variable LEVEL because SAS interprets 3 (and numeric values in general) as a Boolean value of its own, like (level = 2) or (3). Hence, the subsequent ELSE statement will not take effect. Numeric values (as Boolean expressions) are evaluated as TRUE if they are nonmissing and not zero.
You need:
else if level in (2,3) then
Or:
else if level=2 or level=3 then
You can't use 2 or 3 like that.
Hello @singhsahab,
The condition level = 2 or 3 is true regardless of the value of variable LEVEL because SAS interprets 3 (and numeric values in general) as a Boolean value of its own, like (level = 2) or (3). Hence, the subsequent ELSE statement will not take effect. Numeric values (as Boolean expressions) are evaluated as TRUE if they are nonmissing and not zero.
Logical operators are resolved after arithmetic operators and expressions. So
if level = 2 or 3
resolves to
if (level = 2) or (3)
3 (which is not zero or missing) is considered "true", so the condition will always be true, and the "else" never be executed.
If you want to display a given text for values or ranges of values for a single variable consider using FORMATS instead.
You can apply a format at the time of use so adding a variable is usually not needed and the formats will create groups honored by analysis or graphing procedures.
Example:
proc format library=work; value expertise .='Unknown' 1='Low' 2,3='Medium' other='High' ; run; data test1; input NAME $ LEVEL; CARDS; Frank 1 Joan 2 Sui 2 Jose 3 Burt 4 Kelly . Juan 1 ; run; proc freq data=test1; /* missing is needed to include missing values of the variable in proc freq output */ tables level/missing; format level expertise.; run;
Another advantage of formats is the code for assigning values to ranges can be much more concise then a slew of If/then/else because you can use end of interval indicators: 32.1 - 34.5 includes the ends, 32.1 <- 34.5 excludes 32.1 but includes 32.10004 for example, and 32.1 <-< 34.5 excludes both end points. Additionally you have keywords LOW and HIGH to get from the smallest value or largest value (for numerics) that your SAS session can use.
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.