'SPSS repeat a task on many files
I would like to achieve the following:
For each of the 100 datasets (saved in .sav), 1) open file, 2) add a variable called 'Day' to it and populate it with number. so file1 will have 1 under 'Day', file2 will have 2 under 'Day' and so on (see figure1 below). 3) Select the rows that has Status = 0. and 4) save it in a format of ABC - Day xx - Female.sav
figure1
Ultimately, I will have 100 separate files saved in ABC - Day(1 to 100) - Female.sav.
So far I have been doing them one by one, but I am wondering if there is a way to automate this process?
Here is my code so far:
***DAY 1***.
***open file***.
GET
FILE='ABC - Day 1 - Female_May 6, 2022_09.24.sav'.
DATASET NAME DataSet1 WINDOW=FRONT.
***Drop a row of data***.
SELECT IF (Status=0).
***Add Day***.
COMPUTE DAY = 1.
EXECUTE.
***save file***.
SAVE OUTFILE='Testing\ABC - Day 1 - Female.sav'
/COMPRESSED.
***DAY 2***.
***open file***.
GET
FILE='ABC - Day 2- Female_May 6, 2022_09.24.sav'.
DATASET NAME DataSet1 WINDOW=FRONT.
***Drop a row of data***.
SELECT IF (Status=0).
***Add Day***.
COMPUTE DAY = 2.
EXECUTE.
***save file***.
SAVE OUTFILE='Testing\ABC - Day 2 - Female.sav'
/COMPRESSED.
***DAY 3***.
***open file***.
GET
FILE='ABC - Day 3- Female_May 6, 2022_09.24.sav'.
DATASET NAME DataSet1 WINDOW=FRONT.
***Drop a row of data***.
SELECT IF (Status=0).
***Add Day***.
COMPUTE DAY = 3.
EXECUTE.
***save file***.
SAVE OUTFILE='Testing\ABC - Day 3 - Female.sav'
/COMPRESSED.
I have read that for repetitive commands like this you could write a Macro, but I couldn't find good resources on this? If anyone has any suggestions or pointers, I would greatly appreciat that.
Solution 1:[1]
The following macro will do what you need - it iterates over the !day parameter and performs the sequence you described.
Note - skipped lines in the macro are necessary - do not delete.
define !dofiles ()
!do !day=1 !to 100
***open file***.
!concat("GET FILE='ABC - Day ", !day, " - Female_May 6, 2022_09.24.sav'. ")
***Drop a row of data***.
SELECT IF (Status=0).
***Add Day***.
COMPUTE DAY = !day .
***save file***.
!concat("SAVE OUTFILE='Testing\ABC - Day ", !day , " - Female.sav' /COMPRESSED. ")
dataset close all.
!doend
!enddefine.
* Now that the macro is defined you can call it using it's name.
!dofiles .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
