1

Hi I have a syntax file and I want to create several .sav files

for 87 to 95
  for 1 to 12
    FILE HANDLE EPA/NAME='C:\Users\gma\Downloads/pub0187.prn'/LRECL=138.
    DATA LIST FILE=EPA/
     STATES_PROV  001 - 007
     ...          .....
     VARIABLE LABELS
     ...
     MISSING VALUES
     ....

    SAVE OUTFILE='C:\Users\gma\Downloads/epa0187.sav'.
  end
end

Output file

epa0187.sav
epa0287.sav
epa0387.sav
...
epa0188.sav
...
epa0189.sav
...
epa1295.sav

Input file

pub0187.prn
pub0287.prn
...
pub0188.prn
...
pub0189.prn
...
pub1295.prn

So in other words I want to create a .sav file for each month from January 1987 up to December 1995.

1 Answer 1

2

You could create a macro loop in standard Statistics syntax, but here is a solution using Python code within Statistics. Note that the indentations are important.

begin program.
import spss

for year in range(87, 96):  # note stop is one beyond
    for month in range(1, 13):  # one beyond
        spss.Submit(r"""FILE HANDLE EPA
/NAME='C:\Users\gma\Downloads/pub%(month)s%(year)s.prn'/LRECL=138.
DATA LIST FILE=EPA/
 STATES_PROV  001 - 007
 ...          .....
 VARIABLE LABELS
 ...
 MISSING VALUES
 ....

SAVE OUTFILE='C:\Users\gma\Downloads/epa%(month)s%(year)s.sav'.""" % locals())
end program.
Sign up to request clarification or add additional context in comments.

2 Comments

note that the file names have a two digit number for all months, so month needs to be 01, 02 ... instead of 1, 2, 3 ...
to get that, change the formats for the month parts like this: %(month)02d instead of %(month)s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.