How is the IF-THEN/ELSE condition working on this problem?
up vote
1
down vote
favorite
I am a bit puzzled why the output is a certain way on the following program.
data a;
input name$ lv @@;
cards;
Frank 1 Joan 2 Sui 3 Burt 4 Kelly . Juan 1
;
data b;
set a;
if lv=.
then expertise='?';
else if lv=1
then expertise='L';
else if lv=2 or 3
then expertise ='M';
else expertise ='H';
run;
proc print data=b;
run;
In the program above, I am expecting that the output for the observation containing Burt has expertise of value H, but for some reason it is M.
I was thinking that the if statement should be lv=(2 or 3) but when I do that now those that I am thinking that the lv= 2 and lv=3 whose expertise should equal M now becomes H as well.
*This is probably explained by the fact that the syntax is inappropriate, and lv is never (2 or 3) /I am not sure why this does not cause an error/ thus the ELSE statement is executed.
I have a feeling that I am not understanding how the ELSE is really working.
However, according to that logic then if the lv=2 part is recognized but the others are not, I am not sure why the lv= 3 and lv=4 does not produce an H.
My goal is to understand why the program runs as if there is no syntax error and why the output is what it is.
sas
add a comment |
up vote
1
down vote
favorite
I am a bit puzzled why the output is a certain way on the following program.
data a;
input name$ lv @@;
cards;
Frank 1 Joan 2 Sui 3 Burt 4 Kelly . Juan 1
;
data b;
set a;
if lv=.
then expertise='?';
else if lv=1
then expertise='L';
else if lv=2 or 3
then expertise ='M';
else expertise ='H';
run;
proc print data=b;
run;
In the program above, I am expecting that the output for the observation containing Burt has expertise of value H, but for some reason it is M.
I was thinking that the if statement should be lv=(2 or 3) but when I do that now those that I am thinking that the lv= 2 and lv=3 whose expertise should equal M now becomes H as well.
*This is probably explained by the fact that the syntax is inappropriate, and lv is never (2 or 3) /I am not sure why this does not cause an error/ thus the ELSE statement is executed.
I have a feeling that I am not understanding how the ELSE is really working.
However, according to that logic then if the lv=2 part is recognized but the others are not, I am not sure why the lv= 3 and lv=4 does not produce an H.
My goal is to understand why the program runs as if there is no syntax error and why the output is what it is.
sas
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am a bit puzzled why the output is a certain way on the following program.
data a;
input name$ lv @@;
cards;
Frank 1 Joan 2 Sui 3 Burt 4 Kelly . Juan 1
;
data b;
set a;
if lv=.
then expertise='?';
else if lv=1
then expertise='L';
else if lv=2 or 3
then expertise ='M';
else expertise ='H';
run;
proc print data=b;
run;
In the program above, I am expecting that the output for the observation containing Burt has expertise of value H, but for some reason it is M.
I was thinking that the if statement should be lv=(2 or 3) but when I do that now those that I am thinking that the lv= 2 and lv=3 whose expertise should equal M now becomes H as well.
*This is probably explained by the fact that the syntax is inappropriate, and lv is never (2 or 3) /I am not sure why this does not cause an error/ thus the ELSE statement is executed.
I have a feeling that I am not understanding how the ELSE is really working.
However, according to that logic then if the lv=2 part is recognized but the others are not, I am not sure why the lv= 3 and lv=4 does not produce an H.
My goal is to understand why the program runs as if there is no syntax error and why the output is what it is.
sas
I am a bit puzzled why the output is a certain way on the following program.
data a;
input name$ lv @@;
cards;
Frank 1 Joan 2 Sui 3 Burt 4 Kelly . Juan 1
;
data b;
set a;
if lv=.
then expertise='?';
else if lv=1
then expertise='L';
else if lv=2 or 3
then expertise ='M';
else expertise ='H';
run;
proc print data=b;
run;
In the program above, I am expecting that the output for the observation containing Burt has expertise of value H, but for some reason it is M.
I was thinking that the if statement should be lv=(2 or 3) but when I do that now those that I am thinking that the lv= 2 and lv=3 whose expertise should equal M now becomes H as well.
*This is probably explained by the fact that the syntax is inappropriate, and lv is never (2 or 3) /I am not sure why this does not cause an error/ thus the ELSE statement is executed.
I have a feeling that I am not understanding how the ELSE is really working.
However, according to that logic then if the lv=2 part is recognized but the others are not, I am not sure why the lv= 3 and lv=4 does not produce an H.
My goal is to understand why the program runs as if there is no syntax error and why the output is what it is.
sas
sas
edited Nov 10 at 2:31
asked Nov 10 at 0:42
hyg17
1638
1638
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
1
Any value that is not0
or missing istrue
.
– Tom
Nov 10 at 5:53
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
add a comment |
up vote
2
down vote
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3
is evaluated from left to right. So lv=2
will result in either 1
or 0
depending whether the value of lv
is 2
or not. But both 1 or 3
and 0 or 3
will be true since 3
is always considered true.
What you really want is the in
operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
1
Any value that is not0
or missing istrue
.
– Tom
Nov 10 at 5:53
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
add a comment |
up vote
2
down vote
accepted
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
1
Any value that is not0
or missing istrue
.
– Tom
Nov 10 at 5:53
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
answered Nov 10 at 3:15
Reeza
13k21126
13k21126
1
Any value that is not0
or missing istrue
.
– Tom
Nov 10 at 5:53
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
add a comment |
1
Any value that is not0
or missing istrue
.
– Tom
Nov 10 at 5:53
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
1
1
Any value that is not
0
or missing is true
.– Tom
Nov 10 at 5:53
Any value that is not
0
or missing is true
.– Tom
Nov 10 at 5:53
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
Thank you very much! Everyone was such a big help.
– hyg17
Nov 10 at 6:57
add a comment |
up vote
2
down vote
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3
is evaluated from left to right. So lv=2
will result in either 1
or 0
depending whether the value of lv
is 2
or not. But both 1 or 3
and 0 or 3
will be true since 3
is always considered true.
What you really want is the in
operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
add a comment |
up vote
2
down vote
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3
is evaluated from left to right. So lv=2
will result in either 1
or 0
depending whether the value of lv
is 2
or not. But both 1 or 3
and 0 or 3
will be true since 3
is always considered true.
What you really want is the in
operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
add a comment |
up vote
2
down vote
up vote
2
down vote
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3
is evaluated from left to right. So lv=2
will result in either 1
or 0
depending whether the value of lv
is 2
or not. But both 1 or 3
and 0 or 3
will be true since 3
is always considered true.
What you really want is the in
operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3
is evaluated from left to right. So lv=2
will result in either 1
or 0
depending whether the value of lv
is 2
or not. But both 1 or 3
and 0 or 3
will be true since 3
is always considered true.
What you really want is the in
operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
answered Nov 10 at 6:53
Tom
22.2k2718
22.2k2718
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235017%2fhow-is-the-if-then-else-condition-working-on-this-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown