Page 1 of 1

Empty strings in if statements

Posted: Wed Dec 19, 2012 5:48 am
by paues
I have come across a rather counter-intuitive behavior in EViews. I presumed (and believe that most users would do the same) that this code

Code: Select all

if %name<>"Fredrik" then @uiprompt("%name is NOT Fredrik") else @uiprompt("%name is Fredrik") endif if %name="Fredrik" then @uiprompt("%name is Fredrik") else @uiprompt("%name is NOT Fredrik") endif
would result in the two dialog boxes both reading "%name is NOT Fredrik". No value has been assigned to the string variable %name, which means it is equal to "", which in turn is not equal to "Fredrik".

However, both of if statements return false and the first dialog box thus reads "%name is Fredrik". In order for the first if statement to return true, in line with my intuition, it needs to be rewritten as

Code: Select all

if @isempty(%name) or %name<>"Fredrik" then @uiprompt("%name is NOT Fredrik") else @uiprompt("%name is Fredrik") endif

Re: Empty strings in if statements

Posted: Wed Dec 19, 2012 8:51 am
by EViews Gareth
As outlined in the manual, you should use @eqna to test for equality of strings, not "=" or "<>".

Re: Empty strings in if statements

Posted: Wed Dec 19, 2012 8:58 am
by paues
I missed that. Thank you!