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
Good one
ReplyDeletec
ReplyDeleteAnswer is B. Please run the program and see the answes by yourself, if required
ReplyDeleteB
DeleteAnswer is B
ReplyDeleteThe 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"!
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.
DeleteB
ReplyDeleteit should be
else if level = 2 or level = 3 then expertise = 'Medium';
to get all out put...
Exactly...
DeleteThis code to fetches the ENTIRE "expertise".
DeleteELSE 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
This code to fetches the ENTIRE "expertise".
DeleteELSE 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
!good explaining!
DeleteAnswer is B
ReplyDeleteobs 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
Yes Answer is B
ReplyDeleteDoes anyone explain why it is B ?
ReplyDeleteif you run an if statement with a constant, all values in the new variable will populate with what you used at "then"
DeleteFor example if you only use one if statement in this form:
if 3 then expertise = 'gotcha';
all values in expertise will be 'gotcha'
Answer is B.The condition
ReplyDeleteelse 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".
The answer is B.
ReplyDeleteto 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';
The answer is B.
ReplyDeleteSAS 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.
This is the best explanation out of the entire comments section, thanks for your time Mr or Ms Anonymous!
Deleteyes, this is the best and detailed explanation
Deletegood explanation
Deletegood explanation but one point, its not 3=3
Delete"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
This comment has been removed by the author.
Deletevery good explanation jayrekhs :)
ReplyDeletethx now i got the correct ans
THANKS!!! A GOOD EXPERIENCE
ReplyDeletethanks!!
ReplyDeleteB
ReplyDeleteTry 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;
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?
ReplyDeleteMy senior colleague found this for me. This helped me. hope it helps you too:
ReplyDelete"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;"
Excellent, thank you.
DeleteAwesome!!!
DeleteB
ReplyDeleteanswer is b
ReplyDeletei cant understand else statement why it stops execution at there only???can any one explain me???
ReplyDeleteeven 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?
ReplyDeleteits d trust me
ReplyDeletetry this
ReplyDeleteyou 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;
B
ReplyDeleteYes it is C from my sas studio. but why do prior people answered B? Is it because version is updated?
ReplyDeleteSorry it is B... Even 4 get a medium value.
ReplyDelete3 is treated as Boolean and is always true..
ReplyDeletegood question
how B
ReplyDeleteB.. Great explanation!
ReplyDeleteB
ReplyDeleteI think the answer should be D.
ReplyDeletegclub
gclub casino online
This comment has been removed by the author.
DeleteBecause 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
ReplyDeleteWow, absolutely fantastic blog. I am very glad to have such useful information.
ReplyDeleteทองดีฟันขาว
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.
ReplyDeleteSAS Training in Bangalore