Dropping columns/variables based on count of missing in Stata

I have a large dataset that looks like the one below. I would like to drop the variables (not the observations/rows) that have less 3 observations in the rows. In this case only variable X1 needs to be dropped.

I apologise if I am asking something obvious, however, at this point I do not have a clue on how to proceed with this.

+-----+-----+-----+-----+-----+
| ID  |  X1 | X2  | X3  | X4  |
+-----+-----+-----+-----+-----+
|  1  |  .  |  1  |  1  |  2  |    
|  2  |  .  |  2  |  2  |  3  |    
|  3  |  .  |  3  |  1  |  .  |   
|  4  |  1  |  .  |  3  |  1  |   
|  5  |  .  |  2  |  4  |  3  |   
|  6  |  2  |  3  |  .  |  .  |    
|total|  2  |  5  |  5  |  4  |   
+-----+-----+-----+-----+-----+

Answers


My interpretation is you want to drop variables that have at least 3 missing values.

You can use nmissing, from SSC (ssc install nmissing):

clear
set more off

input ///
x y z
. . 5
. 6 8
4 . 9
. . 1
5 . .
end

list

nmissing, min(3)
drop `r(varlist)'

If my interpretation is incorrect, check the help for nmissing and npresent. The syntax is flexible enough.

Edit

A re-interpretation. You want to drop variables that don't have at least 3 non-missing values:

clear
set more off

input ///
ID    X1  X2   X3   X4  
1    .    1    1    2      
2    .    2    2    3      
3    .    3    1    .     
4    1    .    3    1     
5    .    2    4    3     
6    2    3    .    .   
end

list, sep(0)

npresent, min(3)
keep `r(varlist)'

describe

Need Your Help

How to update cell background after delete

ios uitableview

I have custom backgrounds (one for first rows, other for middle rows and other for last rows). When I delete a row of the tableView (swipe to delete), if I delete the first row, the new first row t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.