Rabin
Rabin

Reputation: 1

Can I impute a variable conditional on another?

I am trying to impute the data about whether someone is born in the UK from wave 1 to wave 2. I suspect the egen function would work but I am not sure what the code would look like?

Table

As you can see, I need to assign the same born in the uk response for person id 1 in wave 1 to wave 2.

I know I could do it by reshaping the dataset to a wide format but do you know whether there is any other way?

Upvotes: 0

Views: 221

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

This is a Stata FAQ as accessible here.

You can copy downwards in the dataset without creating any new variables.

bysort id (wave) : replace born_in_uk = born_in_uk[_n-1] if missing(born_in_uk) 

mipolate (SSC) has a groupwise option that checks for there being more than one non-missing value. Search within www.statalist.org for mentions.

Note that egen is a command, not a function.

Upvotes: 1

Erdne Htábrob
Erdne Htábrob

Reputation: 879

I am not sure whether here born in the UK is numeric with labels or string. But, what if you would do something like:

encode born_in_UK, gen(born_num)

bysort person_id: egen born_num2=mean(born_num)
drop born_num
rename born_num2 born_num

The idea is to think of the repeating personal ids as groups and use the mean function to fill the missing values in the group. I think this should work.

Upvotes: 0

Related Questions