Running same syntax for different years/quarters

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

BartL
Posts: 1
Joined: Fri Jan 04, 2013 9:30 am

Running same syntax for different years/quarters

Postby BartL » Fri Jan 04, 2013 9:43 am

Hi all,
Currently I have to run a program like the following:

Code: Select all

get 2003.sav.
select if month >= 200301 & month <=200303.
...
do stuff
...
save outfile 20031.sav.

get 2003.sav.
select if month >= 200304 & month <=200306.
...
do stuff
...
save outfile 20032.sav.
...
...
I have to write the same piece of code for a couple of years, which is what I do not want to do. Instead, I want to define a variable (?) called X that takes on the values 2003, 2004, 2005, etc. Then, I would like to loop over the values of this variable and write the code as follows:

Code: Select all

get X.sav.
select if month >= X01 & month <=X03.
...
do stuff
...
save outfile X1.sav.

get X.sav.
select if month >= X04 & month <=X06.
...
do stuff
...
save outfile X2.sav.
...
...
Does anybody have an easy reference for this problem? In Stata, I would do it as follows:

forval X = 2003/2012{
code as shown above
}

To counter the most obvious answer: Yes, I DO have to use SPSS :)
Thanks, Bart
JonPedersen
Posts: 117
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Running same syntax for different years/quarters

Postby JonPedersen » Mon Jan 07, 2013 9:06 pm

Hi,
This should get you started

Code: Select all

Define ftest(syear=!TOKENS(1) /eyear=!TOKENS(1)).
!LET !PATH="C:\Users\jpe\Documents\"
!DO !THISYEAR = !SYEAR !TO !EYEAR
!LET !FILENAME=!CONCAT(!THISYEAR,".SAV")
!LET !FILENAME = !QUOTE(!CONCAT(!PATH,!FILENAME))
  get file !FILENAME.
  freq test.
!DOEND
!ENDDEFINE.


set mprint on.
ftest SYEAR=2001 EYEAR=2002.
set mprint off.
This assumes that all files have a variable called "test".
The mprint on and off are just to see what is happening.

hth
Jon

Who is online

Users browsing this forum: No registered users and 2 guests

cron