Encode multiple string variables into numeric
Syntax
multencode strvarlist [if] [in] , generate(newvarlist) [ label(name) force ]
Description
multencode creates new numeric variables newvarlist, with value labels defined and attached, based on the string variables strvarlist. The same set of value labels is used for all the new variables. By default a new set of labels will be created with the same name as the first variable in strvarlist. Optionally, a name may be specified using the label() option. In either case, no existing set of value labels with the same name will be used, unless the force option is specified, in which case those value labels will be over-written. In this way, the user is assured that the new variables will have the same alphabetically ordered set of value labels, provided as usual that the request does not breach any limits that apply.
Do not use multencode if strvarlist contains numbers that merely happen to be stored as strings. Instead, use destring. See destring.
Remarks
multencode uses Mata to determine the distinct values of the variables to be encoded. Thus Stata 9 up is required.
A multiple decode is easy enough, perhaps as easy as
foreach v of var varlist { decode `v', gen(s_`v') }
- so long as no variable name is close enough to the limit of 32 characters.
Options
generate() is required and specifies the names of the variables to be created. Hyphenated lists such as nvar1-nvar8 are allowed.
label() specifies the name of the value label to be created. If label() is not specified, multencode attempts to use the same name for the label as the first existing variable specified. In neither case will an existing set of value labels be overwritten unless force is also specified.
force specifies that any existing value labels with the same name as implied by other choices will be overwritten. Only rarely should this option be used: it may have serious consequences for other variables. Proceed with extreme caution and never use if in doubt.
Example
. multencode svar1-svar8, gen(nvar1-nvar8)
Author
Nicholas J. Cox, Durham University, UK n.j.cox@durham.ac.uk
Acknowledgments
This problem was suggested on Statalist by Moleps Islon.
Also see
help for encode, destring, label