Using Python to loop through code, substituting a different value each time

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

Using Python to loop through code, substituting a different value each time

Fiveja
I have data like:

School v1 v2 v3
Yale
Yale
Yale
Princeton
Princeton
Princeton
Stanford
etc.

I also have several blocks of code that all reference a macro where the user has entered a school name. He enters the school name, runs the code, enters a different school name, repeat. For example:

* User manually types a school name.

define !School () "Yale"
!enddefine

* Run a bunch of code ...

temporary.
select if School = !School.
flip variables ...

[some more code, including some oms send stuff ...]

[some more code ...]

get file "C:\temp\Some other data.sav".
...
string School (A30).
compute School = !School.
...
execute.

[some more code ...]
execute.

*Go back to the define macro and type a different school, and run the code again.

I'd like to automate this so the user doesn't have to manually specify the school each time. Instead, the code will just use the values in the variable School and loop through the code, each time entering a new school name and using it where needed (e.g., the "!School"s).
 
I studied this python code which uses a similar idea, but can't get it to translate to my example: http://www.spss-tutorials.com/create-different-files-for-different-groups/

The Python language is still new to me. I'm wasn't sure if I should put all my code between the

spss.Submit('''

AND

'''%locals())
end program.

Suggestions? Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

Jon K Peck
Here's an example of how to do this.

begin program.
import spss, spssaux, spssdata

curs = spssdata.Spssdata("School")
schoolvalues = set()
for case in curs:
    schoolvalues.add(spssaux._smartquote(case[0].rstrip()))
curs.CClose()

for school in schoolvalues:
   spss.Submit("""your syntax with reference to the school written as %(school)s""")
end program.

In the syntax block, the school names will already be quoted, so don't add additional quotes around the names.

There are other ways to do this using various Python extensions, but unless  your dataset is very large, you might as well go with the simple structure above.

If you do have a lot of data, the SPSSINC SPLIT DATASET and SPSSINC PROCESS FILES can handle all this more efficiently without your writing any Python code.




Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Fiveja <[hidden email]>
To:        [hidden email]
Date:        04/27/2015 08:29 AM
Subject:        [SPSSX-L] Using Python to loop through code, substituting a different value each time
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I have data like:

School v1 v2 v3
Yale
Yale
Yale
Princeton
Princeton
Princeton
Stanford
etc.

I also have several blocks of code that all reference a macro where the user
has entered a school name. He enters the school name, runs the code, enters
a different school name, repeat. For example:

* User manually types a school name.

define !School () "Yale"
!enddefine

* Run a bunch of code ...

temporary.
*select if School = !School.*
flip variables ...

[some more code, including some oms send stuff ...]

[some more code ...]

get file "C:\temp\Some other data.sav".
...
string School (A30).
*compute School = !School.*
...
execute.

[some more code ...]
execute.

*Go back to the define macro and type a different school, and run the code
again.

I'd like to automate this so the user doesn't have to manually specify the
school each time. Instead, the code will just use the values in the variable
School and loop through the code, each time entering a new school name and
using it where needed (e.g., the "!School"s).

I studied this python code which uses a similar idea, but can't get it to
translate to my example:
http://www.spss-tutorials.com/create-different-files-for-different-groups/

The Python language is still new to me. I'm wasn't sure if I should put all
my code between the

spss.Submit('''

AND

'''%locals())
end program.

Suggestions? Thank you.



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Using-Python-to-loop-through-code-substituting-a-different-value-each-time-tp5729382.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


===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

Fiveja
I'm getting an error. Let's use a simple example, where I just want to temporary select if School = %(School) and then run a frequency on School. With the code below, I get an error, "The expression is incomplete. Check for missing operands, ..." The first error occurs when it encounters the "%".

data list free/School(a10) v1 to v3.
begin data
Yale 1 1 0 Yale 1 1 0 Yale 1 1 1 Yale 1 1 1 Princeton 0 1 0 Princeton 0 0 1
Princeton 0 1 1 Stanford 0 1 1 Stanford 0 0 1 Harvard 1 1 0
end data.

begin program.
import spss, spssaux, spssdata

curs = spssdata.Spssdata("School")
schoolvalues = set()
for case in curs:
    schoolvalues.add(spssaux._smartquote(case[0].rstrip()))
curs.CClose()

for School in schoolvalues:
   spss.Submit("""temporary.
select if School = %(School)s.
frequencies School.""")
end program.
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

Jon K Peck
You have to evaluate the expression in the string via the % locals() so that the substitutions will be carried out.

   spss.Submit("""temporary.
select if School = %(School)s.
frequencies School.""" % locals())


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Fiveja <[hidden email]>
To:        [hidden email]
Date:        04/27/2015 10:39 AM
Subject:        Re: [SPSSX-L] Using Python to loop through code, substituting a different value each time
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I'm getting an error. Let's use a simple example, where I just want to
temporary select if School = %(School) and then run a frequency on School.
With the code below, I get an error, "The expression is incomplete. Check
for missing operands, ..." The first error occurs when it encounters the
"%".

data list free/School(a10) v1 to v3.
begin data
Yale 1 1 0 Yale 1 1 0 Yale 1 1 1 Yale 1 1 1 Princeton 0 1 0 Princeton 0 0 1
Princeton 0 1 1 Stanford 0 1 1 Stanford 0 0 1 Harvard 1 1 0
end data.

begin program.
import spss, spssaux, spssdata

curs = spssdata.Spssdata("School")
schoolvalues = set()
for case in curs:
   schoolvalues.add(spssaux._smartquote(case[0].rstrip()))
curs.CClose()

for School in schoolvalues:
  spss.Submit("""temporary.
select if School = %(School)s.
frequencies School.""")
end program.



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Using-Python-to-loop-through-code-substituting-a-different-value-each-time-tp5729382p5729384.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


===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

David Marso
Administrator
In reply to this post by Fiveja
Perhaps a simple SORT CASES
followed by SPLIT FILE will suffice?
--
Fiveja wrote
I have data like:

School v1 v2 v3
Yale
Yale
Yale
Princeton
Princeton
Princeton
Stanford
etc.

I also have several blocks of code that all reference a macro where the user has entered a school name. He enters the school name, runs the code, enters a different school name, repeat. For example:

* User manually types a school name.

define !School () "Yale"
!enddefine

* Run a bunch of code ...

temporary.
select if School = !School.
flip variables ...

[some more code, including some oms send stuff ...]

[some more code ...]

get file "C:\temp\Some other data.sav".
...
string School (A30).
compute School = !School.
...
execute.

[some more code ...]
execute.

*Go back to the define macro and type a different school, and run the code again.

I'd like to automate this so the user doesn't have to manually specify the school each time. Instead, the code will just use the values in the variable School and loop through the code, each time entering a new school name and using it where needed (e.g., the "!School"s).
 
I studied this python code which uses a similar idea, but can't get it to translate to my example: http://www.spss-tutorials.com/create-different-files-for-different-groups/

The Python language is still new to me. I'm wasn't sure if I should put all my code between the

spss.Submit('''

AND

'''%locals())
end program.

Suggestions? Thank you.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

Fiveja
In reply to this post by Jon K Peck
Thanks, John. That did the trick.
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

David Marso
Administrator
But why even bother with python for something so easily done with
SORT CASES BY SCHOOL.
SPLIT FILE BY SCHOOL.
code...

I notice a FLIP command.
What on earth for?
Maybe you want to look at VARSTOCASES and CASESTOVARS.
Much more flexible and powerful.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

Jon K Peck
SPLIT FILES is fine when you want to iterate over a single procedure, but if you want output from a batch of commands grouped by split, that won't work.  The Python code or SPSSINC SPLIT DATASET/SPSSINC PROCESS FILES allow you to group entire syntax files.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        04/27/2015 01:55 PM
Subject:        Re: [SPSSX-L] Using Python to loop through code, substituting a different value each time
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




But why even bother with python for something so easily done with
SORT CASES BY SCHOOL.
SPLIT FILE BY SCHOOL.
code...

I notice a FLIP command.
What on earth for?
Maybe you want to look at VARSTOCASES and CASESTOVARS.
Much more flexible and powerful.



-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Using-Python-to-loop-through-code-substituting-a-different-value-each-time-tp5729382p5729388.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


===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

David Marso
Administrator
That much is true.
However.
Personally, I would analyze the code and determine the route of data restructuring necessary to permit SPLIT FILE.  
I suspect it would reduce the number of data passes.

--
Jon K Peck wrote
SPLIT FILES is fine when you want to iterate over a single procedure, but
if you want output from a batch of commands grouped by split, that won't
work.  The Python code or SPSSINC SPLIT DATASET/SPSSINC PROCESS FILES
allow you to group entire syntax files.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:   David Marso <[hidden email]>
To:     [hidden email]
Date:   04/27/2015 01:55 PM
Subject:        Re: [SPSSX-L] Using Python to loop through code,
substituting a different value each time
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



But why even bother with python for something so easily done with
SORT CASES BY SCHOOL.
SPLIT FILE BY SCHOOL.
code...

I notice a FLIP command.
What on earth for?
Maybe you want to look at VARSTOCASES and CASESTOVARS.
Much more flexible and powerful.



-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to
email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
abyssum?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Using-Python-to-loop-through-code-substituting-a-different-value-each-time-tp5729382p5729388.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



=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Using Python to loop through code, substituting a different value each time

Jon K Peck
The extension commands I mentioned generalize split files, which is strictly limited to iterating within a procedure.  No data restructuring, other than breaking up the dataset, can group output from multiple procedures.

SPLIT DATASET actually makes separate files in a single data pass or two (or in blocks of 64 if there are too many groups), and it does not require sorting, which can take many data passes.  Then each application of PROCESS FILES only pass the data for its own split.

But saving data passes is only worthwhile if there is a lot of data with lots of split values.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        04/27/2015 02:39 PM
Subject:        Re: [SPSSX-L] Using Python to loop through code, substituting a different value each time
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




That much is true.
However.
Personally, I would analyze the code and determine the route of data
restructuring necessary to permit SPLIT FILE.  
I suspect it would reduce the number of data passes.

--

Jon K Peck wrote
> SPLIT FILES is fine when you want to iterate over a single procedure, but
> if you want output from a batch of commands grouped by split, that won't
> work.  The Python code or SPSSINC SPLIT DATASET/SPSSINC PROCESS FILES
> allow you to group entire syntax files.
>
>
> Jon Peck (no "h") aka Kim
> Senior Software Engineer, IBM

> peck@.ibm

> phone: 720-342-5621
>
>
>
>
> From:   David Marso &lt;

> david.marso@

> &gt;
> To:    

> SPSSX-L@.UGA

> Date:   04/27/2015 01:55 PM
> Subject:        Re: [SPSSX-L] Using Python to loop through code,
> substituting a different value each time
> Sent by:        "SPSSX(r) Discussion" &lt;

> SPSSX-L@.UGA

> &gt;
>
>
>
> But why even bother with python for something so easily done with
> SORT CASES BY SCHOOL.
> SPLIT FILE BY SCHOOL.
> code...
>
> I notice a FLIP command.
> What on earth for?
> Maybe you want to look at VARSTOCASES and CASESTOVARS.
> Much more flexible and powerful.
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/Using-Python-to-loop-through-code-substituting-a-different-value-each-time-tp5729382p5729388.html
>
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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
>
>
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Using-Python-to-loop-through-code-substituting-a-different-value-each-time-tp5729382p5729390.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


===================== 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