forked from worldbank/pip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip_cl.ado
210 lines (167 loc) · 6.02 KB
/
pip_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
/*==================================================
project: Interaction with the PIP API at the country level
Author: R.Andres Castaneda
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 5 Jun 2019 - 15:12:13
Modification Date: October, 2021
Do-file version: 01
References:
Output: dta
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pip_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 = "https://pipscoreapiqa.worldbank.org/api/v1/pip?format=csv"
if "`server'"!="" {
local base = "`server'/api/v1/pip"
}
if ("`povline'" == "") local povline 1.9
if ("`ppp'" == "") local ppp -1
if ("`coverage'" == "") local coverage -1
*---------- download guidance data
pip_info, clear justdata `pause'
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 PIP. See {stata povcalnet info}"
}
if ("`countries'" == "") {
noi disp in red "None of the countries provided in {it:country()} is available in PIP"
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
local n = _n
foreach o in pl pp yr cv {
if (`n`o'' == 1) {
local `o': disp _dup(`nct') " ``o'' "
}
}
/*==================================================
2: Download data
==================================================*/
*----------2.1: download data
tempfile clfile
local queryfull "https://pipscoreapiqa.worldbank.org/api/v1/pip?format=csv"
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"
}
*---------- 2.2 create filter conditions in loop
local j = 0
local n = 1
local kquery = "" // whole filter condition to extract data
foreach ict of local countries {
* corresponding element to each country
foreach o in pl yr pp cv {
local i`o': word `n' of ``o''
}
*---------- coverage
if inlist("`icv'", "-1", "all") & ("`ipp'" == "-1") {
*local kquery "`kquery' | (country_code == "`ict'" & poverty_line == `ipl' & reporting_year == `iyr')"
local kquery "`kquery' | (country_code == "`ict'" & reporting_year == `iyr')"
return local kquery_`j' = "`kquery_`j''"
}
else if inlist("`icv'", "-1", "all") & ("`ipp'" != "-1") {
*local kquery "`kquery' | (country_code == "`ict'" & poverty_line == `ipl' & reporting_year == `iyr' & ppp == `ipp')"
local kquery "`kquery' | (country_code == "`ict'" & reporting_year == `iyr' & ppp == "`ipp'")"
return local kquery_`j' = "`kquery_`j''"
}
else if !inlist("`icv'", "-1", "all") & ("`ipp'" == "-1") {
*local kquery "`kquery' | (country_code == "`ict'" & poverty_line == `ipl' & reporting_year == `iyr' & survey_coverage == "`icv'")"
local kquery "`kquery' | (country_code == "`ict'" & reporting_year == `iyr' & survey_coverage == "`icv'")"
return local kquery_`j' = "`kquery_`j''"
}
else {
*local kquery "`kquery' | (country_code == "`ict'" & poverty_line == `ipl' & reporting_year == `iyr' & ppp == `ipp' & survey_coverage == "`icv'")"
local kquery "`kquery' | (country_code == "`ict'" & reporting_year == `iyr' & ppp == "`ipp'" & survey_coverage == "`icv'")"
return local kquery_`j' = "`kquery_`j''"
}
local ++j
local ++n
}
local kquery : subinstr local kquery "|" " "
keep if `kquery'
/*==================================================
3: Clean data
==================================================*/
pip_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 "Condition `x':" as txt _col(15) "`kquery_`x''"
}
noi di as res "{hline}"
}
*/
}
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control: