R124
R124

Reputation: 21

Replacing variable entries to be the same in each group

I'm working with panel data in Stata, and I have a set up like the following:

ID year value
1 2010
1 2011 20
1 2012 20
1 2013
1 2014
2 2010
2 2011 14
2 2012 14
2 2013 14
2 2014 14

and I want to change the blank entries to be the same as the other entries within that ID, for any year. I.e., I want something like the following:

ID year value
1 2010 20
1 2011 20
1 2012 20
1 2013 20
1 2014 20
2 2010 14
2 2011 14
2 2012 14
2 2013 14
2 2014 14

What do you recommend?

Upvotes: 0

Views: 171

Answers (1)

TheIceBear
TheIceBear

Reputation: 3255

If the value in variable value are always the same within id you can use this:

* Example generated by -dataex-. For more info, type help dataex
clear
input byte id int year byte value
1 2010  .
1 2011 20
1 2012 20
1 2013  .
1 2014  .
2 2010  .
2 2011 14
2 2012 14
2 2013 14
2 2014 14
end

*Get mean of values within id
bysort id : egen value2 = mean(value) 

*Transfer values back to original var to maintain var labels etc. then drop value2
replace value = value2
drop value2

Upvotes: 1

Related Questions