Looping through years to run syntax for each

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

Looping through years to run syntax for each

Fiveja
Hoping for some Python magic for this ...

I currently have syntax that requires the user to first specify a year (e.g., 2010), the last digits of the year as a string (e.g., "10"), and either an "A" or "B."

For example:

define YYYY () 2010
!enddefine.
define YYstr () "10"
!enddefine.
define PeriodType () "A"
!enddefine.

The year value is used in some date calculations (e.g., date.mdy(10,01,YYYY-1). The string values are used to create a string variable (e.g., "10A"), to determine how to calculate some things later in the syntax (e.g., if "A", then do this, if "B", then do this), and to create unique file names (e.g., MyFile_' + YYstr + PeriodType + '.sav').

Typically, the syntax needs to be run for multiple years and A & B combinations. For example, the user will know at the start that he needs to the run the syntax for 10A through 14B. In other words:

10A, 10B, 11A, 11B, 12A, 12B, 13A, 13B, 14A, 14B

Is there a way for the user to enter the start and end parameters (e.g., "10A" and "14B") and have the syntax loop through these so it runs for each combination?

I'm thinking something like:

define StartPeriod () "10A"
!enddefine.
define EndPeriod () "14B"
!enddefine.

If that's too tricky, maybe:

define StartYear () 2010
!enddefine.

define StartPeriod () "A"
!enddefine.

define EndYear () 2014
!enddefine.

define EndPeriod () "B"
!enddefine.

Thank you for any starting pointers for how to tackle this.  
Reply | Threaded
Open this post in threaded view
|

Re: Looping through years to run syntax for each

Jignesh Sutar
I don't have time to look at this to setup a demo but you don't need the start and end points as quoted strings for starters (you can have them quoted in the macro itself - add leading zero also ect). You can then have the 10 and 14 as numeric values as arguments for your start and end parameters.

Then you can have bundles of fun wrapping your head around doing basic additional arithmetic in SPSS macro language: http://pages.infinit.net/rlevesqu/Macros/ArithmeticWithMacroVariables.txt

That should give you enough clues, basically your creating a counter starting from 10, adding one each time and then stopping when it equals 14 and you do all this by building a string of blanks and evaluating its length (i.e. the number of blanks) - again referring to the link shared.

And yes python magic is definitely the way forward!

begin program.
for year in xrange(2010,2014+1):
    for period in ["A","B"]:
        print year, period
end program




On 14 May 2015 at 08:26, Fiveja <[hidden email]> wrote:
Hoping for some Python magic for this ...

I currently have syntax that requires the user to first specify a year
(e.g., 2010), the last digits of the year as a string (e.g., "10"), and
either an "A" or "B."

For example:

define YYYY () 2010
!enddefine.
define YYstr () "10"
!enddefine.
define PeriodType () "A"
!enddefine.

The year value is used in some date calculations (e.g.,
date.mdy(10,01,YYYY-1). The string values are used to create a string
variable (e.g., "10A"), to determine how to calculate some things later in
the syntax (e.g., if "A", then do this, if "B", then do this), and to create
unique file names (e.g., MyFile_' + YYstr + PeriodType + '.sav').

Typically, the syntax needs to be run for multiple years and A & B
combinations. For example, the user will know at the start that he needs to
the run the syntax for 10A through 14B. In other words:

10A, 10B, 11A, 11B, 12A, 12B, 13A, 13B, 14A, 14B

Is there a way for the user to enter the start and end parameters (e.g.,
"10A" and "14B") and have the syntax loop through these so it runs for each
combination?

I'm thinking something like:

define StartPeriod () "10A"
!enddefine.
define EndPeriod () "14B"
!enddefine.

If that's too tricky, maybe:

define StartYear () 2010
!enddefine.

define StartPeriod () "A"
!enddefine.

define EndYear () 2014
!enddefine.

define EndPeriod () "B"
!enddefine.

Thank you for any starting pointers for how to tackle this.



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Looping-through-years-to-run-syntax-for-each-tp5729559.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