A little Python help

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

A little Python help

Jignesh Sutar

How do I get the below syntax to work...?

DATASET CLOSE ALL.
NEW FILE.
OUTPUT CLOSE ALL.
GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'.
SET PRINTBACK ON MPRINT ON.
BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern=r"s")
for v in vardict:
spss.Submit("""
TITLE %(v)s.
TITLE %(v.VariableLabel)s.
""" % locals())
END PROGRAM.
SET PRINTBACK ON MPRINT OFF.

One solution would be to use the below but the actual code is much longer and so I prefer to stick with using the 'locals' function.

BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern=r"s")
for v in vardict:
spss.Submit("""
TITLE %s.
TITLE %s.
""" % (v, v.VariableLabel))
END PROGRAM.

TIA
Jignesh
Reply | Threaded
Open this post in threaded view
|

Re: A little Python help

Peck, Jon
Try changing the for to
For v in vardict.variables:

Otherwise you get a variable object where a string is required. I should add a method to the variable object to allow that conversion to work automatically.

HTH,
Jon Peck


From: SPSSX(r) Discussion <[hidden email]>
To: [hidden email] <[hidden email]>
Sent: Fri Jun 19 05:21:28 2009
Subject: [SPSSX-L] A little Python help


How do I get the below syntax to work...?

DATASET CLOSE ALL.
NEW FILE.
OUTPUT CLOSE ALL.
GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'.
SET PRINTBACK ON MPRINT ON.
BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern=r"s")
for v in vardict:
spss.Submit("""
TITLE %(v)s.
TITLE %(v.VariableLabel)s.
""" % locals())
END PROGRAM.
SET PRINTBACK ON MPRINT OFF.

One solution would be to use the below but the actual code is much longer and so I prefer to stick with using the 'locals' function.

BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern=r"s")
for v in vardict:
spss.Submit("""
TITLE %s.
TITLE %s.
""" % (v, v.VariableLabel))
END PROGRAM.

TIA
Jignesh
Reply | Threaded
Open this post in threaded view
|

Re: A little Python help

Jignesh Sutar
Thanks Jon

I tried that but it still gives me variable names? Also, note in the 'for' loop I will require v to be variables names on some occasions and variable labels on others.

Is there a way to do this?

Cheers
Jignesh

2009/6/19 Peck, Jon <[hidden email]>
Try changing the for to
For v in vardict.variables:

Otherwise you get a variable object where a string is required. I should add a method to the variable object to allow that conversion to work automatically.

HTH,
Jon Peck


From: SPSSX(r) Discussion <[hidden email]>
To: [hidden email] <[hidden email]>
Sent: Fri Jun 19 05:21:28 2009
Subject: [SPSSX-L] A little Python help


How do I get the below syntax to work...?

DATASET CLOSE ALL.
NEW FILE.
OUTPUT CLOSE ALL.
GET FILE='C:\Program Files\SPSSInc\Statistics17\Samples\English\employee data.sav'.
SET PRINTBACK ON MPRINT ON.
BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern=r"s")
for v in vardict:
spss.Submit("""
TITLE %(v)s.
TITLE %(v.VariableLabel)s.
""" % locals())
END PROGRAM.
SET PRINTBACK ON MPRINT OFF.

One solution would be to use the below but the actual code is much longer and so I prefer to stick with using the 'locals' function.

BEGIN PROGRAM.
import spss, spssaux
vardict=spssaux.VariableDict(pattern=r"s")
for v in vardict:
spss.Submit("""
TITLE %s.
TITLE %s.
""" % (v, v.VariableLabel))
END PROGRAM.

TIA
Jignesh