jeudi 4 février 2021

STATA "else if" statement including conditional lookup in time series dataset?

I have a dataset in long format:

location_1 I location_2 I year I direction
1              1          2000     1
1              2          2001     0
...

Using an if statement, I can declare whether the locations of the entity is the same in year X.

1 = same location
0 = different location

This is only part of the story. I want the 0 to be more specific. Meaning that if location_1 changes over time but location_2 stays the same I want this to be indicated (the same applies for vice-versa). However, I know I have to add a lookup in the line / year above to see whether the entity has actually changed the location again, say:

location_1 I location_2 I year I direction
1              1          2000     1
1              2          2001     2
1              2          2002     3
1              2          2003     3
1              1          2004     2
1              1          2005     1

IDs for direction

1 = same location
2 = movement in location_1
3 = different location_1 and location 2

In Stata, how do I implement the CONDITION year[-1]==year[] to my if clause.

My current code/ idea is it to do this in two steps. First by binary coding and then in a second step compare the 0 with the line above.

generate int loc1_not_loc2 =
 cond(location_1 == location_2, 1,
 cond(location_1 =! location_2, 0,
)
)``

Aucun commentaire:

Enregistrer un commentaire