*---------------codes-----------------------------
sysuse auto, clear
preserve
gen id = _n
tempfile data1
drop price /*假设对price排序*/
sort id
qui save "`data1'", replace
restore, preserve
keep price
sort price
gen id = _n
tempfile data2
sort id
qui save "`data2'", replace
qui use "`data1'", clear
merge id using "`data2'"
drop _merge id
order price mpg wei
list price mpg wei in 1/10 /*排序后的数据结构*/
save newdata, replace
restore
order price mpg wei
list price mpg wei in 1/10 /*排序前的数据结构*/
*----------------over codes---------------------------
*===============Results===============
. sysuse auto, clear
(1978 Automobile Data)
.
. preserve
. gen id = _n
. tempfile data1
. drop price /*假设对price排序*/
. sort id
. qui save "`data1'", replace
. restore, preserve
.
. keep price
. sort price
. gen id = _n
. tempfile data2
. sort id
. qui save "`data2'", replace
.
. qui use "`data1'", clear
. merge id using "`data2'"
. drop _merge id
. order price mpg wei
. list price mpg wei in 1/10 /*排序后的数据结构*/
+----------------------+
| price mpg weight |
|----------------------|
1. | 3,291 22 2,930 |
2. | 3,299 17 3,350 |
3. | 3,667 22 2,640 |
4. | 3,748 20 3,250 |
5. | 3,798 15 4,080 |
|----------------------|
6. | 3,799 18 3,670 |
7. | 3,829 26 2,230 |
8. | 3,895 20 3,280 |
9. | 3,955 16 3,880 |
10. | 3,984 19 3,400 |
+----------------------+
. save newdata, replace
file newdata.dta saved
. restore
.
. order price mpg wei
. list price mpg wei in 1/10 /*排序前的数据结构*/
+-----------------------+
| price mpg weight |
|-----------------------|
1. | 4,099 22 2,930 |
2. | 4,749 17 3,350 |
3. | 3,799 22 2,640 |
4. | 4,816 20 3,250 |
5. | 7,827 15 4,080 |
|-----------------------|
6. | 5,788 18 3,670 |
7. | 4,453 26 2,230 |
8. | 5,189 20 3,280 |
9. | 10,372 16 3,880 |
10. | 4,082 19 3,400 |
+-----------------------+
[此贴子已经被作者于2008-8-8 10:16:47编辑过]