help elabel encoder

Title

encoder -- Encode strings into numerics with the option to replace.

Syntax

String variable to numeric variable

encoder varname [if] [in] , [generate(newvar)
label(name) noextend replace setzero]

Multiple string variables to numeric variables

encoderall [varlist] [if] [in] , [label(name) noextend noextendall setzero]

Description

encoder is identical to encode, but also includes options to (i) replace an existing variable instead of generating a new variable, and (ii) set the first labeled value to start at 0, rather than at 1. The second option can be useful, among other things, when the encoded variable will be used as a predictor variable in a regression. Another small difference from Stata's encode command is that encoded variables are always compressed to the smallest possible format.

encoderall operates on a list of variables (or on all variables when no list is given), and encode all possible variables in the list. Variables that cannot be encoded are ignored. The newly-created variables replace the original variables.

Variable labels and order are preserved when using these commands to replace existing variables. However, notes and characteristics are not preserved.

Important: the setzero option is dependent on Daniel Klein's elabel program. You will need to install elabel first if you plan to use the setzero option. You can install elabel by typing -ssc install elabel-

Code for this module is heavily based on Kenneth L. Simmon's earlier program rencode (March 2006), and should be viewed as a revised version of that program.

Options for encoder

generate(newvar) specifies the name of a variable to be created. This is required unless the replace option is specified.

label(name) specifies the name of the value label to be created or used and added to if the named value label already exists. If label() is not specified, encoder uses the same name for the label as it does for the new variable, and if a label by this name already exists then it is used.

noextend specifies that if there are values contained in varname that are not present in label(name), varname not be encoded. By default, any values not present in label(name) will be added to that label.

replace if specified causes the encoded variable to replace the original string variable, instead of generating a new variable. This may not be combined with the generate(newvar) option.

setzero specifies that encoded values should start at 0. If this option is not specified, then encoded values will begin at 1 (similar to Stata's encode program).

Options for encoderall

label(name) specifies the name of a single value label to be created or used and added to when the named value label already exists. This label will apply to all
variables encoded. If label() is not specified, encoderall uses a different label for each variable, with in each case the name of the label being the name of the variable. If there is a pre-existing label by this name then it is used.

noextend specifies that if there are values contained in varname that are not present in label(name) - or if label(name) is not specified then in any existing label that shares the name of the variable - varname not be encoded. If label(name) is not specified and no pre-existing label has the same name as the variable, then the noextend option is ignored (unless you specify the noextendall option). By default, any values not present in the label will be added to the label. If varname cannot be encoded, rencode will continue trying to encode other variables (unless you specify the noextendall option), so only variables that can be fully encoded with the relevant label end up being encoded.

noextendall implies noextend, but indicates that all variables should be able to be encoded using existing labels without extending them. If they cannot be, the command exits with an error message.

setzero specifies that all encoded variables start with values at 0. If this option is not specified, then all encoded variables start at 1 (similar to Stata's encode program).

Examples

Setup

. use https://stats.idre.ucla.edu/stat/stata/faq/hsbs, clear

Replaces string variable with encoded variable labeled with the same strings

. encoder gender, replace

Same as before but values are [0,1] rather than [1,2]

. encoder gender, replace setzero

Encodes and replaces all string variables in data, with each variable starting at 0

. encoderall, setzero

Author

David Tannenbaum

Department of Management

University of Utah

https://davetannenbaum.github.io

Also see

Manual: [D] encode

Online: encode