I am working with a longitudinal dataset. I'm trying to create a variable to capture change between two periods in one variable, but only when a different variable remains constant.
Essentially, my goal is to create a single row for each "wave" by creating a new variable that will represent a specific change in the said variable, and then delete the left-over row for that year.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long id_num byte(wave jobcens) 101 1 3 101 2 1 101 3 1 101 5 2 101 5 3 101 6 2 101 8 3 101 9 1 101 10 2 101 10 3 101 11 1 end
From the above code, the cases below represent the type of cases I mean to capture and what I'd like them to be (6, in this case):
101 5 2
101 5 3 --> 101 5 6
101 10 2
101 10 3 --> 101 10 6
I've used the following code in the past to create a change variable:
gen change_var = .
replace change_var = 6 if x1[_n-1] == 1 & x1 == 2 & id_num[_n-1] == id_num
However, the added stipulation I face is to restrict this to only happen when the "wave" is the same. I'm wondering if I will need to turn the dataset wide to do this? And if so, any suggestions on how might I do still achieve the same goal?
Thank you in advance!
Much appreciation,
Wyatt
0 Response to Capture change in one variable while different variable is constant
Post a Comment