I think this calls for a reshape, for example :
use mydata
reshape long @pc, i(id) j(party) string
bysort id (pc): gen byte rank=_N-_n+1
keep if rank<4
keep id party rank
reshape wide party, i(id) j(rank)
sort id
save top3
use mydata
sort id
merge id using top3
assert _merge==3
This assumes your file is called mydata and that there is some sort of
election id variable, called id. It keeps the top 3 as party1 party2 and
party3.
Michael Blasnik
[email protected]
----- Original Message -----
From: "n1700166" <[email protected]>
To: <[email protected]>
Sent: Sunday, January 25, 2004 12:04 AM
Subject: st: Tactical looping
> All,
>
> Some months ago, I posted a question about what code I needed in order to
> generate dummy codes to indicate whether or not a candidate won a
constituency
> election. In my dataset, I needed to do this for 3456 cases across 6
> time-points. Many of you were generous in your response to this, including
> Nick Cox, who supplied this loop:
>
> egen max = rmax(conpc labpc ldmpc natpc othpc)
> foreach x in con lab ldm nat oth {
> egen `x'win=diff(`x'pc max)
> recode `x'win (1=0) (0=1)
> }
> drop max
>
> I don't think I've changed this, so hopefully it's still correct. *This
_in no
> way_ slights the other very helpful solutions given to me.* This has
served me
> superbly over the months, but on reading an old BJPS article about the
> potential effects of tactical voting on consitituency outcomes at general
> elections, I realised I'd made a quite stupid omission: I'd coded the
winners,
> but _not_ the candidates who finished second and third!
>
> Looking at -help mathfun-, the only relevant operators here are -max- and
> -min- (as far as I can tell). Obviously, -max- cannot be used, and it
would be
> rather dicy to use -min- (since different parties finish in different
places
> in different seats at different times). Therefore, is there a solution?
I'm
> lost! Thanks.
>
> Yours,
> CLIVE NICHOLAS,
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/