Macro's and operators

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

Macro's and operators

Alex Rasker
Dear List,

I have encountered some macro behaviour that I don't quite understand.
Perhaps someone has seen this before and knows what's going on.

I would like to use the operator '>' in a macro to make a decision:

DEFINE !mymac ( )
!IF ( !EVAL(!number) < 50 ) !THEN
!CONCAT("Title Small because: 50 > ",!number)
!IFEND
proper
!IF ( (!EVAL(!number) >= 50)  ) !THEN
!CONCAT("Title Large because: 50 < ",!number)
!IFEND

!ENDDEFINE.

DEFINE !number ( ) 45  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 65  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 675  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 175  !ENDDEFINE.
!mymac.

The problem occurs with the last run; SPSS appears to compare 17 to 50
instead of 175 to 50.

I can correct the behaviour by checking for the length of the macro !number
but this does not appear to be decent programming decorum.

Any suggestions are welcome.

Thanks,
Alex

_________________________________________________________________
Play online games with your friends with Messenger
http://www.join.msn.com/messenger/overview
Reply | Threaded
Open this post in threaded view
|

Re: Macro's and operators

Peck, Jon
If you could look up 50 and 175 in a dictionary, which would you expect to come first?

Change your macro test from 50 to 050 and the behavior becomes more obvious,

Macro is a textual expansion engine, not a numerical evaluation system.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Alex Rasker
Sent: Wednesday, July 04, 2007 3:48 PM
To: [hidden email]
Subject: [SPSSX-L] Macro's and operators

Dear List,

I have encountered some macro behaviour that I don't quite understand.
Perhaps someone has seen this before and knows what's going on.

I would like to use the operator '>' in a macro to make a decision:

DEFINE !mymac ( )
!IF ( !EVAL(!number) < 50 ) !THEN
!CONCAT("Title Small because: 50 > ",!number)
!IFEND
proper
!IF ( (!EVAL(!number) >= 50)  ) !THEN
!CONCAT("Title Large because: 50 < ",!number)
!IFEND

!ENDDEFINE.

DEFINE !number ( ) 45  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 65  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 675  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 175  !ENDDEFINE.
!mymac.

The problem occurs with the last run; SPSS appears to compare 17 to 50
instead of 175 to 50.

I can correct the behaviour by checking for the length of the macro !number
but this does not appear to be decent programming decorum.

Any suggestions are welcome.

Thanks,
Alex

_________________________________________________________________
Play online games with your friends with Messenger
http://www.join.msn.com/messenger/overview
Reply | Threaded
Open this post in threaded view
|

Re: Macro's and operators

Albert-Jan Roskam
In reply to this post by Alex Rasker
Hi again,

!EVAL in SPSS is not to be mistaken with %EVAL and
%SYSEVAL in SAS. It's tempting to do so, but...

You attempted to use EVAL in a SASian way, ie. for
arithmatic operations.

Albert-Jan

--- Alex Rasker <[hidden email]> wrote:

> Dear List,
>
> I have encountered some macro behaviour that I don't
> quite understand.
> Perhaps someone has seen this before and knows
> what's going on.
>
> I would like to use the operator '>' in a macro to
> make a decision:
>
> DEFINE !mymac ( )
> !IF ( !EVAL(!number) < 50 ) !THEN
> !CONCAT("Title Small because: 50 > ",!number)
> !IFEND
> proper
> !IF ( (!EVAL(!number) >= 50)  ) !THEN
> !CONCAT("Title Large because: 50 < ",!number)
> !IFEND
>
> !ENDDEFINE.
>
> DEFINE !number ( ) 45  !ENDDEFINE.
> !mymac.
>
> DEFINE !number ( ) 65  !ENDDEFINE.
> !mymac.
>
> DEFINE !number ( ) 675  !ENDDEFINE.
> !mymac.
>
> DEFINE !number ( ) 175  !ENDDEFINE.
> !mymac.
>
> The problem occurs with the last run; SPSS appears
> to compare 17 to 50
> instead of 175 to 50.
>
> I can correct the behaviour by checking for the
> length of the macro !number
> but this does not appear to be decent programming
> decorum.
>
> Any suggestions are welcome.
>
> Thanks,
> Alex
>
>
_________________________________________________________________
> Play online games with your friends with Messenger
> http://www.join.msn.com/messenger/overview
>


Cheers!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Did you know that 87.166253% of all statistics claim a precision of results that is not justified by the method employed? [HELMUT RICHTER]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
Reply | Threaded
Open this post in threaded view
|

Query

Jamie Burnett-3
Hi, could you please help? I am after a system whereby I can take an
excel file and convert it into SPSS and then add in another excel file
that is formatted in exactly the same way, in effect stacking them up
one on top of the other in SPSS.

My excel files have 5 variables in them but I am only interested in two
of them so I was hoping there was some syntax that looped through the
following process

Convert excel file 1 into SPSS keeping only 2 of the 5 variables.
Merge in another excel file with the data in this file stored below the
data from the first excel file in the SPSS file, continue through 122 of
these excel files doing the same operation?

Thanks

Jamie


============================
This e-mail and all attachments it may contain is confidential and intended solely for the use of the individual to whom it is addressed. Any views or opinions presented are solely those of the author and do not necessarily represent those of Ipsos MORI and its associated companies. If you are not the intended recipient, be advised that you have received this e-mail in error and that any use, dissemination, printing, forwarding or copying of this e-mail is strictly prohibited. Please contact the sender if you have received this e-mail in error.

Market & Opinion Research International Ltd , Registered in England and Wales No. 948470 , 79-81 Borough Road , London SE1 1FY, United Kingdom, Email: [hidden email]
============================
Reply | Threaded
Open this post in threaded view
|

Re: Macro's and operators

Alex Rasker
In reply to this post by Alex Rasker
Dear Jon,

The dictionary idea is the reason, fair enough. I also know macro's don't do
arithmatic but if I look under Help for '!IF' though it is stated relational
operators are allowed in macro expressions; including !GT !LT <, > etc. What
exactly is ment then by:
!IF (condition) !THEN [statements]
!ELSE [statements]
!IFEND

I think I'm probably not thinking creatively enough but how do I use an
operator like '<' in a macro-condition without regarding it as a numerical
evaluation system? Is !LT just by its general definition not a numerical
evaluation system (or relational operator) or should I regard the
SPSS-macro-definition of '<' as an operator that comes with a large,
alphabetically ordered, dictionary?

Thanks, Alex
*******************************.

From: "Peck, Jon" <[hidden email]>
Reply-To: "Peck, Jon" <[hidden email]>
To: [hidden email]
Subject: Re: Macro's and operators
Date: Wed, 4 Jul 2007 17:50:21 -0500

If you could look up 50 and 175 in a dictionary, which would you expect to
come first?

Change your macro test from 50 to 050 and the behavior becomes more obvious,

Macro is a textual expansion engine, not a numerical evaluation system.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Alex Rasker
Sent: Wednesday, July 04, 2007 3:48 PM
To: [hidden email]
Subject: [SPSSX-L] Macro's and operators

Dear List,

I have encountered some macro behaviour that I don't quite understand.
Perhaps someone has seen this before and knows what's going on.

I would like to use the operator '>' in a macro to make a decision:

DEFINE !mymac ( )
!IF ( !EVAL(!number) < 50 ) !THEN
!CONCAT("Title Small because: 50 > ",!number)
!IFEND
proper
!IF ( (!EVAL(!number) >= 50)  ) !THEN
!CONCAT("Title Large because: 50 < ",!number)
!IFEND

!ENDDEFINE.

DEFINE !number ( ) 45  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 65  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 675  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 175  !ENDDEFINE.
!mymac.

The problem occurs with the last run; SPSS appears to compare 17 to 50
instead of 175 to 50.

I can correct the behaviour by checking for the length of the macro !number
but this does not appear to be decent programming decorum.

Any suggestions are welcome.

Thanks,
Alex

_________________________________________________________________
Play online games with your friends with Messenger
http://www.join.msn.com/messenger/overview

_________________________________________________________________
Talk with your online friends with Messenger
http://www.join.msn.com/messenger/overview
Reply | Threaded
Open this post in threaded view
|

Re: Macro's and operators

Peck, Jon
Terms in macro expression are strings.  And in conditional expressions they are evaluated as text even if they are meant to be variable names, because the macro is fully expanded and turned into SPSS syntax before any data are referenced.

You have much more power and control if you use Python programmability rather than macros, since there you can control things like expression types and refer to data while still being able to execute SPSS syntax generated by the equivalent of macro logic.

You can even use programmability to define a macro value that can be used later in traditional SPSS syntax.

You can learn about this from the Data Management book, which can be downloaded as a pdf file from SPSS Developer Central (www.spss.com/devcentral) or purchased in hardcopy.  Programmability requires at least SPSS 14 and free downloads from Python.org and Developer Central.

Here is an example from a training class I did last year of converting a macro to Python code.

The macro:
define amacro (numloops !TOKENS(1) !DEFAULT(1)
/weightvar = !TOKENS(1) !DEFAULT("NOT")
/varlist = !CHAREND("/")).
!IF (!weightvar !NE 'NOT') !THEN.
        WEIGHT BY !weightvar.
!IFEND.
!DO !i = 1 !TO !numloops.
        !DO !v !in (!varlist).
                !LET !newname = !CONCAT(!v,!UNQUOTE('_temp')).
                COMPUTE !newname = !v * !v.
                EXAMINE !v !newname/PLOT=NONE.
        !DOEND.
!DOEND.
!ENDDEFINE.

* call the macro a few times.

amacro numloops = 2 varlist = salary salbegin.
amacro numloops = 0 varlist = salary salbegin/ weightvar=jobcat.

The Python equivalent:
BEGIN PROGRAM.
import spss
def afunction(varlist, numloops=1, weightvar=None):
    """square each variable in varlist and run EXAMINE numloops times.
    weight by weightvar if specified."""
    if weightvar:
        spss.Submit("WEIGHT BY %s" % weightvar)
    for i in range(numloops):
        for v in varlist:
            d = {"v" : v, "newname" : v+"_temp"}
            spss.Submit("COMPUTE %(newname)s = %(v)s * %(v)s" % d)
            spss.Submit("EXAMINE %(v)s %(newname)s /PLOT=NONE" % d)
END PROGRAM.

* call the program a few times.
BEGIN PROGRAM.
afunction(numloops=2, varlist=['salary','salbegin'])
afunction(numloops=1, varlist=['salary','salbegin'], weightvar='jobcat')
END PROGRAM.


HTH,
Jon Peck



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Alex Rasker
Sent: Friday, July 06, 2007 6:40 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Macro's and operators

Dear Jon,

The dictionary idea is the reason, fair enough. I also know macro's don't do
arithmatic but if I look under Help for '!IF' though it is stated relational
operators are allowed in macro expressions; including !GT !LT <, > etc. What
exactly is ment then by:
!IF (condition) !THEN [statements]
!ELSE [statements]
!IFEND

I think I'm probably not thinking creatively enough but how do I use an
operator like '<' in a macro-condition without regarding it as a numerical
evaluation system? Is !LT just by its general definition not a numerical
evaluation system (or relational operator) or should I regard the
SPSS-macro-definition of '<' as an operator that comes with a large,
alphabetically ordered, dictionary?

Thanks, Alex
*******************************.

From: "Peck, Jon" <[hidden email]>
Reply-To: "Peck, Jon" <[hidden email]>
To: [hidden email]
Subject: Re: Macro's and operators
Date: Wed, 4 Jul 2007 17:50:21 -0500

If you could look up 50 and 175 in a dictionary, which would you expect to
come first?

Change your macro test from 50 to 050 and the behavior becomes more obvious,

Macro is a textual expansion engine, not a numerical evaluation system.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Alex Rasker
Sent: Wednesday, July 04, 2007 3:48 PM
To: [hidden email]
Subject: [SPSSX-L] Macro's and operators

Dear List,

I have encountered some macro behaviour that I don't quite understand.
Perhaps someone has seen this before and knows what's going on.

I would like to use the operator '>' in a macro to make a decision:

DEFINE !mymac ( )
!IF ( !EVAL(!number) < 50 ) !THEN
!CONCAT("Title Small because: 50 > ",!number)
!IFEND
proper
!IF ( (!EVAL(!number) >= 50)  ) !THEN
!CONCAT("Title Large because: 50 < ",!number)
!IFEND

!ENDDEFINE.

DEFINE !number ( ) 45  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 65  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 675  !ENDDEFINE.
!mymac.

DEFINE !number ( ) 175  !ENDDEFINE.
!mymac.

The problem occurs with the last run; SPSS appears to compare 17 to 50
instead of 175 to 50.

I can correct the behaviour by checking for the length of the macro !number
but this does not appear to be decent programming decorum.

Any suggestions are welcome.

Thanks,
Alex

_________________________________________________________________
Play online games with your friends with Messenger
http://www.join.msn.com/messenger/overview

_________________________________________________________________
Talk with your online friends with Messenger
http://www.join.msn.com/messenger/overview