0

I have several datasets over which i want to run identical commands. My basic idea is to create a vector with the names of the datasets and loop over it, using the specified name in my GET command:

VECTOR=(9) D = Name1 to Name9.
LOOP #i = 1 to 9.
     GET 
     FILE = Directory\D(#i).sav
     VALUE LABELS V1 to V8 'some text D(#i)'
LOOP END. 

Now SPSS doesn't recognize that i want it to use the specific value of the vector D. In Stata i'd use

local D(V1 to V8)
foreach D{
    ....`D' .....
}
Jignesh Sutar
  • 2,909
  • 10
  • 13
Caspar
  • 19
  • 8
  • The reason for `LOOP` not working here is that it only works with SPSS transformation commands as explained in [SPSS Command Types](http://www.spss-tutorials.com/spss-command-types/). – RubenGeert Jul 08 '15 at 17:54

3 Answers3

1

You can't use VECTOR in this way i.e. using GET command within a VECTOR/LOOP loop.

However you can use DEFINE/!ENDDEFINE. This is SPSS's native macro facility language, if you are not aware of this, you'll most likely need to do a lot of reading on it and understand it's syntax usage.

Here's an example:

DEFINE !RunJob ()

!DO !i !IN 1 !TO 9
  GET FILE = !CONCAT("Directory\D(",#i,").sav").
  VALUE LABELS V1 to V8 !QUOTE(!ONCAT("some text D(",#i,")",
!DOEND
!ENDDEFINE.

SET MPRINT ON.
!RunJob.
SET MPRINT OFF.

All the code between DEFINE and !ENDDEFINE is the body of the macro and the syntax near to the end !RunJob. then runs and executes those procedures defined in the macro.

This a very simply use of a macro with no parameters/arguments assigned but there is scope for much more complexity.

If you are new to DEFINE/!ENDEFINE I would actually suggest you NOT invest time in learning this but instead learn Python Program ability which can be used to achieve the same (and much more) with relative ease compared to DEFINE/!ENDDEFINE.

A python solution to your example would look like this (you will need Python Programmability integration with your SPSS):

BEGIN PROGRAM.
for i in xrange(1,9+1):
    spss.Submit("""
    GET FILE = Directory\D(%(i)s).sav
    VALUE LABELS V1 to V8 'some text D(%(i)s)'.""" % locals())
END PROGRAM.

As you will notice there is much more simplicity to the python solution.

Jignesh Sutar
  • 2,909
  • 10
  • 13
  • Thanks a lot. I'm also working with Stata and have some expirience with R. Would you say Define/!Enddefine is comparable to the R "function" command? – Caspar Jul 08 '15 at 11:51
  • Also: Could you explain what the !QUOTE command is doing, pls? – Caspar Jul 08 '15 at 12:07
  • Yes, comparable to R's "function" command. !QUOTE simply enclose any string generated within it with quotation marks. – Jignesh Sutar Jul 08 '15 at 12:16
0

@Caspar: use Python for SPSS for such jobs. SPSS macros have been long deprecated and had better be avoided.

If you use Python for this, you don't even have to type in the file names: you can simply look up all file names in some folder that end with ".sav" as shown in this example.

HTH!

RubenGeert
  • 2,902
  • 6
  • 32
  • 50
0

The Python approach is as Ruben says much superior to the old macro facility, but you can use the SPSSINC PROCESS FILES extension command to do tasks like this without any need to know Python. PROCESS FILES is included in the Python Essentials in recent versions of Statistics but can be downloaded from the SPSS Community website (www.ibm.com/developerworks/spssdevcentral) in older versions.

The idea is that you create a syntax file that works on one data file, and PROCESS FILES iterates that over a list of input files or a wildcard specification. For each file, it defines file handles and macros that you can use in the syntax file to open and process the data.

JKP
  • 5,419
  • 13
  • 5
  • Thanks. I'll consider this. The problem is that its kind of difficult to install new programms on the computers @work. Thats why I prefer the "simple" method of DEFINE. – Caspar Jul 10 '15 at 16:06
  • Starting with Statisics 22, Python and the Python Essentials are part of the main Statistics installation. For earlier versions, this is a separate install. – JKP Jul 11 '15 at 18:58
  • Thanks a lot. I believe we have 21:( but I'll look it up. – Caspar Jul 13 '15 at 10:04