Hi everyone,
My data looks something like this:
group id | time | value | nearest neighbor group id
1 | year1 | 10 | 3
1 | year2 | 20 | 3
1 | year3 | 30 | 3
1 | year4 | 40 | 3
2 | year1 | 100 | 1
2 | year2 | 200 | 1
2 | year3 | 300 | 1
2 | year4 | 400 | 1
3 | year1 | 1000 | 2
3 | year2 | 2000 | 2
3 | year3 | 3000 | 2
3 | year4 | 4000 | 2
What I'd like to do is generate differences of value between each group and its nearest neighbor. For the first row, I'd take group 1's year1 value and subtract that from the year1 value of its neighbor, group 3 (100-1000). What I'm stuck on is how to create a "neighbor value" column, which would match the neighbor id with a group id and pull the group's value for the correct year.
Also, different groups may have the same neighbor.
Want:
group id | time | value | nearest neighbor group id | neighbor value | difference
1 | year1 | 10 | 3 | 1000
1 | year2 | 20 | 3 | 2000
1 | year3 | 30 | 3 | 3000
1 | year4 | 40 | 3 | 4000
2 | year1 | 100 | 1 | 10
2 | year2 | 200 | 1 | 20
2 | year3 | 300 | 1 | 30
2 | year4 | 400 | 1 | 40
3 | year1 | 1000 | 2 | 100
3 | year2 | 2000 | 2 | 200
3 | year3 | 3000 | 2 | 300
3 | year4 | 4000 | 2 | 400
Using Stata 12
Thanks,
Brian
My data looks something like this:
group id | time | value | nearest neighbor group id
1 | year1 | 10 | 3
1 | year2 | 20 | 3
1 | year3 | 30 | 3
1 | year4 | 40 | 3
2 | year1 | 100 | 1
2 | year2 | 200 | 1
2 | year3 | 300 | 1
2 | year4 | 400 | 1
3 | year1 | 1000 | 2
3 | year2 | 2000 | 2
3 | year3 | 3000 | 2
3 | year4 | 4000 | 2
What I'd like to do is generate differences of value between each group and its nearest neighbor. For the first row, I'd take group 1's year1 value and subtract that from the year1 value of its neighbor, group 3 (100-1000). What I'm stuck on is how to create a "neighbor value" column, which would match the neighbor id with a group id and pull the group's value for the correct year.
Also, different groups may have the same neighbor.
Want:
group id | time | value | nearest neighbor group id | neighbor value | difference
1 | year1 | 10 | 3 | 1000
1 | year2 | 20 | 3 | 2000
1 | year3 | 30 | 3 | 3000
1 | year4 | 40 | 3 | 4000
2 | year1 | 100 | 1 | 10
2 | year2 | 200 | 1 | 20
2 | year3 | 300 | 1 | 30
2 | year4 | 400 | 1 | 40
3 | year1 | 1000 | 2 | 100
3 | year2 | 2000 | 2 | 200
3 | year3 | 3000 | 2 | 300
3 | year4 | 4000 | 2 | 400
Using Stata 12
Thanks,
Brian
Comment