Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st:delete all the obervations with negative values


From   "Joseph Coveney" <[email protected]>
To   "Statalist" <[email protected]>
Subject   Re: st:delete all the obervations with negative values
Date   Sun, 3 Aug 2008 06:20:18 +0900

Mandy wrote:

Could anyone could give me some hint on how to delete all the
observations with missing values for at least one variable? I know it's
a quite simple question. But I havn't figured out a good way to deal
with this.

Here's an example to show this:
----------------------------------------------------------
ID        var1       var2       var3

1           0           2           1
2          -2           1           3
3           0          -4           9
4           1           0           1
---------------------------------------------------------
What I want to do is to drop observation whose ID is 2 and 3.

If there are not many variables in the data set, I know I could do:

. drop if var1<0| var2<0 | var3<0

But this way is inconvenient if there are a lot of variables (like 50
variables) in the data set.

--------------------------------------------------------------------------------

Martin Weiss has already shown an approach using -egen-.

Another approach is to use -foreach- to loop across variables.

foreach var of varlist var1-var50 {
drop if `var' < 0
}

Various convenient shortcuts are available in Stata for specifying lists
of variables, such as

foreach var of varlist var?? {
   . . .
}

foreach var of varlist v* {
   . . .
}

foreach var of varlist _all {
   . . .
}

I recommend reading the help files and hardcopy documentation on looping in
Stata (-foreach- and -forvalues-) and on how lists of variables (-varlist-)
are handled in Stata, including the use of "wildcards".

Joseph Coveney


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index