-
Notifications
You must be signed in to change notification settings - Fork 4
/
_gcode.ado
110 lines (101 loc) · 2.51 KB
/
_gcode.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
*! version 2.0.8 01oct2004
program define _gcode
version 7, missing
syntax newvarname(gen) =/exp [if] [in] /*
*/ [, AT(numlist asc) Group(real 0) BY(string) suffix(string) prefix(string)]
local label label
if `"`by'"' != "" {
_egennoby cut() `"`by'"'
}
cap qui sum `exp', meanonly
if _rc~=0 {
di as err "cut`exp'invalid, variable name expected"
exit 198
}
if "`at'"=="" & `group'==0 {
di as err "one of at() or group() must be specified"
exit 198
}
if "`at'"!="" & `group'!=0 {
di as err "cannot specify both at() and group() option"
exit 198
}
tempvar touse pctile x
qui gen `typelist' `x' = `exp'
if "`at'" != "" {
local n : word count `at'
local top : word `n' of `at'
qui replace `x' = . if `x' >= `top'
}
marksample touse, novarlist
markout `touse' `x'
if "`at'"=="" & `group'!=0 & "`icodes'"=="" {
local icodes "icodes"
}
if "`label'"!="" & "`icodes'"=="" {
local icodes "icodes"
}
local vlbl "$EGEN_Varname"
local vlablen : label `vlbl' maxlength
if `vlablen' != 0 { /* a variable label named `vlbl' already exists */
/* we will find another name to use */
local chop = length("`vlbl'") - 1
local ends = "abcdefghijklmnopqrstuvwxyz0123456789" + /*
*/ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
forvalues j = 1/62 {
local vlbl = substr("`vlbl'",1,`chop') + /*
*/ substr("`ends'",`j',1)
local vlablen : label `vlbl' maxlength
if `vlablen' == 0 { /* we found an unused one */
continue, break
}
}
}
if "`at'"=="" {
qui summ `x', meanonly
tempname extra
scalar `extra' = r(min)
local at "`extra'"
qui pctile `pctile' = `x' if `touse', nq(`group')
local grm1 = `group' - 1
forvalues count = 1/`grm1' {
tempname extra
scalar `extra' = `pctile'[`count']
local at "`at' `extra'"
}
qui summ `x' if `touse', meanonly
tempname extra
scalar `extra' = r(max)+1
local at "`at' `extra'"
}
local n : word count `at'
local i=0
foreach cutp of local at {
if "`icodes'"=="" {
qui replace `varlist' = `cutp' /*
*/ if `x' >= `cutp' & `touse'
}
else {
qui replace `varlist' = `i' /*
*/ if `x' >= `cutp' & `touse'
}
if "`label'"!="" & "`icodes'" != "" {
local code = `i' - 1
local slcut = string(`cutp')
if `i' == 1{
label define `vlbl' `code' "up to `prefix'`slcut'`suffix'", add
}
else if `i' == `n' - 1{
label define `vlbl' `code' "`prefix'`past'`suffix'+", add
}
else{
label define `vlbl' `code' "`prefix'`past'`suffix'-`prefix'`slcut'`suffix'", add
}
local past `slcut'
}
local i=`i'+1
}
if "`label'"!="" {
label values `varlist' `vlbl'
}
end