user3633805
user3633805

Reputation: 31

SPSS dataset restructuring involving variable for survey completion date

I'm using SPSS and have a dataset comprised of individuals' responses to a survey question. This is longitudinal data, so the subjects have taken the survey at least twice and some as many as four or five times.

My variables are ID (scale), date of survey completion (date - dd-mmm-yyyy), and response to survey question (scale).

The dataset is sorted by ID then date (ascending). Each date corresponds to survey time 1, time 2, etc. What I would like to do is compute a new variable time that corresponds to the survey completion dates for a particular participant. I would then like to use that variable to complete a long-to-wide restructuring of the dataset.

So, I'd like to accomplish the following and am not sure how to go about doing it:

1) I have something like this:

ID Date          Assessment_Answer
----------------------------------
1  01-Jan-2009   4
1  01-Jan-2010   1
1  01-Jan-2011   5
2  15-Oct-2012   6
2  15-Oct-2012   0

2) Want to compute another variable that would give me this:

ID Date          Assessment_Answer  Time
-----------------------------------------
1  01-Jan-2009   4                  Time1 
1  01-Jan-2010   1                  Time2
1  01-Jan-2011   5                  Time3   
2  15-Oct-2012   6                  Time1
2  15-Oct-2013   0                  Time2

3) And restructure so that I have something like this:

ID Time1 Time2 Time3 Time4
--------------------------
1      4     1     5
2      6     0

Upvotes: 3

Views: 282

Answers (1)

Andy W
Andy W

Reputation: 5089

You can use sequential case processing to create a variable that is a counter within each ID. So for example:

*Making fake data.
DATA LIST FREE / ID (F1.0) Date (DATE10) Assessment_Answer (F1.0).
BEGIN DATA
1 01-Jan-2009 4
1 01-Jan-2010 1
1 01-Jan-2011 5
2 15-Oct-2012 6
2 15-Oct-2012 0
END DATA.

*Making counter within ID.
SORT CASES BY Id Date.
DO IF ($casenum = 1) OR (Id <> LAG(ID)).
  COMPUTE Time = 1.
ELSE.
  COMPUTE Time = LAG(Time) + 1.
END IF.
FORMATS Time (F2.0).
EXECUTE.

Now you can use CASESTOVARS to reshape the data like you requested.

CASESTOVARS
  /ID = Id
  /INDEX = Time
  /DROP Date.

Upvotes: 1

Related Questions