elegant way to select first event within one ID

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

elegant way to select first event within one ID

Postby GerineL » Thu Mar 01, 2012 8:57 am

I have a dataset containing 3 variables:

ID-number, choice (nominal), time (interval).

For instance:


ID________Choice______time
1_________1__________1200
1_________4__________1830
1_________2__________1400
1_________3__________1000
1_________3__________1710
2_________2__________1400
2_________1__________1200
3_________4__________800

etc.

For every ID, I have to know the choice at the first timepoint.
Thus, here it would be: ID1 = 3, ID2 = 1, ID3 = 4.

The amount of choices differs between participants, it ranges between 1 and around 200.

I know I can work something out with lags and if-functions, but it will be a very complex process, and I think there must be a more elegant way of doing this. Anyone got any tips?


Of course, the real dataset contains more cases and the real problem is more complex (for instance, within subjects I have to do this separately for different trials), but if anyone can point me to a basic syntax that would be great!
remover
Posts: 17
Joined: Wed Dec 01, 2010 4:04 pm

Re: elegant way to select first event within one ID

Postby remover » Thu Mar 01, 2012 11:16 am

Can you do:

SORT CASES BY time (A)
SORT CASES ID (A)

then the first case for each ID should be the first time.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: elegant way to select first event within one ID

Postby GerineL » Thu Mar 01, 2012 3:46 pm

Thank you for your suggestion.

I might not have been entirely clear, I am looking for a way to do this automatically, not manually.
So, I want spss to go through the file and give me the answer in a new variable, like this:

ID________Choice______time______FirstChoice
1_________1__________1200______3
1_________4__________1830______3
1_________2__________1400______3
1_________3__________1000______3
1_________3__________1710______3
2_________2__________1400______1
2_________1__________1200______1
3_________4__________800_______4
remover
Posts: 17
Joined: Wed Dec 01, 2010 4:04 pm

Re: elegant way to select first event within one ID

Postby remover » Thu Mar 01, 2012 5:39 pm

Yes but once you have the data set like that you can use lag to see if id is different and copy the val if not.

Not sure how elegant you're looking for but that's all I can think of.
apeape
Posts: 56
Joined: Mon May 02, 2011 6:07 pm

Re: elegant way to select first event within one ID

Postby apeape » Thu Mar 01, 2012 6:12 pm

This does what you need:

Code: Select all

data list list / id  choice  time.
begin data.
1         1          1200
1         4          1830
1         2          1400
1         3          1000
1         3          1710
2         2          1400
2         1          1200
3         4          800
end data.

aggregate
  /outfile=* mode=addvariables
  /break=id
  /time_min=min(time).

if (time eq time_min) temp = choice.

aggregate 
  /outfile=* mode=addvariables 
  /break=id 
  /firstchoice=first(temp).

delete variables temp time_min.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: elegant way to select first event within one ID

Postby GerineL » Fri Mar 02, 2012 8:53 am

@ remover: like I said in my original post, this was all I could come up with as well (using the lag function), but I was looking for something more along the lines of the option apeape provided.

@ apeape: Thanks , that's great!
I was not familiar with the time_min option in aggregate, this was indeed what I was looking for.

Who is online

Users browsing this forum: No registered users and 1 guest

cron