Python syntax applied to SPSS data.

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

Python syntax applied to SPSS data.

geffris
hi I'm trying to understand Python syntax logic applied to SPSS.

I will give a very simple example: it's a simple do if in SPSS, but I cannot figure it out how to obtain the same results using Python

data list list / month (A3) value.
begin data
"Jan" 3
"Jan" 2
"Feb" 11
end data.

*what I do in SPSS.

compute target=0.
do if month="Jan" and value<3.
compute target=1.
end if.
execute.

*trial in Python option 1.

begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables


def my_comp(var):
    target=0
    if  month=="Jan" and var<3:
        return target+1

my_comp (value)
end program.


*trial in Python option 2.

begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables


def my_comp():
    target=0
    if  month=="Jan" and value<3:
        return target+1
end program.
Reply | Threaded
Open this post in threaded view
|

Re: Python syntax applied to SPSS data.

Jon Peck
You can, of course, run regular Statistics syntax from a Python program.  You might do this because you need to generate the syntax based on variable metadata or other logic.  You would do this with the spss.Submit api.  If this approach works, it will be the fastest, because it would not be necessary to pass the data from the Statistics backend to the Python process and back again.

If you want to apply transformations written in Python because, perhaps, Python provides a capability not conveniently available in the built-in transformation system, you can use the SPSSINC TRANS extension command to apply your function.  This eliminates the need to write Python code to define variables and pass the data.  TRANS is installed with the Python Essentials.

If you want to do everything in Python without using TRANS, you need to write the Python code to read the data from SPSS and write the results back.  In the simplest case, where you need to read the data but don't need to write it back, the spssdata.Spssdata class is the easiest.  For example,

begin program.
import spss, spssdata

cursor = spssdata.Spssdata('x')
sum = 0
for case in cursor:
    sum += case[0]
cursor.CClose()
print sum
end program.

If you want to do full transformations, see the spss.Dataset class.

If you haven't downloaded the Programming and Data Management book from the Community website, please do that.  You will find lots of useful examples there.  Of course, the Python help available from the Help menu also has examples.



On Thu, Apr 21, 2016 at 4:07 AM, geffris <[hidden email]> wrote:
hi I'm trying to understand Python syntax logic applied to SPSS.

I will give a very simple example: it's a simple do if in SPSS, but I cannot
figure it out how to obtain the same results using Python

data list list / month (A3) value.
begin data
"Jan" 3
"Jan" 2
"Feb" 11
end data.

*what I do in SPSS.

compute target=0.
do if month="Jan" and value<3.
compute target=1.
end if.
execute.

*trial in Python option 1.

begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables


def my_comp(var):
    target=0
    if  month=="Jan" and var<3:
        return target+1

my_comp (value)
end program.


*trial in Python option 2.

begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables


def my_comp():
    target=0
    if  month=="Jan" and value<3:
        return target+1
end program.




--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Python-syntax-applied-to-SPSS-data-tp5731981.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD