need help for some modification of Jon Peck's python program

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

need help for some modification of Jon Peck's python program

xiaoqin.wan@gmail.com
Dear all,

Jon Peck worte the following small python program which works fine!

*######################################.

get file='D:/formulas.sav'.

begin program.

import spss, spssdata

formula={}

curs = spssdata.Spssdata()

for case in curs:

formula[case.var] = case.coeff

curs.CClose()

spss.Submit("get file='D:/main.sav'")

cmd = "COMPUTE newvar = " + "+".join([n.strip() + "*" + \

str(c) for n,c in formula.items()])

print cmd

spss.Submit(cmd)

end program.

*#########################################.

The file formulas.sav looks like:

var coeff

var1  0.20
var2  0.30
var5  0.50
And the file main.sav looks like:

var1 var2 var3 var4 var5 var6
1,00 2,00 3,00 6,00 8,00 10,00
8,00 9,00 14,00 15,00 16,00 5,00

Thís program create automatically a new variable newvar in the file main.sav,
according to the formular (depending on the file strucure of formulas.sav):

var5*0.5+var2*0.3+var1*0.2
Now I have 10 files formulas1.sav, formulas2.sav, .....formulas10.sav and
one file main.sav

formulas1.sav looks like

var coeff

var1  0.20
var2  0.30
var5  0.50

formulas2.sav looks like

var coeff

var2  0.10
var6  0.80
var3  0.60
and so on ...

I would like to create 10 new variables newvar1, .... newvar10 in the file
main.sav, with the above formula:

newvar1 = var5*0.5+var2*0.3+var1*0.2.
newvar2 = var2*0.1+var6*0.8+var3*0.6.

....

How can I do that? with a loop?

Thanks for your help in advance!

baiyun



2007/9/27, [hidden email] <[hidden email]>:

>
> Hello Jon,
>
> your python program works fine! Thanks so much!
>
> Only I don't understand every row of your codes, e.g. what formula[
> case.var] = case.coeff means, or what n.strip() means. Where can I find
> some explanations of these codes?
>
> Thank you again!!
>
> Regards
>
> Baiyun
>
>
>
> 2007/9/24, Peck, Jon <[hidden email]>:
> >
> > This can be done easily with a little Pythonery.
> > Assume the formulas are in formulas.sav and the main dataset is main.sav.  The
> > program below does the work.  First it reads the formula dataset and builds
> > a little dictionary of the terms.
> > Then it opens the other dataset, generates a compute command based on
> > the terms it found in the formula dataset, and submits that compute.  It
> > also prints the formula.
> >
> > This example uses some supplementary modules from SPSS Developer Central
> > (www.spss.com/devcentral) and will work on SPSS 14 and later.
> >
> > HTH,
> > Jon Peck
> >
> >
> > get file='c:/temp/formulas.sav'.
> > begin program.
> > import spss, spssdata
> >
> > formula={}
> > curs = spssdata.Spssdata()
> > for case in curs:
> > formula[case.var] = case.coeff
> > curs.CClose()
> > spss.Submit("get file='c:/temp/main.sav'")
> >
> > cmd = "COMPUTE newvar = " + "+".join([n.strip() + "*" + \
> > str(c) for n,c in formula.items()])
> > print cmd
> > spss.Submit(cmd)
> > end program.
> > exec.
> >
> > -----Original Message-----
> > From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
> > [hidden email]
> > Sent: Monday, September 24, 2007 9:02 AM
> > To: [hidden email]
> > Subject: Re: [SPSSX-L] can someone help me? Is it possible to use Python
> > to solve this problem?
> >
> > Hello Richard
> >
> > yes, I have a lot of records in the first file, all with
> > those 10 variables.
> >
> > In the second file the variables vary, they are a subset of the 10
> > variables. I would like SPSS detect automatically which variables are in
> > the
> > file 2 .
> >
> > Is it possbile?
> >
> > thanks in advance!
> >
> > baiyun
> >
> >
> >
> > 2007/9/21, Richard Ristow <[hidden email]>:
> > >
> > > At 07:29 AM 9/20/2007, [hidden email] wrote:
> > >
> > > >I have 2 files, one has 10 variables, say: var1 var2 ....var10
> > > >
> > > >another file has two variables, say var and coeff which looks like:
> > > >
> > > >var   coeff
> > > >var2   0.2
> > > >var8   0.3
> > > >var10  0.5
> > > >
> > > >I want to now create a new variable in file1, say c. c should be
> > > >var2*0.2 + var8*0.3 + var10*0.5.
> > >
> > > I'm assuming you have a lot of records in the first file, all with
> > > those 10 variables.
> > >
> > > How many records are there in the second file? Is it just those three,
> > > and the same set of coefficients apply to every record in the first
> > > file? Or are there several groups of records in the second file, with
> > > different sets of coefficients, and there's a key variable connecting
> > > the files?
> > >
> > > This looks like CASESTOVARS followed by MATCH FILES followed by
> > > COMPUTE, likely in a LOOP, but we'd have to know more to be sure.
> > >
> > > -Best of luck,
> > > Richard
> > >
> > >
> >
>
>
Reply | Threaded
Open this post in threaded view
|

Fwd: need help for some modification of Jon Peck's python program

xiaoqin.wan@gmail.com
Dear all,

I posted this on 28.09., I haven't got any replys. I am resending it now,
thanks for your help in advance!

Regards


---------- Forwarded message ----------
From: [hidden email] <[hidden email]>
Date: 28.09.2007 15:10
Subject: need help for some modification of Jon Peck's python program
To: "Peck, Jon" <[hidden email]>, [hidden email]


Dear all,

Jon Peck worte the following small python program which works fine!

*######################################.

get file='D:/formulas.sav'.

begin program.

import spss, spssdata

formula={}

curs = spssdata.Spssdata()

for case in curs:

formula[case.var] = case.coeff

curs.CClose()

spss.Submit("get file='D:/main.sav'")

cmd = "COMPUTE newvar = " + "+".join([n.strip() + "*" + \

str(c) for n,c in formula.items()])

print cmd

spss.Submit(cmd)

end program.

*#########################################.

The file formulas.sav looks like:

var coeff

var1  0.20
var2  0.30
var5  0.50
And the file main.sav looks like:

var1 var2 var3 var4 var5 var6
1,00 2,00 3,00 6,00 8,00 10,00
8,00 9,00 14,00 15,00 16,00 5,00

Thís program create automatically a new variable newvar in the file main.sav,
according to the formular (depending on the file strucure of formulas.sav):

var5*0.5+var2*0.3+var1*0.2
Now I have 10 files formulas1.sav, formulas2.sav, .....formulas10.sav and
one file main.sav

formulas1.sav looks like

var coeff

var1  0.20
var2  0.30
var5  0.50

formulas2.sav looks like

var coeff

var2  0.10
var6  0.80
var3  0.60
and so on ...

I would like to create 10 new variables newvar1, .... newvar10 in the file
main.sav, with the above formula:

newvar1 = var5*0.5+var2*0.3+var1*0.2.
newvar2 = var2*0.1+var6*0.8+var3*0.6.

....

How can I do that? with a loop?

Thanks for your help in advance!

baiyun



2007/9/27, [hidden email] <[hidden email]>:

>
> Hello Jon,
>
> your python program works fine! Thanks so much!
>
> Only I don't understand every row of your codes, e.g. what formula[
> case.var] = case.coeff means, or what n.strip() means. Where can I find
> some explanations of these codes?
>
> Thank you again!!
>
> Regards
>
> Baiyun
>
>
>
> 2007/9/24, Peck, Jon <[hidden email]>:
> >
> > This can be done easily with a little Pythonery.
> > Assume the formulas are in formulas.sav and the main dataset is main.sav.  The
> > program below does the work.  First it reads the formula dataset and builds
> > a little dictionary of the terms.
> > Then it opens the other dataset, generates a compute command based on
> > the terms it found in the formula dataset, and submits that compute.  It
> > also prints the formula.
> >
> > This example uses some supplementary modules from SPSS Developer Central
> > (www.spss.com/devcentral ) and will work on SPSS 14 and later.
> >
> > HTH,
> > Jon Peck
> >
> >
> > get file='c:/temp/formulas.sav'.
> > begin program.
> > import spss, spssdata
> >
> > formula={}
> > curs = spssdata.Spssdata()
> > for case in curs:
> > formula[case.var] = case.coeff
> > curs.CClose()
> > spss.Submit("get file='c:/temp/main.sav'")
> >
> > cmd = "COMPUTE newvar = " + "+".join([n.strip() + "*" + \
> > str(c) for n,c in formula.items()])
> > print cmd
> > spss.Submit(cmd)
> > end program.
> > exec.
> >
> > -----Original Message-----
> > From: SPSSX(r) Discussion [mailto: [hidden email]] On Behalf
> > Of [hidden email]
> > Sent: Monday, September 24, 2007 9:02 AM
> > To: [hidden email]
> > Subject: Re: [SPSSX-L] can someone help me? Is it possible to use Python
> > to solve this problem?
> >
> > Hello Richard
> >
> > yes, I have a lot of records in the first file, all with
> > those 10 variables.
> >
> > In the second file the variables vary, they are a subset of the 10
> > variables. I would like SPSS detect automatically which variables are in
> > the
> > file 2 .
> >
> > Is it possbile?
> >
> > thanks in advance!
> >
> > baiyun
> >
> >
> >
> > 2007/9/21, Richard Ristow < [hidden email]>:
> > >
> > > At 07:29 AM 9/20/2007, [hidden email] wrote:
> > >
> > > >I have 2 files, one has 10 variables, say: var1 var2 ....var10
> > > >
> > > >another file has two variables, say var and coeff which looks like:
> > > >
> > > >var   coeff
> > > >var2   0.2
> > > >var8   0.3
> > > >var10  0.5
> > > >
> > > >I want to now create a new variable in file1, say c. c should be
> > > >var2*0.2 + var8*0.3 + var10*0.5.
> > >
> > > I'm assuming you have a lot of records in the first file, all with
> > > those 10 variables.
> > >
> > > How many records are there in the second file? Is it just those three,
> > > and the same set of coefficients apply to every record in the first
> > > file? Or are there several groups of records in the second file, with
> > > different sets of coefficients, and there's a key variable connecting
> > > the files?
> > >
> > > This looks like CASESTOVARS followed by MATCH FILES followed by
> > > COMPUTE, likely in a LOOP, but we'd have to know more to be sure.
> > >
> > > -Best of luck,
> > > Richard
> > >
> > >
> >
>
>