SAS Certified Base Programmer 123 Questions (111)
A SAS PRINT procedure output of the WORK.LEVELS data set is listed below:
Obs name level 1 Frank 1 2 Joan 2 3 Sui 2 4 Jose 3 5 Burt 4 6 Kelly . 7 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)
Topics:
SAS Base Questions |
16 Comments »
B
Why there is no ‘High’?
To get the correct output
you need to change as
else if level in(2,3) then
instead of
else if level = 2 or 3 then
@Admin, @Bakul: I didn’t understand the answer B. Even after correction, for obs#5 it will give high as level is not 1,2,3 or .
or operator doesnt go with the if statement. Once if encounters or it stops there and takes only the value till that variable
@Deepak, When you are referring same variable i.e X is either 1 or 2 you should write them as
X=1 or X=2.
If you write it X=1 or 2 it will result the value always true. Coz It compares the the value with non zero or non missing. since its not any one of them it will result the same
@admin @bakul deepak is correct
can some one please explain this please, ans B is correct, but could not figure it out how ?
anyone gives the explanation?
some please explain this, why the answer is B ?
if you want 4 which is needed
as high you can also change the above code as
‘else if level = 2 or level=3 then’
i get the answer b but could someone explain how?
SAS is using 3 as a Boolean, and it’s always TRUE. Because 3 is always equal to 3. basically you are writing and using the following code logic:
else if level = 2 or 3=3 then
expertise =’ Medium’;
if x=2 or x=3 then expertise =”medium”, 4 is always equal to 4, so this portion of the equation is always true.
So for a value other than missing. 1 and 2 the following happens regarding the logic. Take the value of 4 for instance:
if level=.(is false if level=4) go to the next else if.
if level=1 ( is false if x=4) go to the next else if.
if level = 2 or 3 then match. One or the other must be true for this expression to be true (the value is 4, so level=2 is false, but 3=3 is always true! so even thought the value is 4, 3=3 is true and expression is “medium”. That’s why you are getting the results of medium for the value of match when the values of level are anything but missing or 1. To get the correct result that is medium for only 2 or 3, you should write the statement as below:
else if level=2 or level=3 then expertise=”medium”;
else if level = 2 or 3 then
because of “or 3”, so this condition is AWLAYS true.
so, when level=4, then entering expertise =’ Medium’.
It will NEVER to reach the last condition.
so, B is right.
if you want to get ‘High’, just modify condition to:
else if level = 2 or level=3 then
The SAS program assigns the expertise ‘unknown’ to missing value, ‘low’ to le 1 and for values 2 and above as ‘medium’. Unless specified clearly level=2 or level=3, it will not assign ‘high’ to level=4.
The trick is in the statement “else if level = 2 or 3”. Think about the difference between that statement and the statement “else if level = 2 or level = 3″.
As known, the expression in an IF statement produces a result that is either non-zero, zero, or missing. A non-zero and nonmissing result causes the expression to be true; a result of zero or missing causes the expression to be false. The statement in the program can always return 3, a non-zero value no matter level is 2 or not. So the statement ” expertise = ‘High’ ” is never executed.