Introduction
In a previous blog entry, we discussed recoding between string and numeric formats in Stata. In this blog, we’ll show how you to recode numeric variables in Stata.
Create Data
Let’s assume that you administered a survey with four questions that measure a construct such as confidence. First, we’ll create mock data, then we’ll show you how to recode them.
set obs 30
gen subj = _n
label variable subj "Subject #"
gen q1_a = runiform(1,7)
gen q2_a = runiform(1,7)
gen q3_a = runiform(1,7)
gen q4_a = runiform(1,7)
gen q1 = round(q1_a)
gen q2 = round(q2_a)
gen q3 = round(q3_a)
gen q4 = round(q4_a)
drop q1_a q2_a q3_a q4_a
edit
A Recoding Scenario
Let’s say you were using a Likert-style scoring approach that is supposed to be:
1 = completely disagree
2 = disagree
3 = somewhat disagree
4 = neither disagree nor agree
5 = somewhat agree
6 = agree
7 = completely agree
However, for whatever reason, your coding was backwards, and what you ended up with was:
1 = completely agree
2 = agree
3 = somewhat agree
4 = neither disagree nor agree
5 = somewhat disagree
6 = disagree
7 = completely disagree
Only the value of 4 is correct. Otherwise, you need to recode the variables as follows:
1 becomes 7
2 becomes 6
3 becomes 5
5 becomes 3
6 becomes 2
7 becomes 1
How to Recode
What we’ll do in the code below is to clone the variables we are recoding before recoding. That’s so that we can confirm that the recoding was done correctly, which is easier to do if we can compare the values.
clonevar q1_a = q1
recode q1_a 1=7 2=6 3=5 5=3 6=2 7=1
clonevar q2_a = q2
recode q2_a 1=7 2=6 3=5 5=3 6=2 7=1
clonevar q3_a = q3
recode q3_a 1=7 2=6 3=5 5=3 6=2 7=1
clonevar q4_a = q4
recode q4_a 1=7 2=6 3=5 5=3 6=2 7=1
Confirm the Recode
Let’s see if the recoding followed the scheme we outlined above:
list q1 q1_a q2 q2_a q3 q3_a q4 q4_a in 1/30
Our side-by-side comparisons indicated that the recoding worked precisely as we wanted it to.
BridgeText can help you with all of your statistical analysis needs.