I would like to drop missing observations based on their position in the dataset, i.e. whenever they are at the end of a group. I used to have a dataset where each variable was a city and observations were individuals, so for cities with a few individuals only, the rest of the "column" (because it comes from Excel) would be missing because individuals were filled in other cities for this observation.
Now that I reshaped this data into an appropriate, format, I would like to drop all the missing variables that are located at the end of each city, i.e. from the last non missing observation to the end of the dataset. Now there are missing individuals that I want to keep, and those are the ones who just didn't fill their names and should be between two non-missing observations.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str5 city str2 individual "city1" "a" "city1" "b" "city1" "c" "city1" "" "city1" "d" "city1" "" "city1" "" "city1" "" "city1" "" "city2" "a" end
The idea here is to drop all the observations from the last nonmissing value i.e. city1, d to the first observation of the next group i.e. city2, a. But the missing value between city1, c and city2, d should be kept. How can I do something like this?
0 Response to Drop missing observations at the end of each group
Post a Comment