ChinG
ChinG

Reputation: 125

Matching different variables for the same observation

I am encountering some difficulty with a dataset that I am analyzing with Stata. The dataset I have is a repeated cross section of the following form:

Individual Year Age VarA VarB VarC

Variable C has been calculated for each individual by year, using the egen command. As a result, this variable is year specific. I now want to match the value of this variable corresponding to the year when each individual was x years old. (I create this new variable by the transform variableD=Year-Age+x). I want to match the value of Variable C that was obtained in the year "variableD" for each individual.

Upvotes: 0

Views: 123

Answers (1)

dimitriy
dimitriy

Reputation: 9460

Here's an example of how to do this with a user-written xfill:

net install xfill, from("http://www.sealedenvelope.com/")
webuse nlswork, clear
duplicates drop idcode age, force
gen x=20 if mod(idcode,2)==1
replace x=25 if mod(idcode,2)!=1
bys idcode year: egen var_c = mean(ln_wage)

bys idcode: gen var_c_at_x = var_c if age == x
xfill var_c_at_x, i(idcode)

edit idcode ln_wage year age x var_c* 

Upvotes: 2

Related Questions