forked from worldbank/pip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip_info.ado
247 lines (202 loc) · 7.31 KB
/
pip_info.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
*********************************************************************************
* pip_info *
*********************************************************************************
program def pip_info, rclass
version 16.0
syntax [, ///
COUntry(string) ///
REGion ///
AGGregate ///
clear ///
justdata /// programmers option
pause /// debugging
server(string) ///
]
if ("`pause'" == "pause") pause on
else pause off
qui {
//if ("`clear'" == "") preserve
*---------- API defaults
if "`server'" == "" {
local site_name = "api/v1"
local server = "https://pipscoreapiqa.worldbank.org"
*local server = "http://wzlxqpip01.worldbank.org"
local url = "`server'/`site_name'"
return local site_name = "`site_name'"
return local url = "`url'"
}
else {
local url "https://pipscoreapiqa.worldbank.org/api/v1"
*local url "http://wzlxqpip01.worldbank.org/api/v1"
}
***************************************************
* 0. Country name
***************************************************
tempfile temp100
local csvfile0 = "`url'/aux?table=countries&format=csv"
return local csvfile0 = "`csvfile0'"
cap copy "`csvfile0'" `temp100'
if (_rc != 0 ) {
noi disp in red "There is a problem accessing country name data."
noi disp in red "to check your connection, copy and paste in your browser the following address:" _n /*
*/ _col(4) in w `"`url'/aux?table=countries&format=csv"'
error
}
import delim using `temp100', delim(",()") stringc(_all) /*
*/ stripq(yes) varnames(1) clear
keep country_code country_name income_group
sort country_code
save `temp100', replace
***************************************************
* 1. Load guidance database
***************************************************
tempfile temp1000
local csvfile = "`url'/pip?format=csv"
return local csvfile = "`csvfile'"
cap copy "`csvfile'" `temp1000'
if (_rc != 0 ) {
noi disp in red "There is a problem accessing the information file."
noi disp in red "to check your connection, copy and paste in your browser the following address:" _n /*
*/ _col(4) in w `"`url'/pip?format=csv"'
error
}
import delim using `temp1000', delim(",()") stringc(_all) /*
*/ stripq(yes) varnames(1) clear
local orgvar region_code survey_coverage reporting_year
local newvar wb_region coverage_level reporting_year
local i = 0
foreach var of local orgvar {
local ++i
rename `var' `: word `i' of `newvar''
}
keep country_code wb_region coverage_level survey_year reporting_year
order country_code wb_region coverage_level survey_year reporting_year
sort country_code
merge country_code using `temp100'
collapse _merge, by(country_code country_name wb_region coverage_level reporting_year income_group)
drop _merge
order country_code country_name wb_region coverage_level reporting_year income_group
gen year = ""
tostring reporting_year, replace
tempfile lkupdata
save `lkupdata', replace
levelsof country_code , local(ctry)
foreach ct of local ctry{
preserve
levelsof coverage_level, local(cv_area)
disp `cv_area'
foreach cv of local cv_area {
keep if country_code == "`ct'" & coverage_level == "`cv'"
levelsof reporting_year, local(year) sep(,) clean
disp `year'
replace year = "`year'" if coverage_level == "`cv'"
append using `lkupdata'
save `lkupdata', replace
}
restore
}
use `lkupdata', clear
drop if year == ""
sort country_code
duplicates drop country_code country_name wb_region coverage_level income_group year, force
drop reporting_year
if ("`justdata'" != "") exit
***************************************************
* 2. Inital listing with countries and regions
***************************************************
if ("`country'" == "") & ("`region'" == "") {
qui{
noi disp in y _n "{title:Available Surveys}: " in g "Select a country or region"
noi disp in y _n "{title: Countries}"
quietly levelsof country_code , local(countries)
local current_line = 0
foreach cccc of local countries{
local current_line = `current_line' + 1
local display_this = "{stata pip_info, country(`cccc') clear: `cccc'} "
if (`current_line' < 10) noi display in y `"`display_this'"' _continue
else{
noi display in y `"`display_this'"'
local current_line = 0
}
}
noi disp in y _n(2) "{title: Regions}"
quietly levelsof wb_region, local(regions)
foreach i_reg of local regions{
local current_line = 0
local dipsthis "{stata pip, region(`i_reg') year(all) aggregate clear:`i_reg' }"
noi disp " `dipsthis' " _c
}
noi display in y _n "{stata pip_info, region clear: World Bank regions by year}"
noi display _n ""
exit
}
}
***************************************************
* 3. Listing of country surveys
***************************************************
if ("`country'" != "") & ("`region'" == "") {
qui{
noi disp in y _n "{title:Available Surveys for `country'}"
preserve
local country = upper("`country'")
keep if country_code == "`country'"
local link_detail = "`url'/Docs/CountryDocs/`country'.htm"
noi display `"{browse "`link_detail'" : Detailed information (browser)}"'
local nobs = _N
local current_line = 0
local index_s = 1
foreach n of numlist 1/`nobs' {
noi disp in y _n "`=country_name[`index_s']'-`=coverage_level[`index_s']'"
noi disp in y "survey year"
local years_current = "`=year[`index_s']'"
local coverage = "`=coverage_level[`index_s']'"
local years_current: subinstr local years_current "," " ", all
local index_s = `index_s'+ 1
foreach ind_y of local years_current {
local current_line = `current_line' + 1
local ind_y_c=substr("`ind_y'",1,4)
local display_this = "{stata pip, country(`country') year(`ind_y') coverage(`coverage') clear: `ind_y_c'}"
if (`current_line' < 10) noi display in y `"`display_this'"' _continue
else{
noi display in y `"`display_this'"'
local current_line = 0
}
}
noi display `"{stata pip, country(`country') year(all) coverage(`coverage') clear: All}"'
}
restore
noi display _n ""
exit
break
}
}
***************************************************
* 4. Listing of regions
***************************************************
if ("`country'" == "") & ("`region'" != "") {
qui{
noi disp in y _n "{title:Available Surveys}"
noi disp in y _n "{title:Select a Year}"
quietly levelsof wb_region, local(regions)
foreach i_reg of local regions{
local current_line = 0
noi disp in y _n "`i_reg'"
local years_current = "$refyears"
foreach ind_y of local years_current {
local current_line = `current_line' + 1
local display_this = "{stata pip, region(`i_reg') year(`ind_y') aggregate clear: `ind_y'}"
if (`current_line' < 10) noi display in y `"`display_this'"' _continue
else{
noi display in y `"`display_this'"'
local current_line = 0
}
}
noi display in y "{stata pip, region(`i_reg') year(all) aggregate clear: All}"
}
noi display _n ""
exit
break
}
}
}
end