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

data a;

input name $8. level 1.;

cards;

Alfred 1

Alice 2

Barbara 2

Carol 3

Brut 4

Kelly .

Juan 1

;

run;

data exp;

set a;

if level =. then exp = 'Unknown';

else if level = 1 then exp = 'Low';

else if level= 2 or 3 then exp ='Medium';

else exp = 'high';

run;

 

The output dor dataset exp is as below:

 

namelevelexp
Alfred.Unknown
Alice2Medium
Barbara2Medium
Carol3Medium
Brut4Medium
Kelly.Unknown
Juan1Low

 

 

Not sure why for level 4 exp is 'Medium'?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
else if level= 2 or 3 then exp ='Medium';

is translated as

 

if (level = 2) or (3)

Now, in SAS the boolean value of "false" is represented by the number zero; any other numerical value means "true". "3" is therefore always true, and makes the whole condition always true, meaning that the branch for "high" will never be entered.

 

Edit: Heh, Josh beat me to it.

View solution in original post

4 REPLIES 4
JoshB
Quartz | Level 8

You would need to use the 'in' operator or list out each condition of the 'or' operator for the 'Medium' assignment.

 

else if level in(2 3) then exp ='Medium';

/* or */

else if level = 2 or level = 3 then exp = 'Medium';

I would also recommend you take a look at PROC FORMAT. It is useful for assignment logic like this.

Y_S
Calcite | Level 5 Y_S
Calcite | Level 5

Thanks for the solution, but I am intrested in understanding why exp for level  4 is 'Medium' when datastep written in that way.

JoshB
Quartz | Level 8

Not 100% sure, but my hunch is that the expression is being evaluated like so..

 

if (level = 2) or ( 3 )

 

For your observation in question, level = 2 is evaulated as false, but (3) on it's own is always true. It would be like saying if 3 ^= .

The if statement returns a value of true, thus the assignment of 'Medium' takes place.

Kurt_Bremser
Super User
else if level= 2 or 3 then exp ='Medium';

is translated as

 

if (level = 2) or (3)

Now, in SAS the boolean value of "false" is represented by the number zero; any other numerical value means "true". "3" is therefore always true, and makes the whole condition always true, meaning that the branch for "high" will never be entered.

 

Edit: Heh, Josh beat me to it.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1100 views
  • 3 likes
  • 3 in conversation