Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Removing variables if missing a particular observation
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Removing variables if missing a particular observation
Date
Thu, 11 Apr 2013 16:21:33 +0100
If as you say 2004m3 is the first observation
foreach var of varlist _all {
if missing(`var'[1]) drop `var'
}
should also work. The method I suggested earlier is more general.
s
Nick
[email protected]
On 11 April 2013 16:15, Nick Cox <[email protected]> wrote:
> First problem:
>
> You are confusing -drop-ping of variables and -drop-ping of
> observations. Forgetting about your loop, the generic statement
>
> drop `var' if missing(`var') in 1
>
> is illegal, as you can't simultaneously -drop- variables and
> observations. (Saying that it is "not working" is an odd description,
> unless you mean that your code produces an error message.)
>
> The loop that you want is (for example)
>
> foreach var of varlist _all {
> su `var' if date == ym(2004,3), meanonly
> if r(min) == . drop `var'
> }
>
> Second problem: Note first that _all is not pertinent here. At most it
> is a way of designating all variable names concisely, but you want all
> distinct values of a variable.
>
> More positively, this is an FAQ.
>
> FAQ . . . . . . . . . . Making foreach go through all values of a variable
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
> 8/05 Is there a way to tell Stata to try all values of a
> particular variable in a foreach statement without
> specifying them?
> http://www.stata.com/support/faqs/data-management/try-all-values-with-foreach/
>
> Nick
> [email protected]
>
>
> On 11 April 2013 15:57, Nick Baker <[email protected]> wrote:
>
>> I have a sample of mutual fund returns data and I wish to analyse only those
>> funds with returns over a particular time period. Currently my data is of
>> the format:
>>
>> Date fundret1 fundret3 fundret4
>> 2004m3 . 2.2 3.15
>> 2004m4 . 3.1 -1.5 2004m5 2.3 .
>> 2.1
>> 2004m6 1.1 . 1.8
>>
>>
>> I have two problems. I want to delete all variables that have a missing
>> value for 2004m3. At the moment I am trying the following:
>>
>> foreach var of varlist _all {
>>
>> drop `var' if missing(`var') in 1
>> }
>>
>> since 2004m3 is the first observation, but this is not working.
>>
>> A second problem is that I wish to loop over all funds. They are numbered
>> from 1 to 4722 but due to some prior deletions the sequence of funds does
>> not include every value. I do not want to use _all because the dataset will
>> include variables other than fund returns eventually. Any ideas if it
>> possible to create a loop in such circumstances?
>>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/