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

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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';

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

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

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nish1017
Fluorite | Level 6

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.

mkeintz
PROC Star

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';

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
nish1017
Fluorite | Level 6
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 .
jklaverstijn
Rhodochrosite | Level 12

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

 

 

nish1017
Fluorite | Level 6
Thanks jklaverstijn. I mixed up exactly in the thing you pointed out/

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1237 views
  • 4 likes
  • 3 in conversation