BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
singhsahab
Lapis Lazuli | Level 10

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 !!

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

 

 

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

FreelanceReinh
Jade | Level 19

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.

 

 

Kurt_Bremser
Super User

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.

ballardw
Super User

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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 847 views
  • 2 likes
  • 5 in conversation