| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: RE: -egen rank ()
Scott Merryman wrote:
Do you mean something like this:
clear
input str3 v1 v2
aaa 374
bbb 213
ccc 204
gvfdg 193
gfdg 170
gfdh 166
abc 213
end
egen rank2 = rank2(v2), track
. l, ab(20)
+-------------------+
| v1 v2 rank2 |
|-------------------|
1. | aaa 374 1 |
2. | bbb 213 2.5 |
3. | ccc 204 4 |
4. | gvf 193 5 |
5. | gfd 170 6 |
|-------------------|
6. | gfd 166 7 |
7. | abc 213 2.5 |
+-------------------+
Scott
-----------------------------------------------
*! rank2 adapted from -egen, rank-
*! egen, rank - field option with tie correction
program define _grank2
version 7, missing
syntax newvarname =/exp [if] [in] [, BY(varlist) /*
*/ {Field|Track|Unique} Suffix(str)]
if ("`field'"!="") + ("`track'"!="") + ("`unique'"!="") > 1 {
di as err "{p}only one of field, track, or unique"
di "may be specified{p_end}"
exit 198
}
if ("`by'"!="") + ("`suffix'"!="") > 1 {
di as err "{p}by() and suffix() may not be specified
together{p_end}"
exit 198
}
if ("`suffix'"!="") > 0 & ("`field'"!="") + ("`track'"!="") + /*
*/("`unique'"!="") < 1 {
di as err "{p}suffix() allowed only with the field, track,"
di "or unique options{p_end}"
exit 198
}
local sign = 1
if "`field'"!="" {
local fstar = "*"
local sign = -1
}
if "`track'"!="" {
local tstar = "*"
local sign = -1
}
if "`unique'"!="" {
local ustar = "*"
}
if ("`field'"!="") + ("`track'"!="") + ("`unique'"!="") > 0 {
local space " "
}
tempvar GRV
quietly {
gen double `GRV' = `sign'*(`exp') `if' `in'
sort `by' `GRV'
if "`by'"!="" {
local by2 = `"by `by':"'
}
`by2' gen `typlist' `varlist' = _n if `GRV'<.
`ustar' `by2' replace `varlist' = `varlist'[_n-1] ///
if `GRV'<. & `GRV'==`GRV'[_n-1]
by `by' `GRV': replace `varlist' = `varlist'+(_N-1)/2
if "`by'"!="" {
local by2 = `"by `by'"'
}
label var `varlist' /*
*/ "`field'`track'`unique'`space'rank of `exp'
`by2'"
_getnewlabelname vlbl
tempvar group
if "`suffix'" != "" {
local nties = 0
local i = 1
while `i' <= _N {
count if `varlist' == `i'
if r(N) > 1 { /* there are tied ranks */
label def `vlbl' `i' "`i'`suffix'",
add
local nties = `nties' + 1
}
local i = `i' + 1
}
if `nties' {
label val `varlist' `vlbl'
}
}
}
end
-----Original Message-----
From: [email protected] [mailto:owner-
[email protected]] On Behalf Of Nikolaos A. Patsopoulos
Sent: Thursday, September 21, 2006 8:50 AM
To: [email protected]
Subject: st: -egen rank ()
Hi,
I want to create a variable containing the rank order of another one but
I cannot make tie corrections:
the data:
v6 countMain
aaa 374
bbb 213
ccc 204
gvfdg 193
gfdg 170
gfdh 166
I use egen rankMain=rank( countMain) if counter_v6==1, field
#counter_v6 is a _n variable for v6
if I don't use the /field/ parameter I get the reverse order of that I
want, e.g. aaa gets a rank of 4880 instead of 1. However using field
doesn't correct for ties (want to use the average for ties).
Any suggestions?
Thanks in advance,
Nikos
*
* 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/
*
* 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/
Exactly what I want!
Thanks.
Nikos
*
* 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/