Hi everyone
I have a problem concerning identifying the first time a respondent occurs in my data. The reason being that I only wish to continue with the first occurrence for each individual in my further analysis.
For the purpose I have a year variable, an identifier and a happening
An individual can figure multiple times in the same year. The identifier is just a unique number. Lastly, the happening is a binary variable [0;1], but I’m only interested in the respondents that have 1 in this case.
I haven't quite been able to find something that does the trick in past threads.
Here's an example of how my data looks.
I've tried
but that just gives me a variables containing only 1's.
What I'm looking for is a counting variable that tells me the nth time this identifyer is occuring. From there I could go on and only keep if counter==1
The end result could look like this:
So, in short: How do I construct a "countingvariable" that tells me what time it is that the specific person/identifier occurs? It is importance to note here, that the "first" time is thought of as chronological, so the earliest observation you might say. That's why I have tried to sort by year.
Kind regards
Mads
I have a problem concerning identifying the first time a respondent occurs in my data. The reason being that I only wish to continue with the first occurrence for each individual in my further analysis.
For the purpose I have a year variable, an identifier and a happening
An individual can figure multiple times in the same year. The identifier is just a unique number. Lastly, the happening is a binary variable [0;1], but I’m only interested in the respondents that have 1 in this case.
I haven't quite been able to find something that does the trick in past threads.
Here's an example of how my data looks.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(year identifier occurence) 2011 111 1 2012 111 1 2013 111 1 2010 211 1 2015 211 1 2009 311 1 2011 311 1 2011 311 1 2014 311 1 2009 411 1 2010 411 1 2011 411 1 2012 411 1 2013 411 1 2015 511 1 2016 511 1 2001 611 1 2011 611 1 2000 711 1 2001 811 1 2001 811 1 2008 811 1 2011 911 1 2012 911 1 2013 911 1
Code:
bysort identifier year: gen counter=_N
What I'm looking for is a counting variable that tells me the nth time this identifyer is occuring. From there I could go on and only keep if counter==1
The end result could look like this:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(year identifyer occurence counter) 2011 111 1 1 2012 111 1 2 2013 111 1 3 2010 211 1 1 2015 211 1 2 2009 311 1 1 2011 311 1 2 2011 311 1 3 2014 311 1 4 2009 411 1 1 2010 411 1 2 2011 411 1 3 2012 411 1 4 2013 411 1 5 2015 511 1 1 2016 511 1 2 2001 611 1 1 2011 611 1 2 2000 711 1 1 2001 811 1 1 2001 811 1 2 2008 811 1 3 2011 911 1 1 2012 911 1 2 2013 911 1 3 end
Kind regards
Mads
Comment