-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgettoken.ado
175 lines (157 loc) · 4.4 KB
/
vgettoken.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
*! version 1.0.4 December 9, 2003 @ 22:28:23
*! parses one word off the front of a string variable, just as gettoken does with macros
program define vgettoken
version 8.2
/* 1.0.3 - updated to version 8, changed syntax to be more like gettoken */
/* 1.0.2 - updated to version 7, changed name from _nextwrd */
/* 1.0.1 - allows gen option for generating a new variable */
local me vgettoken
syntax anything(id="variable names") [if] [in] [, Parse(str) noSPACE REPLACERESUlt REPLACERESt noDELIMiters]
if "`parse'"=="" & "`space'"!="" {
display as error "[`me']: You need to parse on something! Either give a character in the parse option,"
display as error " or specify the space option!"
exit 198
}
/* fixing stata's troubles with substr(" ",1,1) being "" */
/* need to strip spaces from the parse string */
local pspace = index("`parse'"," ")
if `pspace' {
if "`space'"!="" {
display as error "[`me']: You specified no spaces, but put a space in the parse string! I'm lost."
exit 198
}
local parse = subinstr("`parse'"," ","",.)
}
if "`delimiters'"=="" {
local dlength 0
}
else {
local dlength 1
}
tokenize `"`anything'"', parse(" :")
if `"`3'"' == "" GenlSyntaxError `me'
local result `"`1'"'
capture confirm new var `result'
local rc = _rc
if `rc' {
if `rc' == 110 {
if "`replaceresult'"=="" {
display as error "[`me']: the result variable " as result "`result'" as error "already exists, and " as text "replaceresult" as error " was not specified!"
exit 110
}
tempname oldresult
}
else {
display as error `"[`me']: had trouble with result variable `result'"'
error `rc'
}
} /* end error check for result */
/* if 2nd token is colon, then just taking first word, and chucking rest */
if "`2'"!=":" {
if "`3'"!=":" {
GenlSyntaxError `me'
}
local rest `"`2'"'
capture confirm new var `rest'
local rc = _rc
if `rc' {
if `rc' == 110 {
if "`replacerest'"=="" {
display as error "[`me']: the rest variable " as result "`rest'" as error "already exists, and " as text "replacerest" as error " was not specified!"
exit 110
}
tempname oldrest
}
else {
display as error `"[`me']: had trouble with result variable `rest'"'
error `rc'
}
} /* end error check for rest */
local source `"`4'"'
}
else {
local source `"`3'"'
}
capture confirm variable `source'
if _rc {
display as error `"[`me']: Had trouble with the source variable `source'"'
exit 198
}
/* error checking finished */
marksample useme, strok
quietly sum `useme'
if r(max)==0 {
display as result "[`me']: no observations chosen... nothing generated"
exit
}
quietly {
if "`space'"=="" {
tempvar atspace
gen int `atspace' = index(`source'," ") if `useme'
replace `atspace' = . if `atspace'==0
}
else {
local atspace .
}
local plength = length("`parse'")
if `plength' {
tempvar atdelim whereat
forvalues cnt = 1/`plength' {
local theDelim = substr("`parse'",`cnt',1)
gen int `whereat' = index(`source',"`theDelim'") if `useme'
if `cnt' == 1 {
gen int `atdelim' = `whereat'
}
else {
replace `atdelim' = `whereat' if `whereat' & (`whereat'<`atdelim') & `useme'
}
drop `whereat'
local cnt = `cnt' + 1
}
replace `atdelim' = . if `atdelim'==0
}
else {
local atdelim .
}
*set trace on
tempvar take
gen int `take' = min(`atspace',`atdelim')-1 if `useme'
*set trace off
*pause fooey
if `plength' & ("`delimiters'"=="") {
replace `take' = 1 if (`take'==0) & (`atdelim'<.) & `useme'
}
*pause fooey2
GenRep `result', what(str)
`r(genrep)' `result' = substr(`source',1,`take') if `useme'
if "`rest'"!="" {
GenRep `rest', what(str)
`r(genrep)' `rest' = trim(substr(`source',`take'+`dlength'+1,.)) if `useme'
}
}
end
program define GenlSyntaxError
args me
display as error "[`me']: the primary syntax is either "
display as text " nextword " as result "result : source" as error " or"
display as text " nextword " as result "result rest : source"
exit 198
end
program define GenRep , rclass
local me "GenRep"
syntax anything(id="variable") [, what(str)]
capture confirm new var `anything'
local rc = _rc
if `rc' {
if `rc'== 110 {
return local genrep "replace"
}
else {
display as error "[`me']: had trouble with variable name `anything'"
error `rc'
}
}
else {
return local genrep "gen `what'"
}
end