-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccodes.do
206 lines (174 loc) · 4.46 KB
/
ccodes.do
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
qui {
capture program drop ccodes
program ccodes
//version 15 // 14 and above only
syntax [varname(default=none)] [if] [in] , from(string) to(string) [GENerate(string) SVALue(string) year(string)]
qui {
local csets = "p4_ccode p4_scode p4_country cow_ccode cow_scode cow_country iso_scode iso_country"
if "`varlist'"!= "" & "`svalue'"!= "" {
di as err "{bf:varname} cannot be combined with {bf:svalue()}"
exit
}
if "`from'"!= "" { // from and to THEY CANNOT BE MISSING
if "`to'"=="`from'" {
di as error "{bf:from()}'s and {bf:to()}'s arguments cannot be the same."
exit
}
local exit = 0
foreach c of local csets {
if "`c'"=="`from'" {
local exit = 1
continue, break
}
}
if `exit'==0 {
di as error "{bf:from()}'s argument {it:`from'} unrecognized."
di as error "Currently accepted code sets are: {it:`csets'}."
exit
}
}
if "`to'"!= "" {
local exit = 0
foreach c of local csets {
if "`c'"=="`to'" {
local exit `exit'+1
continue, break
}
}
if `exit'==0 {
di as error "{bf:to()}'s argument {it:`to'} unrecognized."
di as error "Currently accepted code sets are: {it:`csets'}."
exit
}
}
if "`generate'"!= "" & "`svalue'"!= "" {
di as err "{bf:generate()} cannot be combined with {bf:svalue()}"
exit
}
if "`year'"!= "" {
confirm variable `year'
tempvar y
capture confirm string variable `year'
if rc==0 {
destring `year', generate(`y') force
}
else {
gen `y' = `year'
}
if length(string(`y'[1]))!=4 di as err "four-digit year date format if required for the {bf:year()} variable"
}
tempvar Nseq
egen `Nseq' = seq()
preserve
tempfile codes
import excel csets, first clear
tempvar `from' `to'
rename `from' ``from''
rename `to' ``to''
save "`codes'"
restore
merge 1:1 _n using "`codes'", keepusing(``from'' ``to'') nogen
if "`svalue'"!="" {
local n = 0
capture confirm number `svalue'
if _rc==0 {
capture confirm string variable ``from''
if _rc==0 {
di as err "{bf:from()}'s argument must be numeric if {bf:svalue()}'s argument is numeric."
exit
}
tempvar seq
egen `seq' = seq() if ``from''!=.
sort `seq'
sum `seq', meanonly
forval i = 1/`r(max)' {
if `svalue'==``from''[`i'] {
noi di as text _newline ``to''[`i']
local n = 1
}
}
}
else {
capture confirm string variable ``from''
if _rc!=0 {
di as err "{bf:from()}'s argument must be string if {bf:svalue()}'s argument is string."
exit
}
local single = trim(ustrto(ustrnormalize(ustrlower("`svalue'","en"), "nfd"), "ascii", 2))
tempvar seq
egen `seq' = seq() if ``from''!=""
sort `seq'
sum `seq', meanonly
forval i = 1/`r(max)' {
if "`single'"==ustrto(ustrnormalize(ustrlower(``from''[`i'],"en"), "nfd"), "ascii", 2) {
noi di as text _newline ``to''[`i']
local n = 1
}
}
}
if `n'==0 noi di as text _newline "no {it:`to'} match found for {it:`from': `svalue'}"
keep if `Nseq'!=.
sort `Nseq'
}
else {
if "`varlist'"=="" {
di as err "either {bf:varname} or {bf:svalue} must be specified"
exit
}
tempvar tvar seq
capture confirm string variable `varlist'
if _rc==0 {
gen `tvar' = trim(ustrto(ustrnormalize(ustrlower(`varlist',"en"), "nfd"), "ascii", 2))
if "`if'"!="" {
egen `seq' = seq() `if' & `tvar'!="" `in'
}
else {
egen `seq' = seq() if `tvar'!="" `in'
}
}
else {
gen `tvar' = `varlist'
if "`if'"!="" {
egen `seq' = seq() `if' & `tvar'!=. `in'
}
else {
egen `seq' = seq() if `tvar'!=. `in'
}
}
// !!! request year for a specific "from" or "to"
tempvar res
confirm string variable ``to''
if _rc==0 {
gen `res' = ""
}
else {
gen `res' = .
}
local m = 0
sort `seq'
sum `seq', meanonly
forval i = 1/`r(max)' {
capture replace `res' = ``to'' if `tvar'[`i']==``from'' in `i'
capture if `tvar'[`i']!=. & `res'[`i']==. local m = 1
capture replace `res' = ``to'' if `tvar'[`i']==ustrto(ustrnormalize(ustrlower(``from'',"en"), "nfd"), "ascii", 2) in `i'
capture if `tvar'[`i']!="" & `res'[`i']=="" local m = 1
}
keep if `Nseq'!=.
sort `Nseq'
if `m' > 0 {
noi di as text _newline "List of {it:`from'} entries from the {bf:`varlist'} variable without a {it:`to'} match"
capture tab `varlist' if `tvar'!="" & `res'==""
capture tab `varlist' if `tvar'!=. & `res'==.
}
if "`generate'"!="" {
rename `res' `generate'
}
else {
rename `res' `to'
}
}
}
end
noi `=scalar(cmd)'
}
exit