INCLUDE within DEFINE-!ENDDEFINE?

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

INCLUDE within DEFINE-!ENDDEFINE?

Peters Gj (PSYCHOLOGY)
Dear list,

I have an increasing number of syntax files with macro definitions to
analyse a (by my standards large and complex :-)) datafile.
I find that there are some commands I repeat in most macros (let's call
the commands 'dummy definitions' for the sake of easy explanation (of
course, a completely arbitrary name :-))). I now have to change 'dummy
definitions' in all macros. Ideally, I would now switch to a more
flexible way of structuring the macros: either including the same file
in all macros and have the 'dummy definitions' in that file, so that
'dummy definitions' can be changed fast and I am certain that all macros
run exactly the same commands, or define another macro which 'executes'
the 'dummy definitions', with the same purpose.

However, neither seems to work; the 'dummy definitions' commands have
two features which seem to prohibit this:

1) they use macro variables filled with (by?) arguments of the macros
   eventually using them, therefore not defined within the 'dummy
   definitions' sequence;
2) they use macro commands

When including the file in the macro definition, the macro is defined
without problems; however, upon execution SPSS provides this rather
scary response:

  >Error # 1 on line 18176.  Command name: !IF
  >The first word in the line is not recognized as an SPSS command.
  >This command not executed.

  >Note # 214 on line 18176.  Command name: !IF
  >Due to an error, INCLUDE file processing has been terminated and the
  >working file has been lost.  You may either redefine your data or
leave
  >SPSS.

  >Note # 236
  >All outstanding macros have been terminated, all include processing
has
  >been terminated, and all outstanding PRESERVE commands have been
undone.

When trying to define a macro using macro variables that do not yet
exist when the macro is defines, for example like this:

  DEFINE MCR_test ()
  !LET !whateverYouNameThis = !QUOTE(!thisVariableDoesNotExist).
  !ENDDEFINE.

SPSS reacts thus:

  >Error # 6843 in column 37.  Text: !thisVariableDoesNotExist
  >The end of a macro expression occurred when an operand was expected.
  >This command not executed.

Am I missing something here? Am I doomed to work with elaborate search &
replaces in all the relevant SPSS files?

Thanks a lot in advance!!!

Kind regards,

Gjalt-Jorn
________________________________________________________________________
____
Gjalt-Jorn Ygram Peters

## Phd. Student                            ## Visit:
   Department of Experimental Psychology      Room 3.004
   Faculty of Psychology                      Universiteitssingel 5
   University of Maastricht                   6229 ES Maastricht
                                              The Netherlands
## Contact:
   P.O. Box 616                            ## Private:
   6200 MD Maastricht                         Alexander Battalaan 68d
   The Netherlands                            6221 CE Maastricht
                                              The Netherlands
   Phone: +31 43 388 4508
   Fax:   +31 43 388 4211                     Phone: +31 (0) 62 120 0009
                                              Mail:  [hidden email]
   Mail: [hidden email]      Msn:   [hidden email]
   Web:  http://interventionmapping.nl        Web:   http://www.gjyp.nl
Reply | Threaded
Open this post in threaded view
|

Re: INCLUDE within DEFINE-!ENDDEFINE?

Simon Phillip Freidin
Instead of using INCLUDE use INSERT

Including the file requires that the spss program meets syntax
restriction requirements.
So code that runs okay interactively fails when included.

By default INSERT allows interactive coding.

(The line with !if is indented without an indent-permitted character
in the first column of the line (a full-stop or dash).)

cheers
Simon



On 21/06/2006, at 7:42 PM, Peters Gj (PSYCHOLOGY) wrote:

> Dear list,
>
> I have an increasing number of syntax files with macro definitions to
> analyse a (by my standards large and complex :-)) datafile.
> I find that there are some commands I repeat in most macros (let's
> call
> the commands 'dummy definitions' for the sake of easy explanation (of
> course, a completely arbitrary name :-))). I now have to change 'dummy
> definitions' in all macros. Ideally, I would now switch to a more
> flexible way of structuring the macros: either including the same file
> in all macros and have the 'dummy definitions' in that file, so that
> 'dummy definitions' can be changed fast and I am certain that all
> macros
> run exactly the same commands, or define another macro which
> 'executes'
> the 'dummy definitions', with the same purpose.
>
> However, neither seems to work; the 'dummy definitions' commands have
> two features which seem to prohibit this:
>
> 1) they use macro variables filled with (by?) arguments of the macros
>    eventually using them, therefore not defined within the 'dummy
>    definitions' sequence;
> 2) they use macro commands
>
> When including the file in the macro definition, the macro is defined
> without problems; however, upon execution SPSS provides this rather
> scary response:
>
>> Error # 1 on line 18176.  Command name: !IF
>> The first word in the line is not recognized as an SPSS command.
>> This command not executed.
>
>> Note # 214 on line 18176.  Command name: !IF
>> Due to an error, INCLUDE file processing has been terminated and the
>> working file has been lost.  You may either redefine your data or
> leave
>> SPSS.
>
>> Note # 236
>> All outstanding macros have been terminated, all include processing
> has
>> been terminated, and all outstanding PRESERVE commands have been
> undone.
>
> When trying to define a macro using macro variables that do not yet
> exist when the macro is defines, for example like this:
>
>   DEFINE MCR_test ()
>   !LET !whateverYouNameThis = !QUOTE(!thisVariableDoesNotExist).
>   !ENDDEFINE.
>
> SPSS reacts thus:
>
>> Error # 6843 in column 37.  Text: !thisVariableDoesNotExist
>> The end of a macro expression occurred when an operand was expected.
>> This command not executed.
>
> Am I missing something here? Am I doomed to work with elaborate
> search &
> replaces in all the relevant SPSS files?
>
> Thanks a lot in advance!!!
>
> Kind regards,
>
> Gjalt-Jorn
> ______________________________________________________________________
> __
> ____
> Gjalt-Jorn Ygram Peters
>
> ## Phd. Student                            ## Visit:
>    Department of Experimental Psychology      Room 3.004
>    Faculty of Psychology                      Universiteitssingel 5
>    University of Maastricht                   6229 ES Maastricht
>                                               The Netherlands
> ## Contact:
>    P.O. Box 616                            ## Private:
>    6200 MD Maastricht                         Alexander Battalaan 68d
>    The Netherlands                            6221 CE Maastricht
>                                               The Netherlands
>    Phone: +31 43 388 4508
>    Fax:   +31 43 388 4211                     Phone: +31 (0) 62 120
> 0009
>                                               Mail:  [hidden email]
>    Mail: [hidden email]      Msn:   [hidden email]
>    Web:  http://interventionmapping.nl        Web:   http://
> www.gjyp.nl
Reply | Threaded
Open this post in threaded view
|

Re: INCLUDE within DEFINE-!ENDDEFINE?

Peters Gj (PSYCHOLOGY)
Dear Simon and other list-members,

My apologies: I fear that I forgot to mention my version of SPSS . . .
It's version 12.

INSERT is not included in the syntax manual of SPSS 12, not does SPSS
seem to recognize it as a command. Our university still uses only 12 :-(

Do you know of a solution that works in SPSS 12?

Thanks again!!!
All the best,

Gjalt-Jorn
________________________________________________________________________
____
Gjalt-Jorn Ygram Peters

## Phd. Student                            ## Visit:
   Department of Experimental Psychology      Room 3.004
   Faculty of Psychology                      Universiteitssingel 5
   University of Maastricht                   6229 ES Maastricht
                                              The Netherlands
## Contact:
   P.O. Box 616                            ## Private:
   6200 MD Maastricht                         Alexander Battalaan 68d
   The Netherlands                            6221 CE Maastricht
                                              The Netherlands
   Phone: +31 43 388 4508
   Fax:   +31 43 388 4211                     Phone: +31 (0) 62 120 0009
                                              Mail:  [hidden email]
   Mail: [hidden email]      Msn:   [hidden email]
   Web:  http://interventionmapping.nl        Web:   http://www.gjyp.nl
________________________________________________________________________
____

-----Original Message-----
From: Simon Freidin [mailto:[hidden email]]
Sent: woensdag 21 juni 2006 13:10
To: Peters Gj (PSYCHOLOGY)
Cc: [hidden email]
Subject: Re: INCLUDE within DEFINE-!ENDDEFINE?

Instead of using INCLUDE use INSERT

Including the file requires that the spss program meets syntax
restriction requirements.
So code that runs okay interactively fails when included.

By default INSERT allows interactive coding.

(The line with !if is indented without an indent-permitted character
in the first column of the line (a full-stop or dash).)

cheers
Simon



On 21/06/2006, at 7:42 PM, Peters Gj (PSYCHOLOGY) wrote:

> Dear list,
>
> I have an increasing number of syntax files with macro definitions to
> analyse a (by my standards large and complex :-)) datafile.
> I find that there are some commands I repeat in most macros (let's
> call
> the commands 'dummy definitions' for the sake of easy explanation (of
> course, a completely arbitrary name :-))). I now have to change 'dummy
> definitions' in all macros. Ideally, I would now switch to a more
> flexible way of structuring the macros: either including the same file
> in all macros and have the 'dummy definitions' in that file, so that
> 'dummy definitions' can be changed fast and I am certain that all
> macros
> run exactly the same commands, or define another macro which
> 'executes'
> the 'dummy definitions', with the same purpose.
>
> However, neither seems to work; the 'dummy definitions' commands have
> two features which seem to prohibit this:
>
> 1) they use macro variables filled with (by?) arguments of the macros
>    eventually using them, therefore not defined within the 'dummy
>    definitions' sequence;
> 2) they use macro commands
>
> When including the file in the macro definition, the macro is defined
> without problems; however, upon execution SPSS provides this rather
> scary response:
>
>> Error # 1 on line 18176.  Command name: !IF
>> The first word in the line is not recognized as an SPSS command.
>> This command not executed.
>
>> Note # 214 on line 18176.  Command name: !IF
>> Due to an error, INCLUDE file processing has been terminated and the
>> working file has been lost.  You may either redefine your data or
> leave
>> SPSS.
>
>> Note # 236
>> All outstanding macros have been terminated, all include processing
> has
>> been terminated, and all outstanding PRESERVE commands have been
> undone.
>
> When trying to define a macro using macro variables that do not yet
> exist when the macro is defines, for example like this:
>
>   DEFINE MCR_test ()
>   !LET !whateverYouNameThis = !QUOTE(!thisVariableDoesNotExist).
>   !ENDDEFINE.
>
> SPSS reacts thus:
>
>> Error # 6843 in column 37.  Text: !thisVariableDoesNotExist
>> The end of a macro expression occurred when an operand was expected.
>> This command not executed.
>
> Am I missing something here? Am I doomed to work with elaborate
> search &
> replaces in all the relevant SPSS files?
>
> Thanks a lot in advance!!!
>
> Kind regards,
>
> Gjalt-Jorn
> ______________________________________________________________________

> __
> ____
> Gjalt-Jorn Ygram Peters
>
> ## Phd. Student                            ## Visit:
>    Department of Experimental Psychology      Room 3.004
>    Faculty of Psychology                      Universiteitssingel 5
>    University of Maastricht                   6229 ES Maastricht
>                                               The Netherlands
> ## Contact:
>    P.O. Box 616                            ## Private:
>    6200 MD Maastricht                         Alexander Battalaan 68d
>    The Netherlands                            6221 CE Maastricht
>                                               The Netherlands
>    Phone: +31 43 388 4508
>    Fax:   +31 43 388 4211                     Phone: +31 (0) 62 120
> 0009
>                                               Mail:  [hidden email]
>    Mail: [hidden email]      Msn:   [hidden email]
>    Web:  http://interventionmapping.nl        Web:   http://
> www.gjyp.nl
Reply | Threaded
Open this post in threaded view
|

Re: INCLUDE within DEFINE-!ENDDEFINE?

Richard Ristow
In reply to this post by Peters Gj (PSYCHOLOGY)
This will be partly tentative, without seeing your code, but, at 05:42
AM 6/21/2006, Peters Gj (PSYCHOLOGY) wrote:

>I have an increasing number of syntax files with macro definitions. I
>find that there are some commands I repeat in most macros (let's call
>the commands 'dummy definitions'). I now have to change 'dummy
>definitions' in all macros. Ideally, I would now switch to a more
>flexible way of structuring the macros: either including the same file
>in all macros and have the 'dummy definitions' in that file, so that
>'dummy definitions' can be changed fast and I am certain that all
>macros run exactly the same commands, or define another macro which
>'executes' the 'dummy definitions'.

FIRST, why "INCLUDE" doesn't work for you:

>When including the file in the macro definition, the macro is defined
>without problems[, or anyhow, without error messages]; however, upon
>execution SPSS provides this rather scary response:
>
>   >Error # 1 on line 18176.  Command name: !IF
>   >The first word in the line is not recognized as an SPSS command.
>   >This command not executed.

This is easy to be confused about, when using INCLUDE with macros; it's
come up on the list before. (For reference, see threads "problem with
insert inserting a macro-command", Thu, 1 Sep 2005 13:59:57 +0200; "A
Macro question: how to INCLUDE !let commands?", Fri, 5 Mar 2004
16:28:53 +0100.)

INCLUDE is an SPSS statement. When it's within a macro definition
(DEFINE-!ENDDEFINE), the INCLUDE statement, not the INCLUDEd text,
becomes part of the definition. Then the expanded macro code includes
the INCLUDE; when the expanded code is executed, *then* the INCLUDEd
text is inserted in the SPSS code stream. But since the code at that
point is not within a macro definition, macro commands like !IF aren't
recognized as such; SPSS itself tries to recognize them, and of course
fails; and you get that error. Run with SET MPRINT ON and it will
probably be clearer. (By the way, whenever a macro gives strange
results, do a test run with SET MPRINT ON.)

Of course, if the INCLUDEd code were plain SPSS code without macro
features, you'd get the same results either way. But your code is true
macro code:

>1) [It uses] macro variables filled with (by?) arguments of the macros
>eventually using them, therefore not defined within the 'dummy
>definitions' sequence;
>2) [It uses] macro commands

INCLUDEd text can contain complete macro definitions, but not parts of
macro definitions. (INSERT is probably the same.) That's where that
error came from, but it  doesn't solve your problem.

..............
SECOND, invoking a macro within another macro ("define another macro
which 'executes' the 'dummy definitions'.") is probably what you want
to do. Be aware:

When you invoke a macro inside another macro, the invoked macro is only
expanded and executed if you use the !EVAL macro directive. As an
example, see the following little utility to print the expansions of
macro:

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

Now, if you define your 'dummy definitions' as a macro,

>2) they use macro commands

should not be a problem - macro commands within a macro.

>1) they use macro variables filled with (by?) arguments of the macros
>eventually using them, therefore not defined within the 'dummy
>definitions' sequence;

The manual is not clear whether an inner macro invoked by !EVAL, can
use macro variables defined in the outer, calling macro. Try it, but I
think it probably can't.

You probably need to write your 'dummy definitions' macro so that the
arguments of the outer macro are passed as explicit arguments to the
'dummy definitions' macro.

I think this is the right approach. Like everything else having to do
with macros, it will probably give you some problems; and I'm afraid I
don't have time to write test code and work out the glitches, now. See
what you can do, and feel free to post back.

Remember the two debugging tools for macros:

A. Run with SET MPRINT ON.
B. Use ECHO to print values during macro expansion - to display value
of variable !FILE, use

ECHO !QUOTE(!FILE).

For examples of both, see my posting "Re: Can't !LET & VAR LABELS be
used in DO IF [structures]?", Tue, 13 Jun 2006 16:46:09 -0400.