Introduction
Making sure that your data are appropriately sorted can be a precondition of data analysis. In this blog entry, we’ll show you how to sort your data in Stata.
Create Data
First, we’ll create mock data, then we’ll show you some approaches to sorting.
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 subj total in 1/30
Have a look at these data, and you’ll see that each total score is associated with exactly one subject. That’s important because, after you sort your data, you don’t want to lose track of which values go with which subject.
Sort
Let’s say that you want to sort the total score in an ascending manner. Try:
sort total
list subj total in 1/30
Your data are now sorted by ascending total score. However, each total is still associated with the correct subject. If you ever wanted to go back to the original sorting, you could simply type:
sort subj
BridgeText can help you with all of your statistical analysis needs.