By admin | October 7, 2009

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)

16 comments | Add One

  1. admin - 10/7/2009 at 3:30 pm

    B

  2. quesiton - 10/23/2010 at 3:31 pm

    Why there is no ‘High’?

  3. Bakul - 12/16/2010 at 5:39 pm

    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

  4. Deepak - 08/16/2011 at 9:17 am

    @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 .

  5. A - 10/1/2011 at 8:47 pm

    or operator doesnt go with the if statement. Once if encounters or it stops there and takes only the value till that variable

  6. Jagadeesh - 01/19/2012 at 9:17 am

    @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

  7. ankit - 01/30/2012 at 7:23 am

    @admin @bakul deepak is correct

  8. akived - 02/25/2012 at 1:01 am

    can some one please explain this please, ans B is correct, but could not figure it out how ?

  9. rolling23 - 05/7/2012 at 11:01 am

    anyone gives the explanation?

  10. dk - 06/11/2012 at 9:01 pm

    some please explain this, why the answer is B ?

  11. adyd - 08/31/2012 at 7:05 am

    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’

  12. Renu - 10/23/2012 at 1:49 pm

    i get the answer b but could someone explain how?

  13. sunny - 08/31/2013 at 10:32 pm

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

  14. seizetheday - 12/3/2013 at 9:20 pm

    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

  15. Mallika - 03/27/2014 at 11:33 pm

    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.

  16. Lili - 07/25/2014 at 11:09 am

    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.

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *