-
Notifications
You must be signed in to change notification settings - Fork 6
/
pip_pov_check_args.ado
283 lines (216 loc) · 7.91 KB
/
pip_pov_check_args.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*==================================================
project: Evaluate the logical consistency of arguments for poverty estimation
Author: R.Andres Castaneda
----------------------------------------------------
Creation Date: 11 May 2023 - 17:45:06
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pip_pov_check_args, rclass
syntax [anything(name=subcmd)] ///
[ , ///
COUntry(string) ///
REGion(string) ///
Year(string) ///
POVLine(numlist) ///
POPShare(numlist) ///
CLEAR ///
COVerage(string) ///
FILLgaps ///
]
version 16.1
//========================================================
// setup
//========================================================
local version = "${pip_version}"
tokenize "`version'", parse("_")
local _version = "_`1'_`3'_`9'"
local ppp_year = `3'
//------------ Get auxiliary data
pip_timer pov_check_args.auxframes, on
pip_auxframes
pip_timer pov_check_args.auxframes, off
//========================================================
// General checks
//========================================================
//------------ year
if ("`year'" == "") local year "all"
else if (lower("`year'") == "all") local year "all"
else if (lower("`year'") == "last") local year "last"
else if (ustrregexm("`year'"), "[a-zA-Z]+") {
noi disp "{err} `year' is not a valid {it:year} value" _n /*
*/ "only numeric values are accepted{txt}" _n
error
}
else {
numlist "`year'"
local year = r(numlist)
}
return local year = "year(`year')"
local optnames "`optnames' year"
*---------- Coverage
if (lower("`coverage'") == "all") local coverage = ""
local coverage = lower("`coverage'")
foreach c of local coverage {
if !inlist(lower("`c'"), "national", "rural", "urban", "") {
noi disp in red `"option {it:coverage()} must be "national", "rural", "urban" or "all" "'
error
}
}
return local coverage = "coverage(`coverage')"
local optnames "`optnames' coverage"
//------------ Region
if ("`region'" != "") {
local region = upper("`region'")
if (regexm("`region'", "SAR")) {
noi disp in red "Note: " in y "The official code of South Asia is" ///
"{it: SAS}, not {it:SAR}. We'll make the change for you"
local region: subinstr local region "SAR" "SAS", word
}
//------------ Regions frame
local frpiprgn "_pip_regions`_version'"
frame `frpiprgn' {
levelsof region_code, local(av_regions) clean
}
// Add all to have the same functionality as in country(all)
local av_regions = "`av_regions'" + " ALL"
local inregion: list region in av_regions
if (`inregion' == 0) {
noi disp in red "region `region' is not available." _n ///
"Only the following are available:" _n "`av_regions'"
error
}
}
//------------empty data
if !ustrregexm("`subcmd'", "^info") {
if (c(changed) != 0 & "`clear'" == "") {
noi di as err "You must start with an empty dataset; or enable the option {it:clear}."
error 4
}
drop _all
}
//========================================================
// Country Level (cl)
//========================================================
if ("`subcmd'" == "cl") {
//------------ Poverty line
// defined popshare and defined povline = error
if ("`popshare'" != "" & "`povline'" != "") {
noi disp as err "povline and popshare cannot be used at the same time"
error
}
// Blank popshare and blank povline = default povline 1.9
else if ("`popshare'" == "" & "`povline'" == "") {
if ("`ppp_year'" == "2005") local povline = 1.25
if ("`ppp_year'" == "2011") local povline = 1.9
if ("`ppp_year'" == "2017") local povline = 2.15
}
return local povline = "povline(`povline')"
return local popshare = "popshare(`popshare')"
local optnames "`optnames' povline popshare"
//------------ fillgaps
return local fillgaps = "`fillgaps'"
local optnames "`optnames' fillgaps"
*---------- Country
// check availability
if ("`country'" != "") {
local country = upper("`country'")
frame _pip_fw`_version' {
qui levelsof country_code, local(av_cts) clean
}
// Add all to have the same functionality as in country(all)
local av_cts = "`av_cts'" + " ALL"
local inct: list country in av_cts
if (`inct' == 0) {
noi disp in red "Country `country' is not available." _n ///
"Only the following are available:"
noi pip_info
error
}
}
// Check if year is available
if ("`country'" != "" & "`fillgaps'" == "" & !inlist(lower("`year'"), "all", "")) {
frame _pip_fw`_version' {
tempname CT o YR
mata : ; /*
*/ st_sview(`CT' = ., ., "country_code") ; /*
*/ `o' = selectindex(`CT' :== "`country'") ; /*
*/ st_view(`YR' = ., `o', "year") ; /*
*/ st_local("av_year", strofreal(anyof(`YR', `year')))
if (`av_year'== 0) {
noi disp in red "Survey year {ul:`year'} is not available in `country'." _n ///
"Only the following are available:"
noi pip_info, country(`country')
error
}
}
}
local country = stritrim(ustrtrim("`country' `region'"))
if (lower("`country'") != "all") local country = upper("`country'")
if ("`country'" == "") local country "all" // to modify
return local country = "country(`country')"
local optnames "`optnames' country"
} // end of cl subcmd
//========================================================
// Aggregate level (wb)
//========================================================
if ("`subcmd'" == "wb") {
if ("`country'" != "") {
noi disp as err "option {it:country()} is not allowed with subcommand {it:wb}"
noi disp as res "Note: " as txt "subcommand {it:wb} only accepts options {it:region()} and {it:year()}"
error
}
if ("`fillgaps'" != "") {
noi disp "{res}Note:{txt} option {it:fillgaps} not allowed with " /*
*/ "subcommand {cmd:wb}."
error
}
// poshare
if ("`popshare'" != "") {
noi disp in red "option {it:popshare()} can't be combined " /*
*/ "with subcommand {it:wb}" _n
error
}
if ("`region'" != "") {
return local region = "region(`region')"
local optnames "`optnames' region"
}
// poverty line
if ("`povline'" == "") {
if ("`ppp_year'" == "2005") local povline = 1.25
if ("`ppp_year'" == "2011") local povline = 1.9
if ("`ppp_year'" == "2017") local povline = 2.15
}
return local povline = "povline(`povline')"
local optnames "`optnames' povline"
}
//========================================================
// Country profiles (cp)
//========================================================
if ("`subcmd'" == "cp") {
*---------- Country
local country = stritrim(ustrtrim("`country' `region'"))
if (lower("`country'") != "all") local country = upper("`country'")
if ("`country'" == "") local country "all" // to modify
return local country = "country(`country')"
local optnames "`optnames' country"
// poverty line
if ("`povline'" == "") {
if ("`ppp_year'" == "2005") local povline = 1.25
if ("`ppp_year'" == "2011") local povline = 1.9
if ("`ppp_year'" == "2017") local povline = 2.15
}
return local povline = "povline(`povline')"
local optnames "`optnames' povline"
return local coverage = ""
return local year = ""
}
//========================================================
// Return options names
//========================================================
return local optnames "`optnames'"
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><