oddity?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

oddity?

Maguin, Eugene

A student wrote and executed an if statement like this.

 

if (a or b or c or d eq 1) y=1.

execute.

 

The syntax processor issued warnings, not errors, and correctly computed a value for y. I was stunned!

 

I subsequently made up some data and ran the above statements and … correct computation but no warnings (both versions are 21.0.0.1).

 

I looked at the documentation and in what I think is the applicable statement it says:

 

A relation can compare variables, constants, or more complicated arithmetic expressions.

Relations cannot be abbreviated. For example,(A EQ 2 OR A EQ 5)is valid, while (A

EQ 2 OR 5)is not. Blanks (not commas) must be used to separate relational operators from

the expressions being compared.

 

So, I ran this statement.

 

if (a eq 1 or 3) y=2.

execute.

 

It computed correctly and no warnings or errors were issued.

 

While all the above seems like it should fail to execute with a cascade of error statements, I’ve noticed from the work of others over the years that my interpretations of syntax rules are far too limited and inflexible. Perhaps this another example of that.

 

Gene Maguin

Reply | Threaded
Open this post in threaded view
|

oddity? addendum

Maguin, Eugene

I said:

 

So, I ran this statement.

 

if (a eq 1 or 3) y=2.

execute.

 

It computed correctly and no warnings or errors were issued.

 

The last sentence was incorrect. It should have read

 

It computed correctly and warnings but no errors were issued.

 

Gene Maguin

Reply | Threaded
Open this post in threaded view
|

Re: oddity?

Jon K Peck
In reply to this post by Maguin, Eugene
This, from the CSR, may help

The simplest logical expression is a logical variable. A logical variable is any numeric variable that has
the values 1, 0, or system-missing. Logical variables cannot be strings.

Note also that for examples such as below, the any function can be very helpful.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        "Maguin, Eugene" <[hidden email]>
To:        [hidden email],
Date:        03/18/2014 09:04 AM
Subject:        [SPSSX-L] oddity?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




A student wrote and executed an if statement like this.
 
if (a or b or c or d eq 1) y=1.
execute.
 
The syntax processor issued warnings, not errors, and correctly computed a value for y. I was stunned!
 
I subsequently made up some data and ran the above statements and … correct computation but no warnings (both versions are 21.0.0.1).
 
I looked at the documentation and in what I think is the applicable statement it says:
 
A relation can compare variables, constants, or more complicated arithmetic expressions.
Relations cannot be abbreviated. For example,(A EQ 2 OR A EQ 5)is valid, while (A
EQ 2 OR 5)is not. Blanks (not commas) must be used to separate relational operators from
the expressions being compared.
 
So, I ran this statement.
 
if (a eq 1 or 3) y=2.
execute.
 
It computed correctly and no warnings or errors were issued.
 
While all the above seems like it should fail to execute with a cascade of error statements, I’ve noticed from the work of others over the years that my interpretations of syntax rules are far too limited and inflexible. Perhaps this another example of that.
 
Gene Maguin
Reply | Threaded
Open this post in threaded view
|

Re: oddity? addendum

Salbod
In reply to this post by Maguin, Eugene

SPSS 22

 

I created a file with a (=1,2,3,1,2,3) and y (=1) variables.

 

I ran:

 

if (a eq 1 or 3) y=2.

execute.

 

Got this:

 

Warning # 532

During execution of the indicated command, one of the operands of AND or OR

had other than a valid logical value.  The valid logical values are 0, 1, and

missing.  The invalid value has been treated as a system missing value.

Command line: 24  Current case: 1  Current splitfile group: 1

 

Bottom line: y was changed to 2 at x values of 1 and 3.

 

  

--Steve

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Maguin, Eugene
Sent: Tuesday, March 18, 2014 11:07 AM
To: [hidden email]
Subject: oddity? addendum

 

I said:

 

So, I ran this statement.

 

if (a eq 1 or 3) y=2.

execute.

 

It computed correctly and no warnings or errors were issued.

 

The last sentence was incorrect. It should have read

 

It computed correctly and warnings but no errors were issued.

 

Gene Maguin

Reply | Threaded
Open this post in threaded view
|

Re: oddity? addendum

Bruce Weaver
Administrator
In reply to this post by Maguin, Eugene
Gene, I think it would help readers to understand the issue if you provided a small data set that illustrates what you mean by "it computed correctly".  For example, you say the following IF was computed correctly:

if (a eq 1 or 3) y=2.
execute.

Does that mean it was computed the same as:

IF ANY(a,1,3) y=2.

Finally, here is some more info from the UNIVERSALS section of the FM that may be useful.

* When arithmetic operators and functions are used in a logical expression, the order of
operations is functions and arithmetic operations first, then relational operators, and then
logical operators.

* When more than one logical operator is used, NOT is evaluated first, then AND, and then OR.

* To change the order of evaluation, use parentheses.

HTH.


Maguin, Eugene wrote
I said:

So, I ran this statement.

if (a eq 1 or 3) y=2.
execute.

It computed correctly and no warnings or errors were issued.

The last sentence was incorrect. It should have read

It computed correctly and warnings but no errors were issued.

Gene Maguin
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: oddity?

Rick Oliver-3
In reply to this post by Maguin, Eugene
This may shed a little light on the issue:

data list list /x y.
begin data
1 1
2 1
3 2
4 2
5 3
end data.
if x=1 or x=2 z1=1.
if x=1 or 2 z2=1.
if x=1 or  x=2 or y=3 z3=1.
if x=1 or 2 or y=2 z4=1.
list.


Rick Oliver
Senior Information Developer
IBM Business Analytics (SPSS)
E-mail: [hidden email]




From:        "Maguin, Eugene" <[hidden email]>
To:        [hidden email],
Date:        03/18/2014 10:11 AM
Subject:        oddity?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




A student wrote and executed an if statement like this.
 
if (a or b or c or d eq 1) y=1.
execute.
 
The syntax processor issued warnings, not errors, and correctly computed a value for y. I was stunned!
 
I subsequently made up some data and ran the above statements and … correct computation but no warnings (both versions are 21.0.0.1).
 
I looked at the documentation and in what I think is the applicable statement it says:
 
A relation can compare variables, constants, or more complicated arithmetic expressions.
Relations cannot be abbreviated. For example,(A EQ 2 OR A EQ 5)is valid, while (A
EQ 2 OR 5)is not. Blanks (not commas) must be used to separate relational operators from
the expressions being compared.
 
So, I ran this statement.
 
if (a eq 1 or 3) y=2.
execute.
 
It computed correctly and no warnings or errors were issued.
 
While all the above seems like it should fail to execute with a cascade of error statements, I’ve noticed from the work of others over the years that my interpretations of syntax rules are far too limited and inflexible. Perhaps this another example of that.
 
Gene Maguin
Reply | Threaded
Open this post in threaded view
|

Re: oddity?

Bruce Weaver
Administrator
In reply to this post by Maguin, Eugene
I'd like to come back to Gene's first example below.  Many SPSS users seem unaware of the fact that for variables that can take values of 1 and 0 only, one can treat them the same way one treats boolean variables in some programming languages.  I.e., if a, b, c and d are all such variables, then "IF a" is equivalent to "IF a EQ 1".  For example:

DATA LIST list / a b c d (4f1).
BEGIN DATA
0 0 0 0
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
END DATA.

IF (a or b or c or d eq 1) Y1=1.
IF (a eq 1 or b eq 1 or c eq 1 or d eq 1) Y2=1.
IF (a or b or c or d) Y3 = 1.
IF ANY(1,a,b,c,d) Y4 = 1.
FORMATS Y1 to Y4(f1).
LIST.

OUTPUT:

a b c d Y1 Y2 Y3 Y4
 
0 0 0 0  .  .  .  .
1 0 0 0  1  1  1  1
0 1 0 0  1  1  1  1
0 0 1 0  1  1  1  1
0 0 0 1  1  1  1  1
 
Number of cases read:  5    Number of cases listed:  5

If one uses good variable names in conjunction with this feature of 1-0 "boolean" variables, one can generate nice readable code.  E.g.,

COMPUTE ConditionIsMet = { fill in some conditions here }.

Then, later on...

TEMPORARY.
SELECT IF ConditionIsMet.
etc.

Note that you do not have to say "ConditionIsMet EQ 1".  

HTH.


Maguin, Eugene wrote
A student wrote and executed an if statement like this.

if (a or b or c or d eq 1) y=1.
execute.

The syntax processor issued warnings, not errors, and correctly computed a value for y. I was stunned!

I subsequently made up some data and ran the above statements and ... correct computation but no warnings (both versions are 21.0.0.1).

I looked at the documentation and in what I think is the applicable statement it says:

A relation can compare variables, constants, or more complicated arithmetic expressions.
Relations cannot be abbreviated. For example,(A EQ 2 OR A EQ 5)is valid, while (A
EQ 2 OR 5)is not. Blanks (not commas) must be used to separate relational operators from
the expressions being compared.

So, I ran this statement.

if (a eq 1 or 3) y=2.
execute.

It computed correctly and no warnings or errors were issued.

While all the above seems like it should fail to execute with a cascade of error statements, I've noticed from the work of others over the years that my interpretations of syntax rules are far too limited and inflexible. Perhaps this another example of that.

Gene Maguin
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: oddity? addendum

Paul Cook
In reply to this post by Salbod
The expression "a eq 1 or 3" is evaluated as "(a eq 1) or 3". 

The value "3" is not a valid logical value.

I suspect the expression you really want is "a eq 1 or a eq 3", which can also be written as "any(a, 1, 3)"

Kind regards,

Paul Cook



On 19 March 2014 04:23, Salbod, Mr. Stephen <[hidden email]> wrote:

SPSS 22

 

I created a file with a (=1,2,3,1,2,3) and y (=1) variables.

 

I ran:

 

if (a eq 1 or 3) y=2.

execute.

 

Got this:

 

Warning # 532

During execution of the indicated command, one of the operands of AND or OR

had other than a valid logical value.  The valid logical values are 0, 1, and

missing.  The invalid value has been treated as a system missing value.

Command line: 24  Current case: 1  Current splitfile group: 1

 

Bottom line: y was changed to 2 at x values of 1 and 3.

 

  

--Steve

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Maguin, Eugene
Sent: Tuesday, March 18, 2014 11:07 AM
To: [hidden email]
Subject: oddity? addendum

 

I said:

 

So, I ran this statement.

 

if (a eq 1 or 3) y=2.

execute.

 

It computed correctly and no warnings or errors were issued.

 

The last sentence was incorrect. It should have read

 

It computed correctly and warnings but no errors were issued.

 

Gene Maguin