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
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';
Do you have SAS? Why not run a program and see?
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.
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';
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
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.