DATA Step, Macro, Functions and more

Why "else" doesn't get executed?

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Why "else" doesn't get executed?

Hi,

 

This is driving me crazy as, this is too simple, but I may be too tired to see what is wrong.

 

In the code below, I have a snippet from a data step. When "LnMonthsToFinalStepCount " is missing, then instead of "Step_indicator = 'Never Step', I keep getting 'Finished Stepping'. What am I missing?

 

Thank you for your help!

 

Code:

        length Step_Indicator $20;
        if LnMonthsToFinalStepCount >= 0 then
            Step_Indicator = 'Currently Stepping';
        else if LnMonthsToFinalStepCount < 0 then
            Step_Indicator = 'Finished Stepping';
        else
            Step_Indicator = 'Never Step';


Accepted Solutions
Solution
‎01-18-2018 06:56 PM
PROC Star
Posts: 1,547

Re: Why "else" doesn't get executed?

[ Edited ]

in sas sort sequence . (missing) is smaller than 0, that's the reason

 

therefore when  LnMonthsToFinalStepCount  is missing the statement if LnMonthsToFinalStepCount < 0  evaluates to true

View solution in original post


All Replies
Solution
‎01-18-2018 06:56 PM
PROC Star
Posts: 1,547

Re: Why "else" doesn't get executed?

[ Edited ]

in sas sort sequence . (missing) is smaller than 0, that's the reason

 

therefore when  LnMonthsToFinalStepCount  is missing the statement if LnMonthsToFinalStepCount < 0  evaluates to true

Contributor
Posts: 39

Re: Why "else" doesn't get executed?

Posted in reply to novinosrin
Thank you very much!
PROC Star
Posts: 1,547

Re: Why "else" doesn't get executed?

if you want else statement to execute, you would have to change the code to

 

Code:

        length Step_Indicator $20;
        if LnMonthsToFinalStepCount >= 0 then
            Step_Indicator = 'Currently Stepping';
        else if not missing(LnMonthsToFinalStepCount ) and  LnMonthsToFinalStepCount < 0 and  then 
            Step_Indicator = 'Finished Stepping';
        else 
            Step_Indicator = 'Never Step';

Super User
Posts: 13,283

Re: Why "else" doesn't get executed?

Posted in reply to novinosrin

 


novinosrin wrote:

if you want else statement to execute, you would have to change the code to

 

Code:

        length Step_Indicator $20;
        if LnMonthsToFinalStepCount >= 0 then
            Step_Indicator = 'Currently Stepping';
        else if not missing(LnMonthsToFinalStepCount ) and  LnMonthsToFinalStepCount < 0 and  then 
            Step_Indicator = 'Finished Stepping';
        else 
            Step_Indicator = 'Never Step';



Shorter

if LnMonthsToFinalStepCount >= 0 then
            Step_Indicator = 'Currently Stepping';
        else if . < LnMonthsToFinalStepCount < 0 then
            Step_Indicator = 'Finished Stepping';
        else
            Step_Indicator = 'Never Step';

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 115 views
  • 0 likes
  • 3 in conversation