In Stata, how do I recode an existing variable?

To recode variables in Stata, use the recode command. To use recode, you must provide a list of variables to be recoded and the rules associated with that change.

For a variable (e.g., q1) that contains integers ranging from 1 to 7, to collapse the values into three categories, use:

 recode q1 1=1 2=1 3/5=2 6=3 7=3

In the example above:

  • The values of 1 and 2 are recoded as 1.
  • The values of 3, 4, 5 are recoded as 2.
    Note:
    The forward slash (/) denotes a range of values (e.g., from 3 to 5), including the beginning and end of the range.
  • The values of 6 and 7 are recoded as 3.

After using recode, you cannot recover the original values. To recode and store changes into a new variable (e.g., new_q1), combine recode with the gen() option:

 recode q1 1/2=1 3/5=2 6/7=3, gen(new_q1)

For additional help, see the recode help file within Stata (in Stata, enter help recode).

If you have questions about using statistical and mathematical software at Indiana University, contact Research Analytics. Research Analytics is located on the IU Bloomington campus at Woodburn Hall 200; staff are available for consultation Monday-Friday 9am-noon and by appointment.

This is document afrh in the Knowledge Base.
Last modified on 2016-11-15 08:36:52.

Contact us

For help or to comment, email the UITS Support Center.