Create new variable from labels stata
In Stata, you can create a new variable from existing labels using the label
command. Here are a few ways to do it:
Method 1: Create a new variable with the same values as the labels
generate newvar = label(varname)
Replace varname
with the name of the variable whose labels you want to use. This will create a new variable newvar
with the same values as the labels of varname
.
Method 2: Create a new variable with the labels as strings
generate newvar = strtrim(label(varname))
This will create a new variable newvar
with the labels of varname
as strings. The strtrim()
function is used to remove any trailing spaces from the labels.
Method 3: Create a new variable with the labels as numeric values
generate newvar = numlabel(varname)
This will create a new variable newvar
with the labels of varname
as numeric values. The numlabel()
function converts the labels to numeric values, where each unique label is assigned a unique number.
Example
sysuse auto
generate make_label = label(make)
list make_label
+----------------+
| make_label |
|----------------|
| 1. American |
| 2. Ford |
| 3. Chevrolet |
| 4. Plymouth |
| 5. AMC |
+----------------+
In this example, we create a new variable make_label
from the labels of the make
variable. The list
command is used to display the values of make_label
.