Use repeat or loop to specify a list of values

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

0101
Posts: 52
Joined: Fri Mar 04, 2011 9:42 pm

Use repeat or loop to specify a list of values

Postby 0101 » Tue Jul 08, 2014 7:09 pm

I have some simple code where the user defines a year of interest, and then a date is calculated based on the year provided. I then save some output with the year appended to the file name:

Code: Select all

* Define the FFY. 
define YYYY () 2011 !enddefine.

* Run some code.
compute FFY = date.mdy(10,01,YYYY-1).

* Save some output.
* (I have code that saves a custom table with the FFY in the file name).
But I would like to do this for several years of data (2009, 2010, 2011, 2012, and 2013). I tried to use DO REPEAT but am getting the error, "DO REPEAT has no effect on this command."

I tried something like:

Code: Select all

do repeat Years = 2009 2010 2011 2012 2013.
define YYYY () Years !enddefine.
compute FFY = date.mdy(10,01,YYYY-1).
etc. 
end repeat.
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: Use repeat or loop to specify a list of values

Postby RubenGeert » Wed Jul 09, 2014 5:31 am

You can't use DEFINE or SAVE within DO REPEAT (or within LOOP).

Instead, of putting the macro in the loop, put the loop in the macro (!DO ... !TO ...).

Or better, loop with Python. An tutorial I wrote (long ago) does almost exactly what you're asking for: http://www.spss-tutorials.com/create-di ... nt-groups/.

Finally, note that you can use TO for these years (DO REPEAT year = 2009 to 2013).

Kind regards,

Ruben Geert van den Berg
www.spss-tutorials.com
Kurt
Posts: 4
Joined: Tue Apr 29, 2008 6:25 pm

Re: Use repeat or loop to specify a list of values

Postby Kurt » Wed Jul 09, 2014 5:00 pm

Thank you!

Still having trouble figuring this out. I would like a non-Python approach if possible, but even the Python code you show assumes the values are stored in the file (in your case, school). In my case, they are not - they are specified at run time and may expand over time (e.g., 2009 to 2013). And all of the !DO ... !TO examples I find focus on looping through a set of *variables*, but not values.

I've attached a sample dataset to help illustrate. The data look like:

Code: Select all

SchoolID YrEnrolled SubjectID Sex
1        10/01/2009 1          Female
1        10/01/2009 2          Male
1        10/01/2010 3          Male
etc.
The code below works well. For each year I define (e.g., 2009, 2010, and 2011), the syntax will create an SPSS file showing the percentage of males and females in each schoool, for the given year. The problem is that it requires manually replacing the values for YYYY and YYstr and then rerunning the code. My goal is to have it automatically loop through a list of a values defined at run time (e.g., 2009 to 2011, and "09", "10", and "11").

Code: Select all

* Define the FFY.
define YYYY () 2012 !enddefine.
define YYstr ( ) "12" !enddefine. 

* Run some code.
compute FFY = date.mdy(10,01,YYYY-1).
formats FFY (ADate10).
execute.

oms select tables
  /destination format = sav outfile = "C:\temp\Period " +YYstr+".sav"
  /if commands = ['ctables'] subtypes = ['Custom Table']. 
temporary.
select if FFY=YrEnrolled.
ctables
  /VLABELS VARIABLES=SchoolID Sex DISPLAY=BOTH
  /TABLE SchoolID [C][ROWPCT.COUNT PCT40.1] BY Sex [C]
  /CATEGORIES VARIABLES=SchoolID ORDER=A KEY=VALUE EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER
  /CATEGORIES VARIABLES=Sex ORDER=A KEY=VALUE EMPTY=INCLUDE.
omsend.
I've tried all sorts of DO REPEAT and DO! !TO options and they all bomb. The syntax is tricky to figure out. Thanks again for any pointers.
You do not have the required permissions to view the files attached to this post.
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: Use repeat or loop to specify a list of values

Postby RubenGeert » Thu Jul 10, 2014 7:12 am

I already pointed out that LOOP and DO REPEAT are no options here, although I do believe a macro could do the job. I don't see the problem of relevant years not being in variables, you can just use something like

!DO !YEAR = 2009 !TO 2013

right?

But anyway, I think macros are a complete waste of time, a dead end street. I think the whole thing would be solved in a minute if you'd go for a Python solution. Moving from macros to Python feels like moving from the GUI to syntax. You'll wonder how on earth you ever got anything done before you discovered it...

Best,

Ruben

Who is online

Users browsing this forum: No registered users and 2 guests

cron