Statalist


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

Re: st: More than 2 arguments for by


From   Neil Shephard <nshephard@nhs.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: More than 2 arguments for by
Date   Tue, 16 Sep 2008 10:38:12 +0100

Ashim Kapoor wrote:
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?
You might want to use the slightly more direct -bysort-...

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

However, I don't think this is what you want. You haven't explained what the variables are, but lets assume that we have...

year : year of voting
state : state of voting
cnum : candidate number
votes : number of votes

It sounds as though you want to find the candidate with the maximum number of votes in each year/state (as I'm assuming your cnum will be unique within this grouping).

What you probably then want is...

bysort year stata (votes) : keep if _n == _N

The use of the brackets is explained extremely clearly -man by-....


"The varlist1 (varlist2) syntax is of special use to programmers. It verifies that the data are sorted by
varlist1 varlist2 and then performs a by as if only varlist1 were specified. For instance,

by pid (time): gen growth = (bp - bp[_n-1])/bp

performs the generate by values of pid but first verifies that the data are sorted by pid and time within pid."

So in my example your data is grouped by year and state and within that its ordered by the number of votes.

Neil

***********************************************************************
This message may contain confidential and privileged information.
If you are not the intended recipient you should not disclose, copy
or distribute information in this e-mail or take any action in reliance
on its contents. To do so is strictly prohibited and may be unlawful.
Please inform the sender that this message has gone astray before
deleting it. Thank you.

2008 marks the 60th anniversary of the NHS. It's an opportunity to pay
tribute to the NHS staff and volunteers who help shape the service, and
celebrate their achievements.

If you work for the NHS and would like an NHSmail email account, go
to: www.connectingforhealth.nhs.uk/nhsmail
***********************************************************************

*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index