DATA Step, Macro, Functions and more

else if

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

else if

[ Edited ]

Obsname level :

                             Frank 1

                             Joan 2

                             Sui 2

                             Jose 3

                             Burt 4

                             Kelly .

                             Juan 1

 

The following SAS program is submitted:

 data work . expertise;

 set work. levels;

 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;

 Which of the following values does the variable EXPERTISE contain?

A. Low, Medium, and High only
B. Low, Medium, and Unknown only
C. Low, Medium, High, and Unknown only
D. Low, Medium, High, Unknown, and ' ' (missing character value)

 

 

Can anyone help me with its answer with a reasoning


Accepted Solutions
Solution
‎02-25-2017 10:56 AM
Trusted Advisor
Posts: 1,022

Re: else if

The statement

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

does NOT mean  

   else if level=2 or level=3 then expertise='Medium':

 

That's because the clause "or 3"   is really a logical expression "or [some condition not=0]".    Only zero is false, so 3 must ALWAYS be true.  As a result the "medium" ends up getting everything the failed the prior 2 IF tests.

 

You could use

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

View solution in original post


All Replies
Trusted Advisor
Posts: 1,022

Re: else if

Do you have SAS?  Why not run a program and see?

New Contributor
Posts: 4

Re: else if

[ Edited ]

I have, but somehow not getting the logic. Just want to know why expertise level of 'High' wont come, as there is clearly a level of 4 stated in this problem. This was one of the questions posted on the forums earlier, but didnt find a convincing answer there. Would be great if you can help me understand the logic. Specifically I am stuck as to why the else if NOT generate 'High' as an output in the problem.

Solution
‎02-25-2017 10:56 AM
Trusted Advisor
Posts: 1,022

Re: else if

The statement

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

does NOT mean  

   else if level=2 or level=3 then expertise='Medium':

 

That's because the clause "or 3"   is really a logical expression "or [some condition not=0]".    Only zero is false, so 3 must ALWAYS be true.  As a result the "medium" ends up getting everything the failed the prior 2 IF tests.

 

You could use

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

New Contributor
Posts: 4

Re: else if

I got it ! I was considering it as
if (level=2 or level=3) WHICH is not the case. I got this thing clear now .
Super Contributor
Posts: 441

Re: else if

[ Edited ]

I didn't run the code myself buy this looks suspicious:

 

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

That is not the same as 

 

if (level=2 or level=3)

What SAS sees is 

 

else if 3

which is always true. So the else after that is never executed. 

 

Regards,

- Jan

 

 

New Contributor
Posts: 4

Re: else if

Posted in reply to jklaverstijn
Thanks jklaverstijn. I mixed up exactly in the thing you pointed out/
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 213 views
  • 4 likes
  • 3 in conversation