How is the IF-THEN/ELSE condition working on this problem?











up vote
1
down vote

favorite
1












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.










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    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.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 2:31

























      asked Nov 10 at 0:42









      hyg17

      1638




      1638
























          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.






          share|improve this answer

















          • 1




            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


















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





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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.






            share|improve this answer

















            • 1




              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















            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.






            share|improve this answer

















            • 1




              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













            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 3:15









            Reeza

            13k21126




            13k21126








            • 1




              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














            • 1




              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








            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












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





            share|improve this answer

























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





              share|improve this answer























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





                share|improve this answer












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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 6:53









                Tom

                22.2k2718




                22.2k2718






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff