Variable Names, Labels, and Values

Variable names

When it comes to naming variables, some people prefer camel casing (e.g. GearRatio); some prefer underscore (e.g. gear_ratio); and others prefer lowercase abbreviations (e.g. gr). We may also develop our own naming convention. But whichever we use, we should keep to one style throughout.

To change a variable's name, use rename oldvar newvar.
. rename rep78 repair_record

Variable labels

Each variable can have a label attached to it, which provides more information of a variable.

label variable varname “label” attaches a label to a variable.
. label variable rep78 “repair_record_78”

Value labels

When browsing the auto data, you may have noticed that the foreign variable has values of characters in blue. This means that it is a numeric variable with labeled values. If you see the values in red, then it is a string variable. The black values are numbers.

We will discuss how to define variable value labels below.

label define; label values

label define and label values attach value labels to a numeric variable in two steps:

label define labelname # label # label … first creates a label name for all the labels we are going to attach to the variable values .
label values var labelname then matches the labels to the values.

For instance,
. sysuse auto
. gen priceIndicator = (price >= 5000 & price < .)
. label define priceLabel 0 affordable 1 expensive
. label values priceIndicator priceLabel
first creates an indicator variable based on an arbitrary standard of affordability (price<5000), and then attaches the labels of “affordable” to the numeric value 0 and “expensive” to 1.

. codebook priceIndicator

priceIndicator                                                                 (unlabeled)

                  type:  numeric (float)
                 label:  priceLabel

                 range:  [0,1]                        units:  1
         unique values:  2                        missing .:  0/74

            tabulation:  Freq.   Numeric  Label
                            37         0  affordable
                            37         1  expensive
We can see how each value of priceIndicator is labeled. The label name is "priceLabel".

label list

Once we got the value label name, we can list the labels for each value of the variable.

label list labelname returns a list.
. label list priceLabel

           0 affordable
           1 expensive

In fact, in the auto data, variable foreign came with attached value lables.

. codebook foreign

foreign                                                                       Car type

                  type:  numeric (byte)
                 label:  origin

                 range:  [0,1]                        units:  1
         unique values:  2                        missing .:  0/74

            tabulation:  Freq.   Numeric  Label
                            52         0  Domestic
                            22         1  Foreign	

We can see the label name is "origin", and the value labels are "Domestic" and "Foreign".

. label list origin

           0 Domestic
           1 Foreign			

label drop

label drop labelname removes the value labels from a variable.
. label drop priceLabel

Author: Yun Dai, 2018