How do I delete observations with no data in Stata?

user2830684 picture user2830684 · Aug 6, 2014 · Viewed 25.9k times · Source

I have data with IDs which may or may not have all values present. I want to delete ONLY the observations with no data in them; if there are observations with even one value, I want to retain them. Eg, if my data set is:

ID val1 val2 val3 val4
1 23 . 24 75
2 . . . .
3 45 45 70 9

I want to drop only ID 2 as it is the only one with no data -- just an ID.

I have tried Statalist and Google but couldn't find anything relevant.

Answer

Dimitriy V. Masterov picture Dimitriy V. Masterov · Aug 6, 2014

This will also work with strings as long as they are empty:

ds id*, not
egen num_nonmiss = rownonmiss(`r(varlist)'), strok
drop if num_nonmiss == 0

This gets a list of variables that are not the id and drops any observations that only have the id.