Introduction
Cloning variables can be very useful for data analysis. In this blog entry, we’ll show you to clone variables in Stata, both generally and conditionally.
Create Data
First, we’ll create mock data, then we’ll show you how to clone a variable both generally and conditionally. Let’s assume we’re measuring IQ for 1,000 subjects.
set obs 1000
gen subj = _n
label variable subj "Subject"
gen iq_a = runiform(90,140)
gen iq = round(iq_a)
drop iq_a
label variable iq "IQ"
list in 1/30
Cloning a Variable
Let’s clone the variable of iq using the following code:
clonevar iq2 = iq
list in 1/30
One reason to clone a variable can be to isolate certain values for analysis without either (a) deleting the original variable or (b) having to use if or other conditional commands. For instance, let’s say that you want to focus on individuals who have an IQ over 120. One approach is to add a conditional operator such as if iq > 120 to all of your subsequent code, but you can also create a separate variable that stores only individuals of this iq level. Try the following code:
clonevar iq3 = iq if iq > 120
list in 1/30
BridgeText can help you with all of your statistical analysis needs.