How can I backwards fill the missing values with the non-missing value of the variable as per the groups within the sample?
I have the following data:
I want to fill up the missing value with the value that is not missing. But there are IDs for which no date fillup is needed as either all the date values are missing or all date values are filled up.
I want the output like this:
I tried several codes:
while, for and if loop, reverse id sorting, ipolate but none of them work
I also tried this code: bysort id: replace date = date[_n+1] if missing(date) but it only fills up one previous missing value and not the rest.
I have the following data:
I want to fill up the missing value with the value that is not missing. But there are IDs for which no date fillup is needed as either all the date values are missing or all date values are filled up.
| ID | Date |
| 1 | . |
| 1 | 1sep2020 |
| 2 | . |
| 2 | . |
| 2 | 3sep2021 |
| 3 | . |
| 3 | 4sep2021 |
| 4 | . |
| 4 | . |
| 4 | 1sep2020 |
| 5 | . |
| 5 | 5sep2021 |
| 6 | 3sep2022 |
| 6 | 3sep2022 |
| 6 | 3sep2022 |
| 7 | . |
| 7 | . |
| ID | Date |
| 1 | 1sep2020 |
| 1 | 1sep2020 |
| 2 | 3sep2021 |
| 2 | 3sep2021 |
| 2 | 3sep2021 |
| 3 | 4sep2021 |
| 3 | 4sep2021 |
| 4 | 1sep2020 |
| 4 | 1sep2020 |
| 4 | 1sep2020 |
| 5 | 5sep2021 |
| 5 | 5sep2021 |
| 6 | 3sep2022 |
| 6 | 3sep2022 |
| 6 | 3sep2022 |
| 7 | . |
| 7 | . |
while, for and if loop, reverse id sorting, ipolate but none of them work
I also tried this code: bysort id: replace date = date[_n+1] if missing(date) but it only fills up one previous missing value and not the rest.

Comment