*! version 1.2.2 15may2018

Title

expandrank -- Expand and rank observations

Syntax

expandrank [=]exp [if] [in]
[, base(real) name(string) ordered sort(varlist)]

Main
base(real) sets a base value for the ranking variable (default is 1)

name(string) sets the name of the ranking variable (default is rank)

ordered ranked observations in the expanded dataset are listed in numerical order (from 1 to exp, requires more memory than the default command)

sort(varlist) the expanded dataset is sorted on varlist and rank (requires more memory & comp. time than the default command)

Description

expandrank is a wrapper for expand which runs the command and
creates a ranking variable (rank) that assigns values 1...n to the 1st...nth duplicate observations.

+-------------------+
| name age rank |
|-------------------|
| Alice 20 1 |
+------------+ | Alice 20 2 |
| name age | | Alice 20 3 |
+------------+ | Alice 20 4 |
| Alice 20 | | Alice 20 5 |
|------------| --------> |-------------------|
| Bob 63 | | Bob 63 1 |
+------------+ | Bob 63 2 |
| Bob 63 3 |
| Bob 63 4 |
| Bob 63 5 |
+-------------------+

This is analogous to running the expand command followed by:

. bysort name: gen rank = _n

expandrank avoids the computationally-intensive sorting,
which is appreciated when handling large data sets.

Examples

Setup

. webuse stackxmpl

. list

Expand by 3 and create the ranking variable (note that the duplicate observations are not stored in numerical order)

. expandrank 3

. list, sep(0)

Expand by 3 and have the duplicate observations stored in numerical order

. webuse stackxmpl

. expandrank 3, ord

. list, sep(3)

Expand by 3 and have the initial observations sorted in reverse

. webuse stackxmpl

. gen id = - _n

. expandrank 3, sort(id)

. list, sep(3)

Expand by 3, observations stored in numerical order, count rank from 2000

. webuse stackxmpl

. expandrank 3, ord base(2000)

. list, sep(3)

Contact

Jan Kabátek, The University of Melbourne

j.kabatek@unimelb.edu.au