Reputation: 43
I have a dataset where I need to separate the cases into several different files. currently I am running:
DATASET COPY DATA1 WINDOW = FRONT.
... (repeats) ...
DATASET COPY DATA25 WINDOW = FRONT.
after it makes 25 copies, I use a bunch of select if commands on each one to pick only the cases I want, and save them as DATA1, through DATA25.
What I want to do is set up some kind of macro or loop so I can say:
LET %X = 1 to 25
loop
DATASET COPY DATA'%X' WINDOW = FRONT.
end loop
Instead of needing 25 lines of near identical syntax. This is just a very simple use case, but I am hoping I can branch out from there and do a whole bunch of other things using this kind of syntax, such as opening multiple files where I can put in a wildcard for the part for the filename that changes, or using a wildcard so I can open 'sheet 1' of and excel, and then repeat for sheets 2 to 10. Is this something I can do with SPSS? Do I need a Python or R extension? Everything I have seen thus far only lets you loop through running a set of commands on a range of variables.
Upvotes: 2
Views: 1202
Reputation: 11350
If you are using SPSS version 23 you don't need to install a Python extension, it is included in the original SPSS installation.
If you do not want to use Python or R, you can do what you described with syntax alone using SPSS macro - look up !define - !enddefine
.
For example, the following macro will repeat the loop 25 times, in each loop it will go to the original file, copy it to a dataset called DATA#, select cases where MyFilterVar=#
, and save the cases to a separate file named like the dataset:
define CreateCopies ()
!do !i=1 !to 25
dataset activate Orig.
dataset copy !concat('DATA',!i).
dataset activate !concat('DATA',!i).
select if MyFilterVar=!i.
!let !FileName=!concat('MyPath\DATA',!i,".sav")
save out=!quote(!FileName).
!doend
!enddefine.
after defining the macro you need to name your original file and then call the macro:
dataset name orig.
CreateCopies.
Upvotes: 1