📄 aorder.ado
字号:
*! version 2.2.3 20aug2003
program aorder
version 6, missing
syntax [varlist]
local n : word count `varlist'
if `n' <= 1 { exit }
preserve
quietly {
tempvar name
drop _all
set obs `n'
gen str32 `name' = ""
tokenize `varlist'
local i 1
while "``i''" != "" {
replace `name' = "``i''" in `i'
local i = `i'+1
}
SortAord `name'
local i 1
while "``i''" != "" {
local x = `name'[`i']
local newlist "`newlist' `x'"
local i = `i'+1
}
restore
order `newlist'
}
end
program SortAord
syntax varname [in]
if "`in'" ~= "" {
tokenize "`in'", parse(" /")
local first "`2'"
local last "`4'"
}
else {
local first 1
local last = _N
}
quietly {
tempvar name l stub digits nonumb group d e check
gen str32 `name' = `varlist' `in'
gen int `l' = index(`name',"0") `in'
replace `l' = . if `l'==0 `in'
local i 1
while `i' <= 33 {
replace `l' = index(`name',"`i'") if /*
*/ index(`name',"`i'")<`l' & /*
*/ index(`name',"`i'")!=0 `in'
local i = `i' + 1
}
replace `l' = 33 if `l'>=. `in'
gen str32 `stub' = substr(`name',1,`l'-1) `in'
local i 1
gen long `digits' = real(substr(`name',`l',`i')) `in'
gen `d' = .
gen `e' = .
gen str31 `check' = ""
while `i' < 31 {
local i = `i' + 1
/*Check for cases of #d# */
replace `check' = substr(`name',`l',`i') `in'
replace `d' = index(`check', "d") `in'
replace `name' = subinstr(`name', "d", "c", `i') if `d' > 0 `in'
/*Check for cases of #d# */
replace `e' = index(`check', "e") `in'
replace `name' = subinstr(`name', "e", "f", `i') if `e' > 0 `in'
replace `digits' = real(substr(`name',`l',`i')) `in' /*
*/ if `digits'<. /*
*/ & real(substr(`name',`l',`i'))<.
replace `name' = subinstr(`name', "c", "d", `i') if `d' > 0 `in'
replace `name' = subinstr(`name', "f", "e", `i') if `e' > 0 `in'
}
drop `d' `e' `check'
gen byte `nonumb' = -1 if `digits'>=. `in'
sort `stub' `nonumb' `digits' `in'
drop `nonumb'
replace `stub' = `stub' + string(`digits') `in'
drop `digits'
local i 1
local flag 0
while `i' <= _N & `flag'==0 {
if `name'[`i'] != `stub'[`i'] {
local flag 1
}
local i = `i' + 1
}
if `flag' == 1 {
encode `stub' `in', gen(`group')
if `group'[`last'] != (`last'-`first'+ 1) {
replace `l' = length(`stub') `in'
replace `name' = substr(`name',`l'+1,.) `in'
local i `first'
while `i' < `last' {
if `group'[`i']==`group'[`i'+1] {
local start `i'
while `group'[`i']== /*
*/ `group'[`i'+1] {
local i = `i' + 1
}
local finish `i'
SortAord `name' /*
*/ in `start'/`finish'
}
else {
local i = `i' + 1
}
}
}
}
}
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -