a strange !IF results within a macro

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

a strange !IF results within a macro

Merav Seifan
Good Morning,
I am trying to create a macro, whose final goal is to preform different
task according to the value of a certain variable within a file (using a
!IF function within the macro).
Unfortunately, I discover some non-rational results while using the !IF
command, that I need help to solve in order to perform the task:
The problem is that I sometime get a wrong result from the !IF (performing
'True' when the argument is false, and wise verse).
Here is the simplified Macro I used:
DEFINE !addingP ()
         GET FILE='D:\test.sav'.
         !IF (1 < 2) !THEN
         Compute x = 1.
         !ELSE
         Compute x = 0.
         !IFEND
!ENDDEFINE.
!addingP.
EXE.

In this example I tried the following combinations:
  1 < 2 (O.K.);  1 < 02 (false result) ; 1 < 20 (O.K.)
10 < 2 (false result) ; 10 < 02 (O.K.) ; 10 < 20 (O.K.)
01 < 2 (O.K.); 01 < 02 (O.K.); 01 < 20 (O.K.).

Any explanations or suggestions will be more than welcome,
Thanks,
Merav

Merav Seifan, PhD
Tuebingen University
Botanical Institute
Plant Ecology Department
Auf der Morgenstelle 3
72076 Tuebingen, Germany
Tel: 07071-297-8814
fax: 07071-295-356
Reply | Threaded
Open this post in threaded view
|

Re: a strange !IF results within a macro

Antoon Smulders
Hello Merav and List

I cannot give you the answer. I hope someone more competent then me will do
that. I thought the two values are somehow considered string values. So
expecting a syntax error because of comparing a string and numeric
"variable", I changed your code into:

compute t = 10.
DEFINE !addingP ()
         !IF (t1 < 1) !THEN
         Compute x = 1.
         !ELSE
         Compute x = 0.
         !IFEND
!ENDDEFINE.
!addingP.
EXE.

But this code runs without syntax errors and gives the unexpected result
that any value for t is smaller then the litteral value of 1 (or any other
number).
Greetings
Antoon Smulders


-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Merav
Seifan
Verzonden: donderdag 9 november 2006 11:19
Aan: [hidden email]
Onderwerp: a strange !IF results within a macro

Good Morning,
I am trying to create a macro, whose final goal is to preform different
task according to the value of a certain variable within a file (using a
!IF function within the macro).
Unfortunately, I discover some non-rational results while using the !IF
command, that I need help to solve in order to perform the task:
The problem is that I sometime get a wrong result from the !IF (performing
'True' when the argument is false, and wise verse).
Here is the simplified Macro I used:
DEFINE !addingP ()
         GET FILE='D:\test.sav'.
         !IF (1 < 2) !THEN
         Compute x = 1.
         !ELSE
         Compute x = 0.
         !IFEND
!ENDDEFINE.
!addingP.
EXE.

In this example I tried the following combinations:
  1 < 2 (O.K.);  1 < 02 (false result) ; 1 < 20 (O.K.)
10 < 2 (false result) ; 10 < 02 (O.K.) ; 10 < 20 (O.K.)
01 < 2 (O.K.); 01 < 02 (O.K.); 01 < 20 (O.K.).

Any explanations or suggestions will be more than welcome,
Thanks,
Merav

Merav Seifan, PhD
Tuebingen University
Botanical Institute
Plant Ecology Department
Auf der Morgenstelle 3
72076 Tuebingen, Germany
Tel: 07071-297-8814
fax: 07071-295-356
Reply | Threaded
Open this post in threaded view
|

Re: a strange !IF results within a macro

Merav Seifan
Good Morning Antoon, Christian and all List members,
I will try to explain a bit more what is bothering me here.
I already checked (as far as my knowledge in SPSS goes) that the Macro can
read the values as numeric, and not string. I even get what I want from
this syntax (which is, of course, a very simplified version of what I
really want to do eventually), but the problem is that I only get the right
result SOMETIMES.
The few number combinations I posted at the end were meant to show when I
got the right answer from the syntax (according to the condition stated in
it), and when I got a wrong answer. I cannot find any systematic way in the
occasions when it is working O.K., and when it is just generating the wrong
answer. Until I will solve that, I cannot trust the !IF condition to work
correctly in my real macro.
Again, Thanks for all the past and future help,
Merav


At 12:30 PM 11/9/2006, you wrote:

>Hello Merav and List
>
>I cannot give you the answer. I hope someone more competent then me will do
>that. I thought the two values are somehow considered string values. So
>expecting a syntax error because of comparing a string and numeric
>"variable", I changed your code into:
>
>compute t = 10.
>DEFINE !addingP ()
>          !IF (t1 < 1) !THEN
>          Compute x = 1.
>          !ELSE
>          Compute x = 0.
>          !IFEND
>!ENDDEFINE.
>!addingP.
>EXE.
>
>But this code runs without syntax errors and gives the unexpected result
>that any value for t is smaller then the litteral value of 1 (or any other
>number).
>Greetings
>Antoon Smulders
>
>
>-----Oorspronkelijk bericht-----
>Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Merav
>Seifan
>Verzonden: donderdag 9 november 2006 11:19
>Aan: [hidden email]
>Onderwerp: a strange !IF results within a macro
>
>Good Morning,
>I am trying to create a macro, whose final goal is to preform different
>task according to the value of a certain variable within a file (using a
>!IF function within the macro).
>Unfortunately, I discover some non-rational results while using the !IF
>command, that I need help to solve in order to perform the task:
>The problem is that I sometime get a wrong result from the !IF (performing
>'True' when the argument is false, and wise verse).
>Here is the simplified Macro I used:
>DEFINE !addingP ()
>          GET FILE='D:\test.sav'.
>          !IF (1 < 2) !THEN
>          Compute x = 1.
>          !ELSE
>          Compute x = 0.
>          !IFEND
>!ENDDEFINE.
>!addingP.
>EXE.
>
>In this example I tried the following combinations:
>   1 < 2 (O.K.);  1 < 02 (false result) ; 1 < 20 (O.K.)
>10 < 2 (false result) ; 10 < 02 (O.K.) ; 10 < 20 (O.K.)
>01 < 2 (O.K.); 01 < 02 (O.K.); 01 < 20 (O.K.).
>
>Any explanations or suggestions will be more than welcome,
>Thanks,
>Merav
>
>Merav Seifan, PhD
>Tuebingen University
>Botanical Institute
>Plant Ecology Department
>Auf der Morgenstelle 3
>72076 Tuebingen, Germany
>Tel: 07071-297-8814
>fax: 07071-295-356

Merav Seifan, PhD
Tuebingen University
Botanical Institute
Plant Ecology Department
Auf der Morgenstelle 3
72076 Tuebingen, Germany
Tel: 07071-297-8814
fax: 07071-295-356
Reply | Threaded
Open this post in threaded view
|

Re: a strange !IF results within a macro

Albert-Jan Roskam
In reply to this post by Antoon Smulders
Hi Merav,

Do you really need those !IF-!THENs, and not the
non-macro language equivalent?

GET FILE='D:\test.sav'.
compute x = 0.
IF (x > 1) y = 1.

Dunno, hard to say from your information.

Maybe enclose the x < y in !EVAL, as in !EVAL(x<y)?

Albert-Jan

--- Antoon Smulders <[hidden email]> wrote:

> Hello Merav and List
>
> I cannot give you the answer. I hope someone more
> competent then me will do
> that. I thought the two values are somehow
> considered string values. So
> expecting a syntax error because of comparing a
> string and numeric
> "variable", I changed your code into:
>
> compute t = 10.
> DEFINE !addingP ()
>          !IF (t1 < 1) !THEN
>          Compute x = 1.
>          !ELSE
>          Compute x = 0.
>          !IFEND
> !ENDDEFINE.
> !addingP.
> EXE.
>
> But this code runs without syntax errors and gives
> the unexpected result
> that any value for t is smaller then the litteral
> value of 1 (or any other
> number).
> Greetings
> Antoon Smulders
>
>
> -----Oorspronkelijk bericht-----
> Van: SPSSX(r) Discussion
> [mailto:[hidden email]] Namens Merav
> Seifan
> Verzonden: donderdag 9 november 2006 11:19
> Aan: [hidden email]
> Onderwerp: a strange !IF results within a macro
>
> Good Morning,
> I am trying to create a macro, whose final goal is
> to preform different
> task according to the value of a certain variable
> within a file (using a
> !IF function within the macro).
> Unfortunately, I discover some non-rational results
> while using the !IF
> command, that I need help to solve in order to
> perform the task:
> The problem is that I sometime get a wrong result
> from the !IF (performing
> 'True' when the argument is false, and wise verse).
> Here is the simplified Macro I used:
> DEFINE !addingP ()
>          GET FILE='D:\test.sav'.
>          !IF (1 < 2) !THEN
>          Compute x = 1.
>          !ELSE
>          Compute x = 0.
>          !IFEND
> !ENDDEFINE.
> !addingP.
> EXE.
>
> In this example I tried the following combinations:
>   1 < 2 (O.K.);  1 < 02 (false result) ; 1 < 20
> (O.K.)
> 10 < 2 (false result) ; 10 < 02 (O.K.) ; 10 < 20
> (O.K.)
> 01 < 2 (O.K.); 01 < 02 (O.K.); 01 < 20 (O.K.).
>
> Any explanations or suggestions will be more than
> welcome,
> Thanks,
> Merav
>
> Merav Seifan, PhD
> Tuebingen University
> Botanical Institute
> Plant Ecology Department
> Auf der Morgenstelle 3
> 72076 Tuebingen, Germany
> Tel: 07071-297-8814
> fax: 07071-295-356
>





____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail.
http://new.mail.yahoo.com
Reply | Threaded
Open this post in threaded view
|

AW: a strange !IF results within a macro

la volta statistics
In reply to this post by Merav Seifan
Merva,
You can't do direct arithmetics in macros.
An assignments such as
let !x = !a + !b generate an error.

But try the following:
Hope this helps, Christian



DATA LIST LIST /v1 v2 V3 V4.
BEGIN DATA.
1 1 1 1
1 2 3 4
END DATA.
Save OUtfile = "D:\temp\test.sav".

DEFINE !addingP ()
          GET FILE='D:\temp\test.sav'.
!LET !a = 10
!LET !b = 020

          !IF (!length(!concat(!blanks(!a)," ")) <
!length(!concat(!blanks(!b)," "))) !THEN
                  Compute x = 1.
          !ELSE
            Compute x = 0.
          !IFEND
!ENDDEFINE.
!addingP.
EXE.




-----Ursprungliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]]Im Auftrag von
Merav Seifan
Gesendet: Donnerstag, 9. November 2006 12:50
An: [hidden email]
Betreff: Re: a strange !IF results within a macro


Good Morning Antoon, Christian and all List members,
I will try to explain a bit more what is bothering me here.
I already checked (as far as my knowledge in SPSS goes) that the Macro can
read the values as numeric, and not string. I even get what I want from
this syntax (which is, of course, a very simplified version of what I
really want to do eventually), but the problem is that I only get the right
result SOMETIMES.
The few number combinations I posted at the end were meant to show when I
got the right answer from the syntax (according to the condition stated in
it), and when I got a wrong answer. I cannot find any systematic way in the
occasions when it is working O.K., and when it is just generating the wrong
answer. Until I will solve that, I cannot trust the !IF condition to work
correctly in my real macro.
Again, Thanks for all the past and future help,
Merav


At 12:30 PM 11/9/2006, you wrote:

>Hello Merav and List
>
>I cannot give you the answer. I hope someone more competent then me will do
>that. I thought the two values are somehow considered string values. So
>expecting a syntax error because of comparing a string and numeric
>"variable", I changed your code into:
>
>compute t = 10.
>DEFINE !addingP ()
>          !IF (t1 < 1) !THEN
>          Compute x = 1.
>          !ELSE
>          Compute x = 0.
>          !IFEND
>!ENDDEFINE.
>!addingP.
>EXE.
>
>But this code runs without syntax errors and gives the unexpected result
>that any value for t is smaller then the litteral value of 1 (or any other
>number).
>Greetings
>Antoon Smulders
>
>
>-----Oorspronkelijk bericht-----
>Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Merav
>Seifan
>Verzonden: donderdag 9 november 2006 11:19
>Aan: [hidden email]
>Onderwerp: a strange !IF results within a macro
>
>Good Morning,
>I am trying to create a macro, whose final goal is to preform different
>task according to the value of a certain variable within a file (using a
>!IF function within the macro).
>Unfortunately, I discover some non-rational results while using the !IF
>command, that I need help to solve in order to perform the task:
>The problem is that I sometime get a wrong result from the !IF (performing
>'True' when the argument is false, and wise verse).
>Here is the simplified Macro I used:
>DEFINE !addingP ()
>          GET FILE='D:\test.sav'.
>          !IF (1 < 2) !THEN
>          Compute x = 1.
>          !ELSE
>          Compute x = 0.
>          !IFEND
>!ENDDEFINE.
>!addingP.
>EXE.
>
>In this example I tried the following combinations:
>   1 < 2 (O.K.);  1 < 02 (false result) ; 1 < 20 (O.K.)
>10 < 2 (false result) ; 10 < 02 (O.K.) ; 10 < 20 (O.K.)
>01 < 2 (O.K.); 01 < 02 (O.K.); 01 < 20 (O.K.).
>
>Any explanations or suggestions will be more than welcome,
>Thanks,
>Merav
>
>Merav Seifan, PhD
>Tuebingen University
>Botanical Institute
>Plant Ecology Department
>Auf der Morgenstelle 3
>72076 Tuebingen, Germany
>Tel: 07071-297-8814
>fax: 07071-295-356

Merav Seifan, PhD
Tuebingen University
Botanical Institute
Plant Ecology Department
Auf der Morgenstelle 3
72076 Tuebingen, Germany
Tel: 07071-297-8814
fax: 07071-295-356
Reply | Threaded
Open this post in threaded view
|

Re: a strange !IF results within a macro

Richard Ristow
In reply to this post by Merav Seifan
At 05:19 AM 11/9/2006, Merav Seifan wrote:

You've had good advice, but this is the central problem:

>I am trying to create a macro, whose final goal is to preform
>different task according to the value of a certain variable within a
>file (using a !IF function within the macro).

That can't work. Macros are expanded, and turned into plain SPSS
syntax, before files are read. "!IF" never sees the values within a
file. It is used to control the macro expansion - the SPSS code that
the macro generates - by testing macro arguments and macro variables.

In addition,

>I discover some non-rational results while using the !IF command:
>[...]
>In this example I tried the following combinations:
>  1 < 2 (O.K.);  1 < 02 (false result) ; 1 < 20 (O.K.)
>10 < 2 (false result) ; 10 < 02 (O.K.) ; 10 < 20 (O.K.)
>01 < 2 (O.K.); 01 < 02 (O.K.); 01 < 20 (O.K.).

You'll see that these are valid STRING comparisons. It's true in
regular SPSS as well:
1<2 (numeric), but '02'>'1' (string).
(You shouldn't have found "10 < 02". Did you enter that wrong?)

Macro values are strings, and are compared that way.

Post again, with what you're trying to do; it isn't clear enough from
your example, to make suggestions.

-Good luck,
  Richard