Dear all
I have two grouping variables (var1 var2). I want to sort them (sort var1 var2) and then create a new variable for each group:
******************
clear *
set obs 100
gen var1=round(runiform(1,10),1)
gen var2=round(runiform(1,10),1)
sort var1 var2
by var1 var2: gen var3=_n
********************
the above works fine for sure. But if I try to achieve the same results with the following, there is a "not sorted" error, when the data should have been sorted:
***********************
clear *
set obs 100
gen var1=round(runiform(1,10),1)
gen var2=round(runiform(1,10),1)
sort var1 var2
gen groupsort=_n
sort groupsort
by var1 var2: gen var3=_n // caused "not sorted" error
***************************
The reason for exploring the second approach is to (i hope) help speed up sorting by having to sort only a single variable.
Grateful for advice.
Charlie
I have two grouping variables (var1 var2). I want to sort them (sort var1 var2) and then create a new variable for each group:
******************
clear *
set obs 100
gen var1=round(runiform(1,10),1)
gen var2=round(runiform(1,10),1)
sort var1 var2
by var1 var2: gen var3=_n
********************
the above works fine for sure. But if I try to achieve the same results with the following, there is a "not sorted" error, when the data should have been sorted:
***********************
clear *
set obs 100
gen var1=round(runiform(1,10),1)
gen var2=round(runiform(1,10),1)
sort var1 var2
gen groupsort=_n
sort groupsort
by var1 var2: gen var3=_n // caused "not sorted" error
***************************
The reason for exploring the second approach is to (i hope) help speed up sorting by having to sort only a single variable.
Grateful for advice.
Charlie
Comment