ElearnSAS.com

ElearnSAS.com
SAS Learning Platform

Base SAS 14

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)Click Comment link to get answer

49 comments:

  1. Anonymous12:39 AM

    Good one

    ReplyDelete
  2. Anonymous1:48 AM

    Answer is B. Please run the program and see the answes by yourself, if required

    ReplyDelete
  3. Anonymous3:21 PM

    Answer is B
    The statement:
    else if level = 2 or 3 then
    has the GOTCHA!
    as the condition "3" is always true all the remaining values are set to "medium"!

    ReplyDelete
    Replies
    1. Anonymous1:58 AM

      yes,u r right. Note that "if level=2 or 3" is equivalent to "if (level=2) or 3". Obviously, the condition is always true.

      Delete
  4. B
    it should be
    else if level = 2 or level = 3 then expertise = 'Medium';
    to get all out put...

    ReplyDelete
    Replies
    1. Unmesh4:26 PM

      Exactly...

      Delete
    2. This code to fetches the ENTIRE "expertise".

      ELSE IF LEVEL IN (2,3) THEN EXPERTISE = 'MEDIUM';


      The SAS System

      Obs NAME LEVEL EXPERTISE

      1 FRANK 1 LOW
      2 JOAN 2 MEDIUM
      3 SUI 2 MEDIUM
      4 JOSE 3 MEDIUM
      5 BURT 4 HIGH
      6 KELLY . UNKNOWN
      7 JUAN 1 LOW

      Delete
    3. This code to fetches the ENTIRE "expertise".

      ELSE IF LEVEL IN (2,3) THEN EXPERTISE = 'MEDIUM';


      The SAS System

      Obs NAME LEVEL EXPERTISE

      1 FRANK 1 LOW
      2 JOAN 2 MEDIUM
      3 SUI 2 MEDIUM
      4 JOSE 3 MEDIUM
      5 BURT 4 HIGH
      6 KELLY . UNKNOWN
      7 JUAN 1 LOW

      Delete
    4. !good explaining!

      Delete
  5. Answer is B

    obs name level expertise
    1 Frank 1 Low
    2 Joan 2 Medium
    3 Sui 2 Medium
    4 Jose 3 Medium
    5 Burt 4 Medium
    6 Kelly Unknown
    7 Juan 1 Low

    ReplyDelete
  6. Anonymous8:43 AM

    Yes Answer is B

    ReplyDelete
  7. Anonymous9:26 AM

    Does anyone explain why it is B ?

    ReplyDelete
    Replies
    1. if you run an if statement with a constant, all values in the new variable will populate with what you used at "then"

      For example if you only use one if statement in this form:
      if 3 then expertise = 'gotcha';
      all values in expertise will be 'gotcha'

      Delete
  8. Answer is B.The condition

    else if level = 2 or 3 then

    is where the problem arises. This will give true condition always so program never reaches to statement where expertise is assigned a value of "HIGH".

    ReplyDelete
  9. swapna7:50 PM

    The answer is B.
    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';

    ReplyDelete
  10. The answer is B.
    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:
    If x=2 or 3=3 then expertise='Medium'; 4 is ALWAYS = 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 though the value is 4, 3=3 is true and expertise='medium'. That’s why you are getting the results of medium for the values of match when the values of level are anything but missing or 1.

    ReplyDelete
    Replies
    1. Anonymous1:21 AM

      This is the best explanation out of the entire comments section, thanks for your time Mr or Ms Anonymous!

      Delete
    2. Anonymous5:30 AM

      yes, this is the best and detailed explanation

      Delete
    3. Anonymous2:23 AM

      good explanation

      Delete
    4. Anonymous9:39 PM

      good explanation but one point, its not 3=3

      "In SAS, any numeric value other than 0 or missing is true"

      so 3 which is not 0 or missing, it is true.

      http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm

      Delete
    5. This comment has been removed by the author.

      Delete
  11. madhuri7:57 AM

    very good explanation jayrekhs :)
    thx now i got the correct ans

    ReplyDelete
  12. Anonymous4:13 AM

    THANKS!!! A GOOD EXPERIENCE

    ReplyDelete
  13. Anonymous4:20 AM

    thanks!!

    ReplyDelete
  14. Anonymous12:08 AM

    B

    Try this:

    data test1;
    input NAME $ LEVEL;
    if level = . then
    expertise = 'Unknown';
    else if level = 1 then
    expertise = 'Low';
    else if level = 2 or 3 then
    expertise = 'Medium';
    else
    expertise = 'High';

    CARDS;
    Frank 1
    Joan 2
    Sui 2
    Jose 3
    Burt 4
    Kelly .
    Juan 1
    ;
    run;

    ReplyDelete
  15. Anonymous2:40 PM

    It is silly things to cut and paste. We can do it, along with your answer we are trying to grasp some more logic that we are lacking. In test we do not have sas program to run rather we learn the reason and logic. If they change the statement are you still going to answer same way or look for reasoning?

    ReplyDelete
  16. Remya9:37 AM

    My senior colleague found this for me. This helped me. hope it helps you too:

    "I found this answer at the link: http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm

    Be careful when using the OR operator with a series of comparisons (in an IF, SELECT, or WHERE statement, for example). Remember that only one comparison in a series of OR comparisons must be true to make a condition true, and any nonzero, nonmissing constant is always evaluated as true (see Boolean Numeric Expressions). Therefore, the following subsetting IF statement is always true:
    if x=1 or 2;"

    ReplyDelete
  17. Anonymous4:44 PM

    i cant understand else statement why it stops execution at there only???can any one explain me???

    ReplyDelete
  18. even me too...i am also not getting the point why it will stop execution at else statement? rather why it will not High when 4 is there?

    ReplyDelete
  19. Anonymous5:12 PM

    its d trust me

    ReplyDelete
  20. try this
    you ill get B


    data work.expertise;
    input level;
    if level = . then
    expertise = 'Unknown';
    else if level = 1 then
    expertise = 'Low';
    else if level = 2 or 3 then
    expertise = 'Medium';
    else
    expertise = 'High';
    datalines;
    1
    2
    2
    3
    4
    .
    1
    ;
    run;
    proc print data = expertise;
    run;

    ReplyDelete
  21. Anonymous9:49 AM

    Yes it is C from my sas studio. but why do prior people answered B? Is it because version is updated?

    ReplyDelete
  22. Anonymous9:51 AM

    Sorry it is B... Even 4 get a medium value.

    ReplyDelete
  23. Anonymous1:03 PM

    3 is treated as Boolean and is always true..

    good question

    ReplyDelete
  24. Anonymous12:44 PM

    how B

    ReplyDelete
  25. B.. Great explanation!

    ReplyDelete
  26. Replies
    1. This comment has been removed by the author.

      Delete
  27. Anonymous4:42 PM

    Because of this "else if level = 2 or 3 then" . When an if statement goes with a constant ( number 3 in this case,) it will always evaluated as true. The correct version should be else if level = 2 or level = 3 then

    ReplyDelete
  28. Wow, absolutely fantastic blog. I am very glad to have such useful information.

    ทองดีฟันขาว

    ReplyDelete
  29. I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
    SAS Training in Bangalore

    ReplyDelete