Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I preserve double-quotes when a macro is expanded?

51 views
Skip to first unread message

Bruce Weaver

unread,
Feb 22, 2007, 2:21:07 PM2/22/07
to
I am trying to enclose a GGRAPH/GPL command in a macro. For GGRAPH and
GPL, it is essential that one use double-quotes. However, even though I
use double-quotes in my macro definition, they become single-quotes when
the macro is expanded. E.g., here is a macro definition:

* -------- Start of macro definition -------- .

DEFINE !splot () .

* Chart Builder.
GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=weight mpg cylinder MISSING=
LISTWISE REPORTMISSING=NO
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: weight=col(source(s), name("weight"))
DATA: mpg=col(source(s), name("mpg"))
DATA: !id=col(source(s), name("cylinder"), unit.category())
SCALE: linear(dim(2), include(0))
COORD: rect(dim(1,2),wrap())
GUIDE: axis(dim(1), label("Vehicle Weight (lbs.)"))
GUIDE: axis(dim(2), label("Miles per Gallon"))
GUIDE: axis(dim(3), label("Number of Cylinders"), opposite())
ELEMENT: point(position(weight*mpg*cylinder))
ELEMENT: line(position(smooth.linear(weight*mpg)))
END GPL.

!ENDDEFINE.

* -------- End of macro definition -------- .

When I call the macro, the /GRAPHDATASET line expands to:

498 M> GGRAPH /GRAPHDATASET NAME='graphdataset' VARIABLES=weight mpg
cylinder
MISSING= LISTWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE.

And this causes the macro to stop dead in its tracks, of course, because
double-quotes are required.

A Google search revealed that this question has been raised at least
once before (message ID given below), but no solution was found at that
time.

Message ID of the older thread asking about this:

1111429176.2...@o13g2000cwo.googlegroups.com

Does anyone have any bright ideas? Would it be easier to do this with
Python, perhaps?

Thanks,
Bruce

p.s. - I am using the Cars data file in the main SPSS folder, in case
anyone wants to try it out for themselves.

GET FILE='C:\Program Files\SPSS\Cars.sav'.

--
Bruce Weaver
bwe...@lakeheadu.ca
www.angelfire.com/wv/bwhomedir

Ben Pfaff

unread,
Feb 22, 2007, 3:19:46 PM2/22/07
to
Bruce Weaver <bwe...@lakeheadu.ca> writes:

> I am trying to enclose a GGRAPH/GPL command in a macro. For GGRAPH
> and GPL, it is essential that one use double-quotes. However, even
> though I use double-quotes in my macro definition, they become
> single-quotes when the macro is expanded. E.g., here is a macro
> definition:

[...]

> Does anyone have any bright ideas? Would it be easier to do this with
> Python, perhaps?

Seeing as your macro does not take any arguments, how about
moving its contents into a separate file and, each time you want
to invoke it, write INCLUDE FILE='myfile'.
--
Ben Pfaff
b...@cs.stanford.edu
http://benpfaff.org

Bruce Weaver

unread,
Feb 22, 2007, 4:04:17 PM2/22/07
to


Ben, thanks for the reply. The actual macro does have arguments,
however. (I was trying to keep things relatively uncomplicated for
posting here.)

Let me give a little more context. Imagine a data file with several
rows per ID. We want to run a simple linear regression for each ID, and
use the slopes in another analysis. It would be nice to visually
inspect scatterplots for each ID to reassure ourselves that a linear
relationship (as opposed to quadratic, etc) is sensible. We could split
file by ID and use the classic scatterplot, but then we'd have to add
fit lines manually via the chart editor. I know it is possible to get
what we want via Interactive Graphics, but am just curious about how to
do it with GPL, since that seems to be the way things are heading.

So, with that in mind, here's the *real* macro and call:

* -------- Start of macro definition -------- .

DEFINE !splot ( split = !TOKENS(1) /
first = !TOKENS(1) /
last = !TOKENS(1) ) .


temporary.
select if range(!split,!first,!last).
* Chart Builder.
GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=weight mpg !split MISSING=


LISTWISE REPORTMISSING=NO
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: weight=col(source(s), name("weight"))
DATA: mpg=col(source(s), name("mpg"))

DATA: !split=col(source(s), name("!split"), unit.category())


SCALE: linear(dim(2), include(0))
COORD: rect(dim(1,2),wrap())
GUIDE: axis(dim(1), label("Vehicle Weight (lbs.)"))
GUIDE: axis(dim(2), label("Miles per Gallon"))

GUIDE: axis(dim(3), label("Split Variable"), opposite())
ELEMENT: point(position(weight*mpg*!split))
ELEMENT: line(position(smooth.linear(weight*mpg)))
ELEMENT: line(position(smooth.quadratic(weight*mpg)))
END GPL.

!ENDDEFINE.

* -------- End of macro definition -------- .

set mprint on.
!splot split = cylinder
first = 3
last = 5 .
.
set mprint off.

As noted already, the problem is that double-quotes in the macro
definition become single-quotes when it expands.


Funny you should mention INCLUDE (or INSERT), because I too was thinking
that might provide a reasonably good work-around. Here's what I came up
with.

1. I recoded CYLINDER (the variable I want to split on) into another
grouping variable such that 1 through 5 cylinders are in one group and 6
through 8 in another group. (In my real application, I'd be creating
groups if IDs rather than cylinders.)

2. Split file by that cylinder (or ID) grouping variable.

3. INSERT a file that contains the GGRAPH/GPL code.

Here's the syntax:

* ------------------------------------------------------- .
recode cylinder
(1 thru 5 = 1)
(6 thru 8 = 2) into cylgrp.
exe.

sort cases by cylinder.
split file by cylgrp.
insert file = !syntax + "\GPL\inc GPL scatterplot 01.sps" .
split file off.
* ------------------------------------------------------- .

The syntax is reasonably compact and understandable, it produces the
desired output, and the GGRAPH/GPL code only appears once in the output
window. It works for me!

Cheers,
Bruce

Mike P

unread,
Feb 23, 2007, 4:50:11 AM2/23/07
to
Hi Bruce,

I ran into a similar problem, the problem comes that GGraph cannot be
incorporated in a macro, i was trying to add different titles into the
GGraph command

i got around the problem by calling a macro and passing an argument
(the title), which in turns calls a script file, again passing the
title as a parameter, which in turns opens a new syntax file, runs the
syntax and then closes it. long winded i know!!!

A better solution though untested would be to use python, set up a
list and then use the spss.Submit command line similar to, but this is
untested and i don't know if it will work, just an idea

begin program.
import spss,
varList = ['arg1', 'arg2','etc etc']
for i in varList:
spss_filter = '''you gpl code.''' % (i)
end program.

Hth

Mike


JKPeck

unread,
Feb 24, 2007, 9:49:55 AM2/24/07
to

Doubtless it is better to do this through programmability. You can
see an example of constructing GPL via programmability in the
paretochart module on SPSS Developer Central (www.spss.com/
devcentral), where we combined Monte Carlo simulation and GPL
graphics.

But you can get the macro to behave by forcing the double quoted form.
Here is an example.

define !dbl(!positional=!tokens(1)) !concat('"', !unquote(!1), '"') !
enddefine.


SOURCE: s=userSource(id(!dbl "graphdataset")).

The !dbl macro returns its argument in double quotes, so embedding it
where quotes are needed in GPL can be done as illustrated in the
SOURCE statement.

HTH,
Jon Peck

Bruce Weaver

unread,
Feb 24, 2007, 11:12:02 AM2/24/07
to
JKPeck wrote:

> Doubtless it is better to do this through programmability. You can
> see an example of constructing GPL via programmability in the
> paretochart module on SPSS Developer Central (www.spss.com/
> devcentral), where we combined Monte Carlo simulation and GPL
> graphics.
>
> But you can get the macro to behave by forcing the double quoted form.
> Here is an example.
>
> define !dbl(!positional=!tokens(1)) !concat('"', !unquote(!1), '"') !
> enddefine.
>
>
> SOURCE: s=userSource(id(!dbl "graphdataset")).
>
> The !dbl macro returns its argument in double quotes, so embedding it
> where quotes are needed in GPL can be done as illustrated in the
> SOURCE statement.
>
> HTH,
> Jon Peck
>

Thanks Jon.

0 new messages