Use of ANY function inside a macro

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

Use of ANY function inside a macro

Julien Mostard
Hi,

I get an error when trying to use the ANY function inside a SPSS macro. I
defined the following macro:

DEFINE !select (var1=!CMDEND/var2=!CMDEND)
SELECT IF (ANY(lessor,!var1,!var2).
!ENDDEFINE.

I call it with the following command sequence.
DEFINE !lessor_n1 () "610" !ENDDEFINE.
DEFINE !lessor_n2 () "602" !ENDDEFINE.
!select !lessor_n1 !lessor_n2.
EXEC.

This results in an error message:
>Error # 4024 in column 24.  Text: ,
>The sequence of operators found is invalid.  Check the expression for
>omitted or extra operands, operators, and parentheses.
>This command not executed.

If I define the macro on just 1 variable (so without the ANY function), it
works. Can anybody explain what is wrong in the above macro definition?

Best regards,

Julien
Reply | Threaded
Open this post in threaded view
|

Re: Use of ANY function inside a macro

Julien Mostard
There is a typo in the second line of the macro, there should of course be
an extra bracket: SELECT IF (ANY(lessor,!var1,!var2)). However, that does
not solve the problem.


On Wed, 6 Jun 2007 13:00:59 -0400, Julien Mostard <[hidden email]>
wrote:

>Hi,
>
>I get an error when trying to use the ANY function inside a SPSS macro. I
>defined the following macro:
>
>DEFINE !select (var1=!CMDEND/var2=!CMDEND)
>SELECT IF (ANY(lessor,!var1,!var2).
>!ENDDEFINE.
>
>I call it with the following command sequence.
>DEFINE !lessor_n1 () "610" !ENDDEFINE.
>DEFINE !lessor_n2 () "602" !ENDDEFINE.
>!select !lessor_n1 !lessor_n2.
>EXEC.
>
>This results in an error message:
>>Error # 4024 in column 24.  Text: ,
>>The sequence of operators found is invalid.  Check the expression for
>>omitted or extra operands, operators, and parentheses.
>>This command not executed.
>
>If I define the macro on just 1 variable (so without the ANY function), it
>works. Can anybody explain what is wrong in the above macro definition?
>
>Best regards,
>
>Julien
Reply | Threaded
Open this post in threaded view
|

AW: Use of ANY function inside a macro

la volta statistics
In reply to this post by Julien Mostard
Hi Julien
This is one of the few SPSS error messages which is not that cryptic to
understand.
You omitted a closing parentheses in the line:
SELECT IF (ANY(lessor,!var1,!var2).
should be:
SELECT IF (ANY(lessor,!var1,!var2)).

hope this helps,
Christian

-----Ursprüngliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]]Im Auftrag von
Julien Mostard
Gesendet: Mittwoch, 6. Juni 2007 19:01
An: [hidden email]
Betreff: Use of ANY function inside a macro


Hi,

I get an error when trying to use the ANY function inside a SPSS macro. I
defined the following macro:

DEFINE !select (var1=!CMDEND/var2=!CMDEND)
SELECT IF (ANY(lessor,!var1,!var2).
!ENDDEFINE.

I call it with the following command sequence.
DEFINE !lessor_n1 () "610" !ENDDEFINE.
DEFINE !lessor_n2 () "602" !ENDDEFINE.
!select !lessor_n1 !lessor_n2.
EXEC.

This results in an error message:
>Error # 4024 in column 24.  Text: ,
>The sequence of operators found is invalid.  Check the expression for
>omitted or extra operands, operators, and parentheses.
>This command not executed.

If I define the macro on just 1 variable (so without the ANY function), it
works. Can anybody explain what is wrong in the above macro definition?

Best regards,

Julien
Reply | Threaded
Open this post in threaded view
|

Re: Use of ANY function inside a macro

Richard Ristow
In reply to this post by Julien Mostard
At 01:00 PM 6/6/2007, Julien Mostard wrote:

>I get an error when trying to use the ANY function inside a SPSS
>macro.

First, some recommendations for posting a question like this.

. If you write macros, be familiar with MPRINT, and use it. MPRINT
lists the code lines the macro generates, which is a *huge* help in
debugging. MPRINT causes listing of *all* code lines, so it causes
double-listing outside a macro, which is usually a nuisance. I
recommend this form:

PRESERVE.
SET MPRINT ON.
!select !lessor_n1 !lessor_n2.
RESTORE.

Often, you'll be able to see your problem from the output. If you
can't, give the listing you get when you run this way, in your posting
to the list.

. When you write something like

>If I define the macro on just 1 variable (so without the ANY
>function), it works.

give us the definition of that macro. I *think* you changed
significantly more than that, but we don't know what. If we had the
definition for this macro, we could probably help with what went wrong
with the changes.

Anyway, I think you have three problems, plus a possible fourth. (See
Appendix I., giving the fixes sequentially, and resulting error
messages.)

a.) You defined your macro arguments 'var1' and 'var2' as keyword
arguments:

DEFINE !select (var1=!CMDEND/var2=!CMDEND)

but you invoked the macro as if they were positional arguments:

!select !lessor_n1 !lessor_n2.

b.) You defined your macro arguments 'var1' and 'var2' using !CMDEND.
So all text on the line after 'var1=' became part of the value of
!var1, and !var2 got no text at all.

c.) You had a missing right parenthesis on your SELECT IF statement.

d.) If variable 'lessor' is numeric (as in my test data), macros
!lessor_n1 and !lessor_n2 need to return values not in quotes.
......................
So, here's the fix:
List
|-----------------------------|---------------------------|
|Output Created               |07-JUN-2007 02:36:32       |
|-----------------------------|---------------------------|
SERIAL Greek    lessor

     1  Alpha      600
     2  Beta       610
     3  Gamma      602
     4  Delta      603


Number of cases read:  4    Number of cases listed:  4
[...]

DEFINE !select (var1=!TOKENS(1)/var2=!TOKENS(1))
SELECT IF (ANY(lessor,!var1,!var2)).
!ENDDEFINE.

DEFINE !lessor_n1 ()  610  !ENDDEFINE.
DEFINE !lessor_n2 ()  602  !ENDDEFINE.


PRESERVE.
SET MPRINT ON.
!select var1=!lessor_n1 var2=!lessor_n2.
  492 M>  SELECT IF (ANY(lessor, 610 , 602 ))
  493 M>  .
RESTORE.
  494 M>  RESTORE.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-JUN-2007 02:36:34       |
|-----------------------------|---------------------------|
SERIAL Greek    lessor

     2  Beta       610
     3  Gamma      602

Number of cases read:  2    Number of cases listed:  2
==================================================
APPENDIX I:  Complete listing, with error messages
==================================================
|-----------------------------|---------------------------|
|Output Created               |07-JUN-2007 02:36:32       |
|-----------------------------|---------------------------|
SERIAL Greek    lessor

     1  Alpha      600
     2  Beta       610
     3  Gamma      602
     4  Delta      603


Number of cases read:  4    Number of cases listed:  4


*  Macros used as arguments, when testing      ....... .

DEFINE !lessor_n1 () "610" !ENDDEFINE.
DEFINE !lessor_n2 () "602" !ENDDEFINE.

*  Macro as originally posted:                 ....... .

*  "I defined the following macro:"                    .

DEFINE !select (var1=!CMDEND/var2=!CMDEND)
SELECT IF (ANY(lessor,!var1,!var2).
!ENDDEFINE.

*  "I call it with the following command sequence."    .

PRESERVE.
SET MPRINT ON.
!select !lessor_n1 !lessor_n2.
  432 M>  SELECT IF (ANY(lessor, , )
  433 M>   '610'
  434 M>   '602'
  435 M>  .

 >Error # 4024 in column 24.  Text: ,
 >The sequence of operators found is invalid.  Check the expression for
 >omitted or extra operands, operators, and parentheses.
 >This command not executed.

RESTORE.
  436 M>  RESTORE.

EXECUTE /* LIST is not illuminating here */ .

*  First, you have KEYWORD arguments, and must specify .
*  the arguments with the keywords when you invoke the .
*  macro (see DEFINE documentation):                   .

PRESERVE.
SET MPRINT ON.
!select var1=!lessor_n1 var2=!lessor_n2.
  446 M>  SELECT IF (ANY(lessor, '610' var2='602' , )
  447 M>  .

 >Error # 4007 in column 30.  Text: var2
 >The expression is incomplete.  Check for missing operands, invalid
 >operators, unmatched parentheses or excessive string length.
 >This command not executed.

RESTORE.
  448 M>  RESTORE.

EXECUTE /* LIST is not illuminating here */ .

*  That's closer, but you've defined your arguments    .
*  using CMDEND. So, the value of var1 is everyting up .
*  to the end of the command:                          .
*  /*  !lessor_n1 var2=!lessor_n2                   */ .
*  which expands to                                    .
*      '610' var2='602'                                .
*  Use "tokens" instead:                               .

*  (AND you had a missing right parenthesis in your    .
*  SELECT IF statement.)                               .



DEFINE !select (var1=!TOKENS(1)/var2=!TOKENS(1))
SELECT IF (ANY(lessor,!var1,!var2)).
!ENDDEFINE.


PRESERVE.
SET MPRINT ON.
!select var1=!lessor_n1 var2=!lessor_n2.
  473 M>  SELECT IF (ANY(lessor, '610' , '602' ))
  474 M>  .

 >Error # 4325 in column 38.  Text: )
 >The arguments to the ANY and RANGE functions must be either all
strings (or
 >string expressions) or all numeric variables (or numeric
expressions).
 >This command not executed.

RESTORE.
  475 M>  RESTORE.

EXECUTE /* LIST is not illuminating here */ .


*  And, at least in my test data, 'lessor' is numeric, .
*  so the macros used as arguments should expand to    .
*  UNquoted values 610 and 602:                        .

DEFINE !lessor_n1 ()  610  !ENDDEFINE.
DEFINE !lessor_n2 ()  602  !ENDDEFINE.


PRESERVE.
SET MPRINT ON.
!select var1=!lessor_n1 var2=!lessor_n2.
  492 M>  SELECT IF (ANY(lessor, 610 , 602 ))
  493 M>  .
RESTORE.
  494 M>  RESTORE.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-JUN-2007 02:36:34       |
|-----------------------------|---------------------------|
SERIAL Greek    lessor

     2  Beta       610
     3  Gamma      602

Number of cases read:  2    Number of cases listed:  2

======================
APPENDIX II  Test data
======================
*  ................................................................. .
*  .................   Utility macro           ..................... .

*  Macro !MacEcho is used to display a macro expansion   ........... .
DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
    ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
    ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
!ENDDEFINE.

*  .................   Test data               ..................... .

DATA LIST FIXED
    /SERIAL  01-03
     Greek   04-11 (A)
     lessor  13-15.
BEGIN DATA
1  Alpha    600
2  Beta     610
3  Gamma    602
4  Delta    603
END DATA.
*  .................   Post after this point   ..................... .
*  ................................................................. .
LIST.


>Hi,
>
>DEFINE !select (var1=!CMDEND/var2=!CMDEND)
>SELECT IF (ANY(lessor,!var1,!var2).
>!ENDDEFINE.
>
>I call it with the following command sequence.
>DEFINE !lessor_n1 () "610" !ENDDEFINE.
>DEFINE !lessor_n2 () "602" !ENDDEFINE.
>!select !lessor_n1 !lessor_n2.
>EXEC.
>
>This results in an error message:
> >Error # 4024 in column 24.  Text: ,
> >The sequence of operators found is invalid.  Check the expression
> for
> >omitted or extra operands, operators, and parentheses.
> >This command not executed.
>
>If I define the macro on just 1 variable (so without the ANY
>function), it
>works. Can anybody explain what is wrong in the above macro
>definition?
>
>Best regards,
>
>Julien
>
>
>
>--
>No virus found in this incoming message.
>Checked by AVG Free Edition.
>Version: 7.5.472 / Virus Database: 269.8.9/834 - Release Date:
>6/5/2007 2:38 PM
Reply | Threaded
Open this post in threaded view
|

Re: Use of ANY function inside a macro

Julien Mostard
Thank you very much for your detailed answer, it was very helpful!

On 07/06/07, Richard Ristow <[hidden email]> wrote:

> At 01:00 PM 6/6/2007, Julien Mostard wrote:
>
> >I get an error when trying to use the ANY function inside a SPSS
> >macro.
>
> First, some recommendations for posting a question like this.
>
> . If you write macros, be familiar with MPRINT, and use it. MPRINT
> lists the code lines the macro generates, which is a *huge* help in
> debugging. MPRINT causes listing of *all* code lines, so it causes
> double-listing outside a macro, which is usually a nuisance. I
> recommend this form:
>
> PRESERVE.
> SET MPRINT ON.
> !select !lessor_n1 !lessor_n2.
> RESTORE.
>
> Often, you'll be able to see your problem from the output. If you
> can't, give the listing you get when you run this way, in your posting
> to the list.
>
> . When you write something like
>
> >If I define the macro on just 1 variable (so without the ANY
> >function), it works.
>
> give us the definition of that macro. I *think* you changed
> significantly more than that, but we don't know what. If we had the
> definition for this macro, we could probably help with what went wrong
> with the changes.
>
> Anyway, I think you have three problems, plus a possible fourth. (See
> Appendix I., giving the fixes sequentially, and resulting error
> messages.)
>
> a.) You defined your macro arguments 'var1' and 'var2' as keyword
> arguments:
>
> DEFINE !select (var1=!CMDEND/var2=!CMDEND)
>
> but you invoked the macro as if they were positional arguments:
>
> !select !lessor_n1 !lessor_n2.
>
> b.) You defined your macro arguments 'var1' and 'var2' using !CMDEND.
> So all text on the line after 'var1=' became part of the value of
> !var1, and !var2 got no text at all.
>
> c.) You had a missing right parenthesis on your SELECT IF statement.
>
> d.) If variable 'lessor' is numeric (as in my test data), macros
> !lessor_n1 and !lessor_n2 need to return values not in quotes.
> ......................
> So, here's the fix:
> List
> |-----------------------------|---------------------------|
> |Output Created               |07-JUN-2007 02:36:32       |
> |-----------------------------|---------------------------|
> SERIAL Greek    lessor
>
>      1  Alpha      600
>      2  Beta       610
>      3  Gamma      602
>      4  Delta      603
>
>
> Number of cases read:  4    Number of cases listed:  4
> [...]
>
> DEFINE !select (var1=!TOKENS(1)/var2=!TOKENS(1))
> SELECT IF (ANY(lessor,!var1,!var2)).
> !ENDDEFINE.
>
> DEFINE !lessor_n1 ()  610  !ENDDEFINE.
> DEFINE !lessor_n2 ()  602  !ENDDEFINE.
>
>
> PRESERVE.
> SET MPRINT ON.
> !select var1=!lessor_n1 var2=!lessor_n2.
>   492 M>  SELECT IF (ANY(lessor, 610 , 602 ))
>   493 M>  .
> RESTORE.
>   494 M>  RESTORE.
>
> LIST.
>
> List
> |-----------------------------|---------------------------|
> |Output Created               |07-JUN-2007 02:36:34       |
> |-----------------------------|---------------------------|
> SERIAL Greek    lessor
>
>      2  Beta       610
>      3  Gamma      602
>
> Number of cases read:  2    Number of cases listed:  2
> ==================================================
> APPENDIX I:  Complete listing, with error messages
> ==================================================
> |-----------------------------|---------------------------|
> |Output Created               |07-JUN-2007 02:36:32       |
> |-----------------------------|---------------------------|
> SERIAL Greek    lessor
>
>      1  Alpha      600
>      2  Beta       610
>      3  Gamma      602
>      4  Delta      603
>
>
> Number of cases read:  4    Number of cases listed:  4
>
>
> *  Macros used as arguments, when testing      ....... .
>
> DEFINE !lessor_n1 () "610" !ENDDEFINE.
> DEFINE !lessor_n2 () "602" !ENDDEFINE.
>
> *  Macro as originally posted:                 ....... .
>
> *  "I defined the following macro:"                    .
>
> DEFINE !select (var1=!CMDEND/var2=!CMDEND)
> SELECT IF (ANY(lessor,!var1,!var2).
> !ENDDEFINE.
>
> *  "I call it with the following command sequence."    .
>
> PRESERVE.
> SET MPRINT ON.
> !select !lessor_n1 !lessor_n2.
>   432 M>  SELECT IF (ANY(lessor, , )
>   433 M>   '610'
>   434 M>   '602'
>   435 M>  .
>
>  >Error # 4024 in column 24.  Text: ,
>  >The sequence of operators found is invalid.  Check the expression for
>  >omitted or extra operands, operators, and parentheses.
>  >This command not executed.
>
> RESTORE.
>   436 M>  RESTORE.
>
> EXECUTE /* LIST is not illuminating here */ .
>
> *  First, you have KEYWORD arguments, and must specify .
> *  the arguments with the keywords when you invoke the .
> *  macro (see DEFINE documentation):                   .
>
> PRESERVE.
> SET MPRINT ON.
> !select var1=!lessor_n1 var2=!lessor_n2.
>   446 M>  SELECT IF (ANY(lessor, '610' var2='602' , )
>   447 M>  .
>
>  >Error # 4007 in column 30.  Text: var2
>  >The expression is incomplete.  Check for missing operands, invalid
>  >operators, unmatched parentheses or excessive string length.
>  >This command not executed.
>
> RESTORE.
>   448 M>  RESTORE.
>
> EXECUTE /* LIST is not illuminating here */ .
>
> *  That's closer, but you've defined your arguments    .
> *  using CMDEND. So, the value of var1 is everyting up .
> *  to the end of the command:                          .
> *  /*  !lessor_n1 var2=!lessor_n2                   */ .
> *  which expands to                                    .
> *      '610' var2='602'                                .
> *  Use "tokens" instead:                               .
>
> *  (AND you had a missing right parenthesis in your    .
> *  SELECT IF statement.)                               .
>
>
>
> DEFINE !select (var1=!TOKENS(1)/var2=!TOKENS(1))
> SELECT IF (ANY(lessor,!var1,!var2)).
> !ENDDEFINE.
>
>
> PRESERVE.
> SET MPRINT ON.
> !select var1=!lessor_n1 var2=!lessor_n2.
>   473 M>  SELECT IF (ANY(lessor, '610' , '602' ))
>   474 M>  .
>
>  >Error # 4325 in column 38.  Text: )
>  >The arguments to the ANY and RANGE functions must be either all
> strings (or
>  >string expressions) or all numeric variables (or numeric
> expressions).
>  >This command not executed.
>
> RESTORE.
>   475 M>  RESTORE.
>
> EXECUTE /* LIST is not illuminating here */ .
>
>
> *  And, at least in my test data, 'lessor' is numeric, .
> *  so the macros used as arguments should expand to    .
> *  UNquoted values 610 and 602:                        .
>
> DEFINE !lessor_n1 ()  610  !ENDDEFINE.
> DEFINE !lessor_n2 ()  602  !ENDDEFINE.
>
>
> PRESERVE.
> SET MPRINT ON.
> !select var1=!lessor_n1 var2=!lessor_n2.
>   492 M>  SELECT IF (ANY(lessor, 610 , 602 ))
>   493 M>  .
> RESTORE.
>   494 M>  RESTORE.
>
> LIST.
>
> List
> |-----------------------------|---------------------------|
> |Output Created               |07-JUN-2007 02:36:34       |
> |-----------------------------|---------------------------|
> SERIAL Greek    lessor
>
>      2  Beta       610
>      3  Gamma      602
>
> Number of cases read:  2    Number of cases listed:  2
>
> ======================
> APPENDIX II  Test data
> ======================
> *  ................................................................. .
> *  .................   Utility macro           ..................... .
>
> *  Macro !MacEcho is used to display a macro expansion   ........... .
> DEFINE !MacEcho(!POS !NOEXPAND !CMDEND)
>     ECHO  !QUOTE(!CONCAT('     Call  : ',!1)).
>     ECHO  !QUOTE(!CONCAT('     Result: ',!EVAL(!1))).
> !ENDDEFINE.
>
> *  .................   Test data               ..................... .
>
> DATA LIST FIXED
>     /SERIAL  01-03
>      Greek   04-11 (A)
>      lessor  13-15.
> BEGIN DATA
> 1  Alpha    600
> 2  Beta     610
> 3  Gamma    602
> 4  Delta    603
> END DATA.
> *  .................   Post after this point   ..................... .
> *  ................................................................. .
> LIST.
>
>
> >Hi,
> >
> >DEFINE !select (var1=!CMDEND/var2=!CMDEND)
> >SELECT IF (ANY(lessor,!var1,!var2).
> >!ENDDEFINE.
> >
> >I call it with the following command sequence.
> >DEFINE !lessor_n1 () "610" !ENDDEFINE.
> >DEFINE !lessor_n2 () "602" !ENDDEFINE.
> >!select !lessor_n1 !lessor_n2.
> >EXEC.
> >
> >This results in an error message:
> > >Error # 4024 in column 24.  Text: ,
> > >The sequence of operators found is invalid.  Check the expression
> > for
> > >omitted or extra operands, operators, and parentheses.
> > >This command not executed.
> >
> >If I define the macro on just 1 variable (so without the ANY
> >function), it
> >works. Can anybody explain what is wrong in the above macro
> >definition?
> >
> >Best regards,
> >
> >Julien
> >
> >
> >
> >--
> >No virus found in this incoming message.
> >Checked by AVG Free Edition.
> >Version: 7.5.472 / Virus Database: 269.8.9/834 - Release Date:
> >6/5/2007 2:38 PM
>
>