Statalist


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

RE: st: RE: More than 2 arguments for by


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: More than 2 arguments for by
Date   Tue, 16 Sep 2008 16:52:32 +0100

That could be condensed to 

bysort year state cnum (votes) : keep if _n == _N 

-search by- points to a discursive tutorial, accessible to all: 

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step
by: step
        Q1/02   SJ 2(1):86--102                                  (no
commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

The important thing is that the parentheses notation is _not_ cosmetic
here. 

Nick 
[email protected] 

Ashim Kapoor

I made a "silly" mistake. I realized it only some time ago.

What I wanted to do was : -

sort year state cnum votes

and then ONLY do

by year state cnum : keep if _n==_N

This would bring out the item with the max no of votes in a year in a
state in a contituency.

I erred and thought that I should do by year state cnum VOTE : keep if
_n==_N.


On Tue, Sep 16, 2008 at 8:09 PM, Nick Cox <[email protected]> wrote:

> Neil Shephard and Martin Weiss made helpful comments.
>
> My guess is that what Ashim needs is
>
> bysort year state cnum (votes) : keep if _n == _N
>
> except that his report "I have ." leads me to guess that he has
missing
> values, which would be problematic with that code.
>
> Missing values are sorted to the end of any block of observations, as
> they are treated as arbitrarily large. So missing values will show up
in
> the kept data for every constituency (if that is what -cnum- indexes)
> for which they exist.
>
> Even if this guess is correct, it raises two questions.
>
> 1. Why does Ashim have missings? Are they something else, e.g. zeros?
If
> so, they should be fixed.
>
> 2. If the missings are genuine, they need to be fixed before
-sort-ing.
> Ashim's purpose seems to call for
>
> drop if missing(votes)
>
> Also -search dropmiss-.
>
> Nick
> [email protected]
>
> Ashim Kapoor
>
> I want to do the following :
>
> sort year state cnum votes
>
> by year stata cnum votes : keep if _n==_N
>
> That way I get the guy with the max no of votes.
>
> Now when I do this for 1 or 2 arguments for by then it works, but not
> for 4 arguments I have . Does by have a limit on how many arguments it
> can have ?
>
> I read -help by- and that does mention using by in the following
> fashion : -- > by year ( state) . Do I need to put in brackets for the
> other arguments?
>
> Could someone please clarify?

*
*   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