Skip to content. | Skip to navigation

Personal tools

Value labels

 

Value labels: words in place of numbers.

Value labels give more description than the integer values of a variable and can make it more user-friendly.

Stata does not allow value labels to be associated to a range of values (eg. 1-10 "low" 11-20 "med" 21-30 "high"). It is possible to get the same results by defining the same value label one-by-one to all integer values in the range. The foreach command may come to your rescue if the range is long.

Stata does not allow value labels to be associated with non-integer data (eg. 1.5 "low" 2.1 "med" 3.3 "high").

Examples of the label command are shown below in a do-file.


clear
use "t:\statatut\examfac2.dta"
keep facid q102_1-q102_5 q103_1-q103_5
su

/************************* from log ****************************

Variable |     Obs        Mean   StdDev      Min        Max
---------+-----------------------------------------------------
   facid |     503    24824.06   18478.16        101      73141  
  q102_1 |     503    10.66799   24.63234          1         99  
  q102_2 |     503    17.27038   30.76907          1         99  
  q102_3 |     503    29.41551   40.11562          1         99  
  q102_4 |     503     46.0159   45.86208          1         99  
  q102_5 |     503    55.77137   46.23995          1         99  
  q103_1 |     503    14.60835   26.57987          0         99  
  q103_2 |     503    19.76143   31.74228          0         99  
  q103_3 |     503     30.9006    40.6609          0         99  
  q103_4 |     503    46.54871   46.02594          0         99  
  q103_5 |     503    56.27634   46.30981          0         99 
 
****************************************************************/

/* Notice that the data are numeric data. */

/* See which value labels are associated with which variables. */

describe

/** Value labels associate character strings to data when printed. **/
 
list q102_* in 1/10
/************************* from log *******************************************

            q102_1         q102_2         q102_3         q102_4         q102_5 
  1.     PH NurseB          Other        ClinOff             99             99 
  2.       ClinOff             99             99             99             99 
  3.      NurseOff      PH NurseB      PH NurseB       MCH Aide       MCH Aide 
  4.       ClinOff        ClinOff  Nurse/Midwife       MCH Aide             99 
  5.      MCH Aide    NurseAssist        ClinOff             99             99 
  6. Nurse/Midwife        ClinOff  Nurse/Midwife             99             99 
  7.   NurseAssist    NurseAssist             99             99             99 
  8.       ClinOff    NurseAssist    NurseAssist             99             99 
  9.       ClinOff    NurseAssist    NurseAssist             99             99 
 10. Nurse/Midwife             99             99             99             99 
  
********************************************************************************/

/* Notice that the data now appear to be character data. */
 
/** List all value labels stored in memory. **/
label dir

/** from log ***
yesno
title
type
auth
urb
****************/


/** Show what the value label title is. **/
label list title

/******* from log ***********
title:
           1 Doctor
           2 AssistMedOff
           3 ClinOff
           4 AssistClinOff
           5 NurseOff
           6 Nurse/Midwife
           7 PH NurseB
           8 MCH Aide
           9 NurseAssist
          10 Other
*****************************/
/* These numbers are the actual data that STATA stores for each variable. */
/* Notice that there is no value label for responses of 99.  Stata prints
   the actual data for un-formatted responses. */


/** Remove the value label title. **/ 
label drop title
list q102_* in 1/10

/* Re-create the title value label. */
#delimit ;
 label define title 
            1 "Doctor"
            2 "AssistMedOff"
            3 "ClinOff"
            4 "AssistClinOff"
            5 "NurseOff"
            6 "Nurse/Midwife"
          ;

/* Add more to the title value label. */
label define title
            7 "PH NurseB"
            8 "MCH Aide"
            9 "NurseAssist"
           10 "Other"
           11 "Dr. Doolittle"
             , add
      ;;;;;
#delimit cr

/* Change the title value label. */
label define title 11 "", modify

/* Re-assign the title value label to each q102_* variable. */
 
foreach v of varlist q102* {
   label val `v' title
}
list q102* in 1/10

/* list the data but without the labels */

list q102* in 1/10, nolabel

exit

Questions:

1. How many characters long can a value label be? Answer.


2. Can character data be assigned a value label? Answer.


3. Can value labels can be given the same name as the variable to which they are assigned? Answer.

 


Answers:

 

1. The maximum length for a value label is 32,000 characters. See help limits

for this and almost all other limits in Stata.

Back to question 

 


 

2. No. Only interger, numeric data can have a value label assigned to it.

Back to question 

 


 

 

3. Yes. For example, you could have a value label gender assigned to the variable gender

.

Back to question 

 


Review again?

 

Another topic?

Questions or comments? If you are affiliated with the Carolina Population Center, send them to Phil Bardsley.