*! version 1.0.1 09sep2020

tolong -- Faster reshape long

Syntax

Basic syntax

tolong stubname [stubname ...] [,
i(varlist) j(newvar) sort]

Syntax with renaming

tolong newvar=stubname
[newvar=stubname ...] [,
i(varlist)
j(newvar) sort]

where each stubname is one of the following:

stubname variables of the form varj with numeric
j, string j, or both

stubname* same as above

stubname# varj with numeric j

stubname@ varj with string j

and *, #, and @ characters may be specified at any place in
stubname to indicate the location of j; see the
Examples.

Description

tolong converts data from wide to long form as follows:

long
wide +
+
+
+ | i j stub |
| i stub1 stub2 | |
|
|
| tolong | 1 1 4.1 |
| 1 4.1 4.5 | <--------> | 1 2 4.5 |
| 2 3.3 3.0 | | 2 1 3.3 |
+
+ | 2 2 3.0 |
+
+

Options

i(varlist)
specifies the variables whose unique values denote a logical observation.
If you omit this option, the i variable is generated as the observation
number and will be named _i in the reshaped data.

j(newvar)
specifies the variable that will hold j values from the variable
names that match stubnames.
If you omit this option, the j variable will be named _j.

sort specifies that the data be sorted on i() and j() variables
after the reshape.
The default is to keep the data arranged in the original order of the
i() variables.

Remarks

Major differences between tolong and reshape long are as follows:

1. tolong does not require or assert that observations are unique
on i(). You can confirm that the variables specified in i()
uniquely identify the observations before using tolong with the
isid command.

2. When varlist contains variables that can be indexed by both
numeric j and string j tolong provides greater control.
The user can choose both (by default or with the * wildcard),
numeric only (# wildcard), or string only (@ wildcard).
reshape long reshapes on numeric j only (default)
or both numeric and string values (with the string option).
reshape long uses the @ wildcard to control the location of
both numeric and string indexes, thus there is no way to isolate just
string indexes with reshape long.

3. By default, tolong does not sort the data on i() and j()
variables after the data are reshaped. The reshaped data are ordered
by the original sort order of i() variables and within each i()
group the data are ordered by the new j() variable. The user can
specify option sort to have the reshaped data sorted by i()
and j(), which replicates the behavior of reshape long.

Examples

Setup

. clear

. set obs 2

. gen id = _n

. gen x1 = rnormal()

. gen x2 = rnormal()

. gen xm = rnormal()

. gen xf = rnormal()

. list

Convert data to long form; x# matches x1 and x2. Equivalent
to reshape long x, i(id)

. preserve

. tolong x#, i(id)

. list, sepby(id)

. restore

Convert data to long form; x* matches x1, x2, xm,
and xf. Equivalent to reshape long x, i(id) string

. preserve

. tolong x*, i(id)

. list, sepby(id)

. restore

Convert data to long form; x@ matches xm and xf.

. preserve

. tolong x@, i(id)

. list, sepby(id)

. restore

With renaming, reshape x1 and x2 into xnum, and xm
and xf into xstr.

. preserve

. tolong xnum=x# xstr=x@, i(id)

. list, sepby(id)

. restore

Rename x1 x2 xm xf to x1x x2x xmx xfx

. rename (x*) (x*x)

Convert data to long form, x#x matches x1x and x2x. Equivalent
to reshape long x@x, i(id).

. preserve

. tolong x#x, i(id)

. list, sepby(id)

. restore

Convert data to long form, x*x matches x1x, x2x, xmx,
and xfx. Equivalent to reshape long x@x, i(id) string.

. preserve

. tolong x*x, i(id)

. list, sepby(id)

. restore

Convert data to long form, x@x matches xmx and xfx.

. preserve

. tolong x@x, i(id)

. list, sepby(id)

. restore

Author

Rafal Raciborski

rraciborski@gmail.com