Dear Statalist users,
I have the following data:
id year date value
1 2005 1-Feb-06 5
1 2005 21-Feb-06 10
1 2005 19-Feb-07 20
1 2006 11-Jan-07 30
1 2006 30-Mar-07 35
1 2007 11-Jan-07 40
2 2005 17-Feb-06 50
2 2006 18-Jun-11 60
2 2006 18-Jun-12 70
...
(I have code below for getting this data into Stata)
My problem is the following:
I would like to add two columns to this dataset.
For each row, I need the value for the previous year (with the same id) with the most recent date that is prior to that row's date, and the date of that value. So, for the example above, the resulting dataset would be:
id year date value valueB dateofvalueB
1 2005 1-Feb-06 5 . .
1 2005 21-Feb-06 10 . .
1 2005 19-Feb-07 20 . .
1 2006 11-Jan-07 30 10 21-Feb-06
1 2006 30-Mar-07 35 20 19-Feb-07
1 2007 11-Jan-07 40 30 11-Jan-07
2 2005 17-Feb-06 50 . .
2 2006 18-Jun-11 60 50 17-Feb-06
2 2006 18-Jun-12 70 50 17-Feb-06
...
I an wondering if this can be done without a loop and if not what would be an efficient way of doing this.
Thanks in advance for your help!
Ana
==============
input id year str20 date value
1 2005 "1-Feb-06" 5
1 2005 "21-Feb-06" 10
1 2005 "19-Feb-07" 20
1 2006 "11-Jan-07" 30
1 2006 "30-Mar-07" 35
1 2007 "11-Jan-07" 40
2 2005 "17-Feb-06" 50
2 2006 "18-Jun-11" 60
2 2006 "18-Jun-12" 70
end
I have the following data:
id year date value
1 2005 1-Feb-06 5
1 2005 21-Feb-06 10
1 2005 19-Feb-07 20
1 2006 11-Jan-07 30
1 2006 30-Mar-07 35
1 2007 11-Jan-07 40
2 2005 17-Feb-06 50
2 2006 18-Jun-11 60
2 2006 18-Jun-12 70
...
(I have code below for getting this data into Stata)
My problem is the following:
I would like to add two columns to this dataset.
For each row, I need the value for the previous year (with the same id) with the most recent date that is prior to that row's date, and the date of that value. So, for the example above, the resulting dataset would be:
id year date value valueB dateofvalueB
1 2005 1-Feb-06 5 . .
1 2005 21-Feb-06 10 . .
1 2005 19-Feb-07 20 . .
1 2006 11-Jan-07 30 10 21-Feb-06
1 2006 30-Mar-07 35 20 19-Feb-07
1 2007 11-Jan-07 40 30 11-Jan-07
2 2005 17-Feb-06 50 . .
2 2006 18-Jun-11 60 50 17-Feb-06
2 2006 18-Jun-12 70 50 17-Feb-06
...
I an wondering if this can be done without a loop and if not what would be an efficient way of doing this.
Thanks in advance for your help!
Ana
==============
input id year str20 date value
1 2005 "1-Feb-06" 5
1 2005 "21-Feb-06" 10
1 2005 "19-Feb-07" 20
1 2006 "11-Jan-07" 30
1 2006 "30-Mar-07" 35
1 2007 "11-Jan-07" 40
2 2005 "17-Feb-06" 50
2 2006 "18-Jun-11" 60
2 2006 "18-Jun-12" 70
end
Comment