-
Notifications
You must be signed in to change notification settings - Fork 2
/
povcalnet_cl.ado
221 lines (166 loc) · 5.56 KB
/
povcalnet_cl.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
/*==================================================
project: Interaction with the PovcalNet API at the country level
Author: R.Andres Castaneda
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 5 Jun 2019 - 15:12:13
Modification Date:
Do-file version: 01
References:
Output: dta
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define povcalnet_cl, rclass
syntax , [ ///
country(string) ///
year(string) ///
povline(numlist) ///
ppp(numlist) ///
server(string) ///
coverage(string) ///
clear ///
pause ///
iso ///
noDIPSQuery ///
]
version 11.0
if ("`pause'" == "pause") pause on
else pause off
qui {
/*==================================================
conditions and setup
==================================================*/
local base="http://iresearch.worldbank.org/PovcalNet/PovcalNetAPI.ashx"
if "`server'"!="" {
local base="`server'/PovcalNetAPI.ashx"
}
if ("`povline'" == "") local povline 1.9
if ("`ppp'" == "") local ppp -1
if ("`coverage'" == "") local coverage -1
*---------- download guidance data
povcalnet_info, clear justdata `pause'
gen n = _n
levelsof country_code, local(countries) clean
if (lower("`country'") != "all") {
local uniq_country : list uniq country
local ncountries: list uniq_country - countries
local avai_country: list uniq_country & countries
local countries: list country | avai_country
}
if ("`ncountries'" != "") {
if wordcount("`ncountries'") == 1 local be "is"
if wordcount("`ncountries'") > 1 local be "are"
noi disp as err "Warning: " _c
noi disp as input `"`ncountries' `be' not part of the country list"' /*
*/ _n "available in PovcalNet. See {stata povcalnet info}"
}
if ("`countries'" == "") {
noi disp in red "None of the countries provided in {it:country()} is available in Povcalnet"
error
}
*---------- alternative macros
local ct = "`countries'"
local pl = "`povline'"
local pp = "`ppp'"
local yr = "`year'"
local cv = "`coverage'"
/*==================================================
1: Evaluate parameters
==================================================*/
*----------1.1: counting words
local nct = wordcount("`ct'") // number of countries
local npl = wordcount("`pl'") // number of poverty lines
local npp = wordcount("`pp'") // number of PPP values
local nyr = wordcount("`yr'") // number of years
local ncv = wordcount("`cv'") // number of coverage
matrix A = `nct' \ `npl' \ `npp' \ `nyr' \ `ncv'
mata: A = st_matrix("A"); /*
*/ B = ((A :== A[1]) + (A :== 1) :>= 1); /*
*/ st_local("m", strofreal(mean(B)))
if (`m' != 1) {
noi disp in r "number of elements in options {it:povline(), ppp(), year()} and " _n /*
*/ "{it:coverage()} must be equal to 1 or to the number of countries in option {it:country()}"
error 197
}
*----------1.2: Expand macros of size one
foreach o in pl pp yr cv {
if (`n`o'' == 1) {
local `o': disp _dup(`nct') " ``o'' "
}
}
/*==================================================
2: Create query
==================================================*/
*----------create query in loop
local j = 0
local n = 1
local query = "" // whole query
foreach ict of local countries {
* corresponding element to each country
foreach o in pl yr pp cv {
local i`o': word `n' of ``o''
}
*---------- query coverage
if inlist("`icv'", "-1", "all") local qcv ""
else {
sum n if country_code == "`ict'" & coverage_level == "`icv'", meanonly
local nobs = r(min)
local qcv = "_"+"`=coverage_code[`nobs']'"
}
local qct = "C`j'=`ict'`qcv'" // query country
local qpl = "PL`j'=`ipl'" // query poverty line
local qyr = "&Y`j'=`iyr'" // query year
if ("`ipp'" == "-1") { // query ppp
local qpp ""
}
else {
local qpp "&PPP`j'=`ipp'"
}
local query "`query'&`qct'&`qpl'`qyr'`qpp'"
local query_`j' = "`qct'&`qpl'`qyr'`qpp'"
return local query_`j' = "`query_`j''"
local ++j
local ++n
}
*----------2.2: format query
local query = substr("`query'", 2, .) + "&format=csv"
return local query = "`query'"
global pcn_query = "`query'"
/*==================================================
3: Download data
==================================================*/
*----------3.1:
tempfile clfile
local queryfull "`base'?`query'"
return local queryfull = "`queryfull'"
local rc = 0
cap copy "`queryfull'" `clfile'
if (_rc == 0) {
cap insheet using `clfile', clear name
if (_rc != 0) local rc "in"
}
else {
local rc "copy"
}
*----------3.2: clean data
povcalnet_clean 1, year("`year'") `iso' rc(`rc')
if ("`dipsquery'" == "") {
noi di as res _n "{title: One-on-one query}"
noi di as res "{hline}"
foreach x of numlist 0/`=`j'-1' {
noi di as res "Query `x':" as txt _col(15) "`query_`x''"
}
noi di as res "{hline}"
}
}
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control: