## Stata 15 help for xtile

```
[D] pctile -- Create variable containing percentiles

Syntax

Create variable containing percentiles

pctile [type] newvar = exp [if] [in] [weight] [, pctile_options]

Create variable containing quantile categories

xtile newvar = exp [if] [in] [weight] [, xtile_options]

Compute percentiles and store them in r()

_pctile varname [if] [in] [weight] [, _pctile_options]

pctile_options          Description
-------------------------------------------------------------------------
Main
nquantiles(#)         number of quantiles; default is nquantiles(2)
genp(newvarp)         generate newvarp variable containing percentages
altdef                use alternative formula for calculating
percentiles
-------------------------------------------------------------------------

xtile_options           Description
-------------------------------------------------------------------------
Main
nquantiles(#)         number of quantiles; default is nquantiles(2)
cutpoints(varname)    use values of varname as cutpoints
altdef                use alternative formula for calculating
percentiles
-------------------------------------------------------------------------

_pctile_options         Description
-------------------------------------------------------------------------
nquantiles(#)           number of quantiles; default is nquantiles(2)
percentiles(numlist)    calculate percentiles corresponding to the
specified percentages
altdef                  use alternative formula for calculating
percentiles
-------------------------------------------------------------------------

aweights, fweights, and pweights are allowed (see [U] 11.1.6 weight),
except when the altdef option is specified, in which case no weights
are allowed.

pctile

Statistics > Summaries, tables, and tests > Summary and descriptive
statistics > Create variable of percentiles

xtile

Statistics > Summaries, tables, and tests > Summary and descriptive
statistics > Create variable of quantiles

Description

pctile creates a new variable containing the percentiles of exp, where
the expression exp is typically just another variable.

xtile creates a new variable that categorizes exp by its quantiles.  If
the cutpoints(varname) option is specified, it categorizes exp using the
values of varname as category cutpoints.  For example, varname might
contain percentiles of another variable, generated by pctile.

_pctile is a programmer's command that computes up to 1,000 percentiles
and places results in r(); see [U] 18.8 Accessing results calculated by
other programs.  summarize, detail computes some percentiles (1, 5, 10,
25, 50, 75, 90, 95, and 99th); see [R] summarize.

Options

+------+
----+ Main +-------------------------------------------------------------

nquantiles(#) specifies the number of quantiles.  It computes percentiles
corresponding to percentages 100*k/m for k=1, 2, ..., m-1, where m=#.
For example, nquantiles(10) requests that the 10th, 20th, ..., 90th
percentiles be computed.  The default is nquantiles(2); that is, the
median is computed.

genp(newvarp) (pctile only) specifies a new variable to be generated
containing the percentages corresponding to the percentiles.

altdef uses an alternative formula for calculating percentiles.  The
default method is to invert the empirical distribution function by
using averages, where the function is flat (the default is the same
method used by summarize; see [R] summarize).  The alternative
formula uses an interpolation method.  See Methods and formulas in
[D] pctile.  Weights cannot be used when altdef is specified.

cutpoints(varname) (xtile only) requests that xtile use the values of
varname, rather than quantiles, as cutpoints for the categories.  All
values of varname are used, regardless of any if or in restriction;
see the technical note in [D] pctile.

percentiles(numlist) (_pctile only) requests percentiles corresponding to
the specified percentages.  Percentiles are placed in r(r1), r(r2),
..., etc.  For example, percentiles(10(20)90) requests that the 10th,
30th, 50th, 70th, and 90th percentiles be computed and placed into
r(r1), r(r2), r(r3), r(r4), and r(r5).  Up to 1,000 (inclusive)
percentiles can be requested.  See nlist for the syntax of a numlist.

Examples

---------------------------------------------------------------------------
Setup
. sysuse auto

Create variable pct containing the deciles of mpg
. pctile pct = mpg, nq(10)

List the resulting data
. list pct in 1/10

Setup
. drop pct

Create variable pct containing the deciles of mpg, and create variable
percent containing the percentages
. pctile pct = mpg, nq(10) genp(percent)

List the resulting data
. list percent pct in 1/10

Setup
. drop pct percent

Create pct containing the deciles of mpg weighted by weight, and create
variable percent containing the percentages
. pctile pct = mpg [w=weight], nq(10) genp(percent)

List the resulting data
. list percent pct in 1/10

---------------------------------------------------------------------------
Setup
. webuse bp1, clear

List variable bp
. list bp, sep(4)

Create variable quart containing the quartiles of bp
. xtile quart = bp, nq(4)

List variables bp and quart
. list bp quart, sep(4)

Setup
. input class

class
1.  100
2.  110
3.  120
4.  130
5.  end

Create variable category categorizing bp using the values of class as
category cutpoints
. xtile category = bp, cut(class)

List variables bp, class, and category
. list bp class category, sep(4)

---------------------------------------------------------------------------
Setup
. sysuse auto, clear

Compute the deciles for weight and store then in r(r#)
. _pctile weight, nq(10)

List the stored results
. return list

Compute the specified percentiles for weight and store them in r(r#)
. _pctile weight, p(10, 33.333, 45 50, 55, 66.667, 90)

List the stored results
. return list
---------------------------------------------------------------------------

Stored results

pctile and _pctile store the following in r():

Scalars
r(r#)     value of #-requested percentile

```