-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpip_wb.ado
440 lines (342 loc) · 11.9 KB
/
pip_wb.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*==================================================
project: Interaction with the PIP API at aggregate level
Author: R.Andres Castaneda
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 2023-05-16
Do-file version: 01
References:
Output: dta
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pip_wb, rclass
version 16.1
pip_timer pip_wb, on
pip_wb_check_args `0'
local optnames "`r(optnames)'"
mata: pip_retlist2locals("`optnames'")
if ("`pause'" == "pause") pause on
else pause off
qui {
//========================================================
// setup
//========================================================
//------------ setup
if ("${pip_version}" == "") {
noi disp "{err}No version selected."
error
}
tokenize "${pip_version}", parse("_")
local ppp_year `3'
//------------ Get auxiliary data
pip_auxframes
//========================================================
// Build query (queries returned in ${pip_last_queries})
//========================================================
pip_wb_query, region(`region') year(`year') povline(`povline') /*
*/ ppp_version(`ppp_year') coverage(`coverage')
//========================================================
// Getting data
//========================================================
//------------ download
pip_timer pip_wb.pip_get, on
pip_get, `clear' `cacheforce' `cachedir'
pip_timer pip_wb.pip_get, off
//------------ clean
pip_timer pip_wb_clean, on
pip_wb_clean, `nowcasts' `fillgaps'
pip_timer pip_wb_clean, off
//------------ Add data notes
local datalabel "WB poverty at regional and global level"
local datalabel = substr("`datalabel'", 1, 80)
label data "`datalabel' (`c(current_date)')"
//------------ Display results
noi pip_utils output, `n2disp' worldcheck ///
sortvars(region_code year) ///
dispvars(region_code year poverty_line headcount mean)
//------------ Povcalnet format
if ("`povcalnet_format'" != "") {
noi disp "{p 2 4 2 70}{err}Warning: {res}option {it:povcalnet_format}" /*
*/ " is meant for replicability purposes only or to be used in Stata code " /*
*/ "that still executes the deprecated {cmd:povcalnet} command.{p_end}" _n
pip_wb_povcalnet
}
}
pip_timer pip_wb, off
end
program define pip_wb_check_args, rclass
version 16.1
syntax ///
[ , ///
REGion(string) ///
Year(string) ///
POVLine(numlist) ///
COVerage(string) ///
CLEAR ///
pause ///
POVCALNET_format ///
replace ///
cacheforce ///
FILLgaps ///
NOWcasts ///
n2disp(passthru) ///
cachedir(passthru) * ///
]
//========================================================
// 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 "MRV"
else if (lower("`year'") == "mrv") local year "MRV"
else if (ustrregexm("`year'"), "[a-zA-Z]+") {
noi disp "{err} `year' is not a valid {it:year} value" _n /*
*/ "only {it:all}, {it:MRV} or numeric values are accepted{txt}" _n
error
}
else {
numlist "`year'"
local year = r(numlist)
}
return local 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'"
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
}
}
return local region = "`region'"
local optnames "`optnames' region"
//========================================================
// Aggregate level (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
}
//------------ nowcasts
if ("`nowcasts'" != "") {
// if nowcasts is selected, fillgaps is also selected
local fillgaps = "fillgaps"
}
return local nowcasts = "`nowcasts'"
local optnames "`optnames' nowcasts"
//------------ fillgaps
return local fillgaps = "`fillgaps'"
local optnames "`optnames' fillgaps"
// 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'"
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'"
local optnames "`optnames' povline"
if ("`clear'" == "") local clear "clear"
return local clear = "`clear'"
local optnames "`optnames' clear"
// allow n2disp as undocumented option
if ("`n2disp'" != "") {
return local n2disp = "`n2disp'"
local optnames "`optnames' n2disp"
}
return local optnames "`optnames'"
end
//========================================================
// Sub programs
//========================================================
//------------ Build CL query
program define pip_wb_query, rclass
version 16.1
syntax ///
[ , ///
REGion(string) ///
YEAR(string) ///
POVLine(numlist) ///
ppp_version(numlist) ///
COVerage(string) ///
]
//========================================================
// conditions and set up
//========================================================
qui {
// country
local country = stritrim(ustrtrim("`region'"))
local country : subinstr local country " " ",", all
if ("`country'" == "") local country = "all"
// year
local year: subinstr local year " " ",", all
if ("`year'" == "") local year = "all"
// reporting level
if ("`coverage'" == "") local reporting_level = "all"
else local reporting_level = "`coverage'"
// version
local version "${pip_version}"
//========================================================
// build query... THE ORDER IS VERY IMPORTANT
//========================================================
local params = "country year reporting_level " + /*
*/ " version welfare_type ppp_version"
foreach p of local params {
if (`"``p''"' == `""') continue
local query "`query'`p'=``p'' "
}
local query = ustrtrim("`query'")
local query : subinstr local query " " "&", all
//========================================================
// Povline
//========================================================
local endpoint "pip-grp"
if ("`povline'" == "") {
global pip_last_queries "`endpoint'?`query'&format=csv&group_by=wb"
exit
}
tempname M
local i = 1
foreach v of local povline {
// each povline or popshare + format
local queryp = "`endpoint'?`query'&povline=`v'&format=csv&group_by=wb"
if (`i' == 1) mata: `M' = "`queryp'"
else mata: `M' = `M' , "`queryp'"
local ++i
}
mata: st_global("pip_last_queries", invtokens(`M'))
}
end
//------------Clean Cl data
program define pip_wb_clean, rclass
syntax [, NOWcasts fillgaps ]
if ("${pip_version}" == "") {
noi disp "{err}No version selected."
error
}
local version = "${pip_version}"
tokenize "`version'", parse("_")
local _version = "_`1'_`3'_`9'"
local ppp_version = `3'
qui {
//========================================================
// labels
//========================================================
//------------ All variables
rename reporting_pop population
label var region_code "region code"
label var reporting_year "year"
label var poverty_line "poverty line in `ppp_version' PPP US\$ (per capita per day)"
label var mean "average daily per capita income/consumption in `ppp_version' PPP US\$"
label var headcount "poverty headcount"
label var poverty_gap "poverty gap"
label var poverty_severity "squared poverty gap"
label var population "population in year"
label var pop_in_poverty "population in poverty"
label var watts "watts index"
label var region_name "world bank region"
cap label var estimate_type "type of estimate"
order region_name region_code reporting_year poverty_line ///
mean headcount poverty_gap poverty_severity watts ///
population
//------------ Formatting
format headcount poverty_gap poverty_severity watts mean %8.4f
format pop_in_poverty population %15.0fc
format poverty_line %6.2f
local old "reporting_year"
local new "year"
rename (`old') (`new')
//------------drop unnecesary variables
keep region_name region_code year poverty_line mean headcount ///
poverty_gap poverty_severity watts population spr pg estimate_type ///
pop_in_poverty
//------------ Nowcasts and fillgaps
if ("`fillgaps'" == "") {
drop if estimate_type == "projection"
}
if ("`nowcasts'" == "") {
drop if estimate_type == "nowcast"
}
//------------ drop vars with missing value
pip_utils dropvars
}
end
program define pip_wb_povcalnet
ren year requestyear
ren population reqyearpopulation
//------------ Renaming and labeling
rename region_code regioncode
rename poverty_line povertyline
rename poverty_gap povgap
rename poverty_severity povgapsqr
keep requestyear regioncode povertyline mean headcount povgap ///
povgapsqr reqyearpopulation
order requestyear regioncode povertyline mean headcount povgap ///
povgapsqr reqyearpopulation
local Snames requestyear reqyearpopulation
local Rnames year population
local i = 0
foreach var of local Snames {
local ++i
rename `var' `: word `i' of `Rnames''
}
//------------ Convert to monthly values
replace mean = mean * (360/12)
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control: