Python Nested Loop

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

Python Nested Loop

Krinsky, Alan-2
I am trying to nest a Python loop within another loop. I have done this before, but the catch here is that I need to save the file each time it loops through the outer loop.
 
So, I have 10 dimensions, each of which has its own file/dataset to start. I need to run each of these through the same set of operations/variables, let's say 16 of them. If all dimensions were in one dataset, I could open the single dataset, run through all 160 loops, and then save the dataset.
 
But what I need to do is open the dataset for dimension 1, run it through the 16 sets of variables, then save the file, open the dataset for dimension 2, run it through, save, etc. But if I embed the GET FILE and SAVE OUTFILE inside the nested loop, it will open the original file for each dimension 16 times and save 16 times. I do not think I am able, or not sure if I am able to embed the GET and SAVE commands in between the outer and nested loops. If I place them outside the outer loop, then basically I have to replicate the syntax 10 times, once for each dimension, the outer loop having no effect or value. Is that clear? Any ideas?
 
The basic logic would work something like this:
 
 
BEGIN PROGRAM.
import spss, spssaux
 
Dimension = ['TypeA', 'TypeB', 'TypeC', 'TypeD', 'TypeE', 'TypeF', 'TypeG', 'TypeH', 'TypeI', 'TypeJ']
 
for dmn in Dimension:
 
            TimeFrame = ['2008Q2to2009Q1', '2008Q3to2009Q2', '2008Q4to2009Q3', '2009Q1to2009Q4', '2009Q2to2010Q1', '2009Q3to2010Q2', '2009Q4to2010Q3', '2010Q1to2010Q4',
                                 '2010Q2to2011Q1', '2010Q3to2011Q2', '2010Q4to2011Q3', '2011Q1to2011Q4', '2011Q2to2012Q1', '2011Q3to2012Q2', '2011Q4to2012Q3', '2012Q1to2012Q4']
            StartYearMonth = [200804, 200807, 200810, 200901, 200904, 200907, 200910, 201001,
                                        201004, 201007, 201010, 201101, 201104, 201107, 201110, 201201]
            EndYearMonth = [200903, 200906, 200909, 200912, 201003, 201006, 201009, 201012,
                                       201103, 201106, 201109, 201112, 201203, 201206, 201209, 201212]
 
            for period, start, end in zip(TimeFrame, StartYearMonth, EndYearMonth):
 
               spss.Submit(r"""
 
                     * %(dmn)s COUNT %(period)s.
                     NUMERIC %(dmn)s_COUNT_from%(period)s (F5.0).
                     COMPUTE %(dmn)s_COUNT_from%(period)s = 0.
                     COUNT %(dmn)s_COUNT_from%(period)s = yearmonth.1 TO yearmonth.57 (%(start)s THRU %(end)s).
 
""" % locals())
END PROGRAM.
 
EXECUTE.
 
 
If I place the GET FILE after the spss.Submit, it will open 160 times, but I want to open only once for each dimension (and save once for each). But can I place the GET FILE after “for dmn in Dimension:” and before the second set of variables? And even if so, where would I place the SAVE OUTFILE command? Can there be 2 sets of END PROGRAM commands? Or, is the only option to copy and paste the loop 10 times, once for each Dimension? A friend suggested declaring a function, but I am not certain how that might work? Does the entire nested loop become a function somewhere else, and I call it up ten times in a single loop?
 
Thank you for any assistance. Be well!
 
Alan
 
 
 
 
Alan D. Krinsky  PhD, MPH
Senior Data Analyst
Office of Clinical Integration
UMass Memorial Health Care
38 Oak Ave.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail: [hidden email]
 
 
 

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
Reply | Threaded
Open this post in threaded view
|

Re: Python Nested Loop

Rich Ulrich
I think in terms of original SPSS rather than Python...

If Python has some equivalent to XSAVE, you can open
them all at once, and write to the appropriate one.

Or, presuming this will be done once or not often, there
is relatively little computer time (compared to programmer
time) if you write them all to one file, then Split it.  Which
is not hard.

--
Rich Ulrich


Date: Fri, 26 Apr 2013 15:42:48 +0000
From: [hidden email]
Subject: Python Nested Loop
To: [hidden email]

I am trying to nest a Python loop within another loop. I have done this before, but the catch here is that I need to save the file each time it loops through the outer loop.
 
So, I have 10 dimensions, each of which has its own file/dataset to start. I need to run each of these through the same set of operations/variables, let's say 16 of them. If all dimensions were in one dataset, I could open the single dataset, run through all 160 loops, and then save the dataset.
 
But what I need to do is open the dataset for dimension 1, run it through the 16 sets of variables, then save the file, open the dataset for dimension 2, run it through, save, etc. But if I embed the GET FILE and SAVE OUTFILE inside the nested loop, it will open the original file for each dimension 16 times and save 16 times. I do not think I am able, or not sure if I am able to embed the GET and SAVE commands in between the outer and nested loops. If I place them outside the outer loop, then basically I have to replicate the syntax 10 times, once for each dimension, the outer loop having no effect or value. Is that clear? Any ideas?
 
The basic logic would work something like this:
 
 
BEGIN PROGRAM.
import spss, spssaux
 
Dimension = ['TypeA', 'TypeB', 'TypeC', 'TypeD', 'TypeE', 'TypeF', 'TypeG', 'TypeH', 'TypeI', 'TypeJ']
 
for dmn in Dimension:
 
            TimeFrame = ['2008Q2to2009Q1', '2008Q3to2009Q2', '2008Q4to2009Q3', '2009Q1to2009Q4', '2009Q2to2010Q1', '2009Q3to2010Q2', '2009Q4to2010Q3', '2010Q1to2010Q4',
                                 '2010Q2to2011Q1', '2010Q3to2011Q2', '2010Q4to2011Q3', '2011Q1to2011Q4', '2011Q2to2012Q1', '2011Q3to2012Q2', '2011Q4to2012Q3', '2012Q1to2012Q4']
            StartYearMonth = [200804, 200807, 200810, 200901, 200904, 200907, 200910, 201001,
                                        201004, 201007, 201010, 201101, 201104, 201107, 201110, 201201]
            EndYearMonth = [200903, 200906, 200909, 200912, 201003, 201006, 201009, 201012,
                                       201103, 201106, 201109, 201112, 201203, 201206, 201209, 201212]
 
            for period, start, end in zip(TimeFrame, StartYearMonth, EndYearMonth):
 
               spss.Submit(r"""
 
                     * %(dmn)s COUNT %(period)s.
                     NUMERIC %(dmn)s_COUNT_from%(period)s (F5.0).
                     COMPUTE %(dmn)s_COUNT_from%(period)s = 0.
                     COUNT %(dmn)s_COUNT_from%(period)s = yearmonth.1 TO yearmonth.57 (%(start)s THRU %(end)s).
 
""" % locals())
END PROGRAM.
 
EXECUTE.
 
 
If I place the GET FILE after the spss.Submit, it will open 160 times, but I want to open only once for each dimension (and save once for each). But can I place the GET FILE after “for dmn in Dimension:” and before the second set of variables? And even if so, where would I place the SAVE OUTFILE command? Can there be 2 sets of END PROGRAM commands? Or, is the only option to copy and paste the loop 10 times, once for each Dimension? A friend suggested declaring a function, but I am not certain how that might work? Does the entire nested loop become a function somewhere else, and I call it up ten times in a single loop?
 
Thank you for any assistance. Be well!
 
Alan
 
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Python Nested Loop

Jon K Peck
In reply to this post by Krinsky, Alan-2
I'm having trouble understanding the difficulty.  Something like this should work.
Dimension = ['TypeA', 'TypeB', 'TypeC', 'TypeD', 'TypeE', 'TypeF', 'TypeG', 'TypeH', 'TypeI', 'TypeJ']
 
for dmn in Dimension:
        spss.Submit(""""get file="%s.sav".""" % dmn)
         for period, start, end in zip(TimeFrame, StartYearMonth, EndYearMonth):
                --stuff--
        spss.Submit("""save outfile="%s.sav".""" % dmn)
print "all done"


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




From:        "Krinsky, Alan" <[hidden email]>
To:        [hidden email],
Date:        04/29/2013 11:05 PM
Subject:        [SPSSX-L] Python Nested Loop
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I am trying to nest a Python loop within another loop. I have done this before, but the catch here is that I need to save the file each time it loops through the outer loop.
 
So, I have 10 dimensions, each of which has its own file/dataset to start. I need to run each of these through the same set of operations/variables, let's say 16 of them. If all dimensions were in one dataset, I could open the single dataset, run through all 160 loops, and then save the dataset.
 
But what I need to do is open the dataset for dimension 1, run it through the 16 sets of variables, then save the file, open the dataset for dimension 2, run it through, save, etc. But if I embed the GET FILE and SAVE OUTFILE inside the nested loop, it will open the original file for each dimension 16 times and save 16 times. I do not think I am able, or not sure if I am able to embed the GET and SAVE commands in between the outer and nested loops. If I place them outside the outer loop, then basically I have to replicate the syntax 10 times, once for each dimension, the outer loop having no effect or value. Is that clear? Any ideas?
 
The basic logic would work something like this:
 
 
BEGIN PROGRAM.
import spss, spssaux
 
Dimension = ['TypeA', 'TypeB', 'TypeC', 'TypeD', 'TypeE', 'TypeF', 'TypeG', 'TypeH', 'TypeI', 'TypeJ']
 
for dmn in Dimension:
 
            TimeFrame = ['2008Q2to2009Q1', '2008Q3to2009Q2', '2008Q4to2009Q3', '2009Q1to2009Q4', '2009Q2to2010Q1', '2009Q3to2010Q2', '2009Q4to2010Q3', '2010Q1to2010Q4',
                                 '2010Q2to2011Q1', '2010Q3to2011Q2', '2010Q4to2011Q3', '2011Q1to2011Q4', '2011Q2to2012Q1', '2011Q3to2012Q2', '2011Q4to2012Q3', '2012Q1to2012Q4']
            StartYearMonth = [200804, 200807, 200810, 200901, 200904, 200907, 200910, 201001,
                                        201004, 201007, 201010, 201101, 201104, 201107, 201110, 201201]
            EndYearMonth = [200903, 200906, 200909, 200912, 201003, 201006, 201009, 201012,
                                       201103, 201106, 201109, 201112, 201203, 201206, 201209, 201212]
 
            for period, start, end in zip(TimeFrame, StartYearMonth, EndYearMonth):
 
               spss.Submit(r"""
 
                     * %(dmn)s COUNT %(period)s.
                     NUMERIC %(dmn)s_COUNT_from%(period)s (F5.0).
                     COMPUTE %(dmn)s_COUNT_from%(period)s = 0.
                     COUNT %(dmn)s_COUNT_from%(period)s = yearmonth.1 TO yearmonth.57 (%(start)s THRU %(end)s).
 
""" % locals())
END PROGRAM.
 
EXECUTE.
 
 
If I place the GET FILE after the spss.Submit, it will open 160 times, but I want to open only once for each dimension (and save once for each). But can I place the GET FILE after “for dmn in Dimension:” and before the second set of variables? And even if so, where would I place the SAVE OUTFILE command? Can there be 2 sets of END PROGRAM commands? Or, is the only option to copy and paste the loop 10 times, once for each Dimension? A friend suggested declaring a function, but I am not certain how that might work? Does the entire nested loop become a function somewhere else, and I call it up ten times in a single loop?
 
Thank you for any assistance. Be well!
 
Alan
 
 
 
 
Alan D. Krinsky  PhD, MPH
Senior Data Analyst

Office of Clinical Integration
UMass Memorial Health Care
38 Oak Ave.
Worcester, MA 01605
Phone: 508-334-5854
Fax: 508-793-6086
E-mail:
alan.krinsky@...
 
 
 

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, transmission, re-transmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Python Nested Loop

Morris, Ramona (MCSCS)
I'm sorry I can't reply to you.  I will be out of the office until Monday March  18, 2013.

For Research assistance please contact Janice Mokanski:  [hidden email] or by phone 519 773 4298.

For Promotional examination inquiries please contact Brian McNair: [hidden email] or by phone 519 773 4288.

Thank you
Ramona Morris
R&E Ontario Police College

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