Neicooo
Neicooo

Reputation: 197

Create time variable based on binary variable using Stata

My dataset contains a year, ID, and binary value variable.

ID Year Value
1 2000 0
1 2001 0
1 2002 1
1 2003 1
1 2004 1
1 2005 1

Using Stata, I would like to create a new variable "YearValue" that takes the value of the variable "Year" when the variable value first turned 1.

ID Year Value YearValue
1 2000 0 2002
1 2001 0 2002
1 2002 1 2002
1 2003 1 2002
1 2004 1 2002
1 2005 1 2002

Thank you for your help!

Upvotes: 0

Views: 83

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

   egen wanted = min(cond(Value == 1, Year, .)), by(ID)

See https://www.stata-journal.com/article.html?article=dm0055 (especially Section 9) for this technique in context.

Upvotes: 1

Related Questions