'How to fill in observations for years with no data when surveys taken periodically
My data looks like this
tertiaryeduc avgeduc
.05 .71
. .
. .
. .
. .
.07 .871
. .
. .
. .
. .
.11 1.137
. .
. .
. .
. .
.15 1.378
. .
. .
. .
. .
Though I'd like to fill in the years with no data with the previous known value, so it looks like this
tertiaryeduc avgeduc
.05 .71
.05 .71
.05 .71
.05 .71
.05 .71
.07 .871
.07 .871
.07 .871
.07 .871
.07 .871
What are the commands for this?
Solution 1:[1]
As flagged in comments, this is an FAQ.
Presumably there is a structure here of distinct identifiers, which should be explicit and respected.
clear
input tertiaryeduc avgeduc
.05 .71
. .
. .
. .
. .
.07 .871
. .
. .
. .
. .
.11 1.137
. .
. .
. .
. .
.15 1.378
. .
. .
. .
. .
end
gen id = sum(tertiaryeduc < .)
bysort id : replace tertiaryeduc = tertiaryeduc[_n-1] if missing(tertiaryeduc)
list, sepby(id)
+-------------------------+
| tertia~c avgeduc id |
|-------------------------|
1. | .05 .71 1 |
2. | .05 . 1 |
3. | .05 . 1 |
4. | .05 . 1 |
5. | .05 . 1 |
|-------------------------|
6. | .07 .871 2 |
7. | .07 . 2 |
8. | .07 . 2 |
9. | .07 . 2 |
10. | .07 . 2 |
|-------------------------|
11. | .11 1.137 3 |
12. | .11 . 3 |
13. | .11 . 3 |
14. | .11 . 3 |
15. | .11 . 3 |
|-------------------------|
16. | .15 1.378 4 |
17. | .15 . 4 |
18. | .15 . 4 |
19. | .15 . 4 |
20. | .15 . 4 |
+-------------------------+
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nick Cox |
