| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: program to calculate specific percentiles
Dirk Nachbar sent the following program (wrapped up in mailjunk, despite
both public and private requests sent previously not to do this):
1. Using -set trace on- would show you where there was a problem.
2. The comparison
if `by' != ""
should be
if "`by'" != ""
I guess that is where the program is failing.
The program contains various other bugs.
3. -if- and -in- are ignored in the calculations themselves.
That is, `touse' is created but not used beyond the beginning.
4. The program sets up tempvars rank, count, nmiss, but instead
creates permanent variables rank, count, miss. Thus, the second
time the program is run in a session, or if any such permanent
variable already exists, it will fail.
5. Results will be incorrect if the variable contains extended
missing values .a ... .z.
6. If Dirk or the user types
perc myvar,
then `1' will include the comma after myvar, which is not
what is wanted.
Incidentally, the program does not calculate percentiles at
all, but presumably that is a side issue. That is, Dirk
is evidently just using the term `percentile' in a non-standard way.
--------------------------------------------------------------------------------
here is a program to calculate specific percentiles. unfortunately it
reports a
syntax error and I don't know why.
capture program drop perc
program define perc
version 8.2
syntax varlist [if] [in], [by(name)] groups(int 10) gen(name)
marksample touse
qui count if `touse'
if r(N) ==0 {
error 2000
}
if `groups'>100 | `groups'<1 {
di as error "there must be between 1 and 100 groups"
exit 198
}
qui {
tempvar rank nomiss count
if `by' != "" {
bysort `by': egen rank=rank(`1')
bysort `by': gen nomiss=(`1' != .)
bysort `by': egen count=sum(nomiss)
bysort `by': gen `gen'=rank/count*`groups'
}
else {
egen rank=rank(`1')
gen nomiss=(`1' != .)
egen count=sum(nomiss)
gen `gen'=rank/count*`groups'
}
}
di "the variable " `gen' " was created"
end
*
* 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/