I have data in this person-level format:
ID TIME EVENT GENDER
1 2 1 1
2 12 0 1
3 5 1 1
4 8 0 0
5 4 1 0
- ID 1 experienced the event (1) at Time 2.
- ID 2 never experienced the event (0) by Time 12, when data collection stopped.
- ID 3 experienced the event (1) at Time 5.
- etc.
I would like to restructure it into a person-period dataset like this:
ID PERIOD EVENT GENDER
1 1 0 1
1 2 1 1
2 1 0 1
2 2 0 1
2 3 0 1
2 4 0 1
2 5 0 1
2 6 0 1
2 7 0 1
2 8 0 1
2 9 0 1
2 10 0 1
2 11 0 1
2 12 0 1
3 1 0 1
3 2 0 1
3 3 0 1
3 4 0 1
3 5 1 1
4 1 0 0
4 2 0 0
4 3 0 0
4 4 0 0
4 5 0 0
4 6 0 0
4 7 0 0
4 8 0 0
5 1 0 0
5 2 0 0
5 3 0 0
5 4 1 0
Any ideas for the syntax? I saw a solution posted in a similar post but the solution does not achieve this structure.
Thanks.
This should work, you can pass variable values for the loop.
*****************************************.
DATA LIST FREE / ID TIME EVENT GENDER .
BEGIN DATA
1 2 1 1
2 12 0 1
3 5 1 1
4 8 0 0
5 4 1 0
END DATA.
loop #i = 1 to TIME.
compute time_new = #i.
compute event_new = 0.
if #i = TIME and event = 1 event_new = 1.
xsave outfile = 'H:\temp.sav'.
end loop.
execute.
get file = 'H:\temp.sav'.
*******************************************.
XSAVE inside a LOOP is one way to do it. E.g.,
data list list / ID TIME EVENT GENDER (4f2.0).
begin data
1 2 1 1
2 12 0 1
3 5 1 1
4 8 0 0
5 4 1 0
end data.
rename variables (event = evt).
numeric period event (f2.0).
loop period = 1 to time.
- do if period LT time.
- compute event = 0.
- else.
- compute event = evt.
- end if.
- xsave outfile = "C:\temp\longfile.sav" /
keep = ID period event gender .
end loop.
execute.
get file = "C:\temp\longfile.sav" .
list.
HTH.
--
Bruce Weaver
bwe...@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."
Here is a slightly 'simpler' version.
data list list / ID TIME EVENT GENDER (4f2.0).
begin data
1 2 1 1
2 12 0 1
3 5 1 1
4 8 0 0
5 4 1 0
end data.
LOOP T=1 TO TIME.
+ COMPUTE E=0.
+ IF (T EQ TIME AND TIME LT 12) E=1.
+ XSAVE OUTFILE "C:\TEMP\Longfile.sav"
/ DROP TIME EVENT
/ RENAME (T E=TIME EVENT).
END LOOP.
EXE.
GET FILE "C:\TEMP\Longfile.sav" .