Introduction
Dropping unwanted variables can simplify data analysis. In this blog entry, we’ll show you how to use Stata’s drop command to eliminate unwanted variables from a given dataset.
Create Data
First, we’ll create mock data, then we’ll show you how to use the drop command.
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
egen total = rowtotal (q1 q2 q3 q4)
list in 1/30
Drop Unwanted Variables
Let’s say that you want to drop q1, q2, q3, and q4.
drop q*
list in 1/30
Technically, you could also have used the following code:
drop q1 q2 q3 q4
Here, we used * because, whenever Stata sees *, it eliminates all variables that have the prefix just before *. In this case, drop q* instructed Stata to drop all variables that begin with q. In this case, the four variables you want to drop (q1, q2, q3, and q4) all begin with q, and there are no other variables than begin with q that you want to retain. Therefore, you can use the * operator. Otherwise, you could have listed the variables you want to drop after the drop command.
BridgeText can help you with all of your statistical analysis needs.