*! Version 2.3.1 NJC 6 February 2001  STB-60 dm89
* 2.3.0 NJC 22 November 2000
* 2.2.0 NJC 21 November 2000
* 2.1.0 NJC 27 October 2000
* 2.0.0 NJC 22 April 1999
* 1.1.0 NJC 5 Feb 1997
* drop variables (optionally observations) with all (optionally any)
* values missing
program define dropmiss
	version 6.0
/*
	allowed syntax
	either
		dropmiss [varlist] [if] [in], Obs [ Trim Piasm any ]
	or
		dropmiss [varlist], [ Trim Piasm any ]
*/
	capture syntax [varlist] [if] [in] , Obs [ Trim Piasm any ]
	if _rc {
		syntax [varlist] [, Trim Piasm any ]
	}

	tokenize `varlist'
	local nvars : word count `varlist'
	local i = 1

	qui {
	        if "`obs'" == "obs" {
			marksample touse, novarlist
			tempvar nmiss
			gen `nmiss' = 0
			while `i' <= `nvars' {
				if "`piasm'" != "" {
					local or `" | `trim'(``i'') == ".""'
				}
				capture confirm string variable ``i''
				if _rc {
			                replace `nmiss' = /*
					*/ `nmiss' + missing(``i'')
				}
				else {
					replace `nmiss' = /*
				*/ `nmiss' + (missing(`trim'(``i'')) `or')
				}
                		local i = `i' + 1
		        }
			if "`any'" == "" {
				replace `nmiss' = `nmiss' == `nvars'
			}
		        noi drop if `nmiss' & `touse'
	        }
        	else {
	        	while `i' <= `nvars' {
				capture confirm string variable ``i''
				if "`piasm'" != "" {
					local or `" | `trim'(``i'') == ".""'
				}
				if _rc {
			                count if missing(``i'')
				}
				else {
					count if missing(`trim'(``i'')) `or'
				}
				local drop = /*
				*/ cond("`any'" != "", r(N), r(N) == _N)
        		        if `drop' {
					local dropped "`dropped'``i'' "
					drop ``i''
				}
                		local i = `i' + 1
			}
			if "`dropped'" != "" {
				noi di in bl "(`dropped'dropped)"
			}
        	}
	}
end