Stata 15 help for _labels2names


[P] _labels2names -- Using value labels as Stata names


_labels2names [varname] [if] [in] [, missing nolabel stub(name) indexfrom(#) nointegers renumber namelist(names)]


_labels2names is a programmer's tool that constructs Stata names for each unique value of the categorical variable varname. _labels2names tries to use the value labels attached to varname to generate names (identifiers), resorting to the value (if it is a nonnegative integer) or a default name when the labels are not valid Stata names; see [P] syntax.

_labels2names will ignore value labels that happen to be nonnegative integers unless the renumber option is specified.


missing requests that missing values be treated as valid categories.

nolabel causes _labels2names to ignore value labels attached to varname.

stub(name) specifies a stub for default name generation in cases where a label is not a valid Stata name. The default names are of the form name#, where # is the index for the default name.

indexfrom(#) specifies that the default names are to be indexed starting from # instead of 1.

nointegers prevents _labels2names from accepting nonnegative integer values as valid names.

renumber allows nonnegative integer value labels to remap the values they represent.

namelist(names) specifies an exclusion list; names that are not allowed.


. sysuse auto, clear (1978 Automobile Data) . _labels2names foreign . sreturn list macros: s(indexfrom) : "3" s(labels) : "Domestic Foreign" s(namelist) : "Domestic Foreign" s(names) : "Domestic Foreign" s(n_cat) : "2" . label define badlabs 1 "first cat" 2 "" 3 "4" 4 "four" . label val rep badlabs . _labels2names rep, stub(mystub) index(3) . sreturn list macros: s(indexfrom) : "8" s(labels) : "`"first cat"' 4 four 5" s(namelist) : "mystub3 mystub4 mystub5 four 5" s(names) : "mystub3 mystub4 mystub5 four 5" s(n_cat) : "5"

Stored results

_labels2names stores the following in s():

Macros: s(n_cat) number of categories in varname s(names) names generated from varname s(namelist) names from namelist() option with s(names) appended, for subsequent calls s(labels) label associated with each name in s(names) s(indexfrom) new value for indexfrom() option on subsequent calls

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index