Unstacking a between subjects factor to form within subjects factors
The below was originally provided by SPSS technical support and forms six columns corresponding to different repeated measures combinations with subjects as rows.
* Begin sample program. * Question: There are three subject records per case. I want a new SPSS dataset that * has only one subject record per case, and six variables * instead of the current two variables. How can I do this using SPSS? . DATA LIST FREE / idno x y . BEGIN DATA 001 34 56 001 23 45 001 39 41 002 33 11 002 87 57 002 99 36 END DATA. * Answer: * You can use the following SPSS syntax provided by SPSS technical support to * accomplish this transformation--The syntax is annotated * to provide brief descriptions of each section of code to make it easier to * adapt to your own unique circumstances--This syntax assumes * that you have three column variables in the original database: the subject * ID variable, var2, and var3. * Sort cases by ID number. DEFINE B_TO_W (ID = !tokens(1) /NVARS = !tokens(1) /VARS = !cmdend). SORT CASES BY !ID (A) . EXECUTE. * Second, we'll need a index variable which enumerates * consecutively the records within each subject id; this can * be done easily with the LAG function:. * A valid value for tasknum in case #1 is created * since there is no lagged case for it, it would return * a missing value otherwise. COMPUTE tasknum=1. DO IF ($CASENUM NE 1). IF (!ID EQ LAG(!ID)) tasknum = LAG(tasknum)+1. END IF. EXECUTE. * Now we can begin the process of collapsing the multiple * records per case into one record per case. First, list the variables * that have values we wish to transpose on the DO REPEAT statement which * in this case is var2 and var3. Next, we use the VECTOR command to * create an array of the new variables required (3 for each of the variables * identified in the DO REPEAT, and populate these variables with the value of * "score" which corresponds to the appropriate test number. * Note that the value of "tasknum" on the COMPUTE statement * will determine which of the three "trial" variables created * by the VECTOR command will get the "score" value for a * given case:. DO REPEAT trial=!VARS. VECTOR trial(!NVARS). COMPUTE trial(tasknum)=trial. END REPEAT. exe. !ENDDEFINE.
Now, let's run the macro B_TO_W
B_TO_W ID=idno nvars=3 vars=x y.
One final bit of tidying up needs to be done:
* At this point, we have all of the variables we need, and we * have their corresponding values arrayed in the appropriate * positions. We still have, however, three logical records * per substantive case, plus a good deal of "air" in the data * matrix given over to missing values. All that remains is to * "collapse" the file into itself, reducing the redundant * information in "id," and preserving only the valid values of * "trial1" to "trial3" for each subject. This we can do with * a straightforward AGGREGATE procedure, Breaking on the "id" * variable, and using the MAX function to preserve the valid * values of "trial1" to "trial3" for each case (note that the * choice of the MAX function was arbitrary here -- the same * thing could be accomplished with the SUM, MEAN, MIN, FIRST, * or LAST, functions):. AGGREGATE OUTFILE=* /BREAK=idno /var11= MAX(x1) /var12= MAX(x2) /var13= MAX(x3) /var21= MAX(y1) /var22= MAX(y2) /var23= MAX(y3) . EXECUTE. LIST. * Do not forget to save your newly-created data file under a new name. * End sample program.