-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcn.ado
491 lines (394 loc) · 13.3 KB
/
pcn.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*==================================================
* project: Stata package to manage PovcalNet files and folders
* Author: R.Andres Castaneda
* E-email: [email protected]
* Author: David Leonardo Vargas Mogollon
* E-email: [email protected]
* url: https://github.com/randrescastaneda/pcn
* Dependencies: The World Bank
----------------------------------------------------
Creation Date: 29 Jul 2019 - 09:18:01
Modification Date: 25 feb 2020
Do-file version: 01
References:
Output:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pcn, rclass
syntax anything(name=subcmd id="subcommand"), ///
[ ///
COUNtries(string) ///
Years(numlist) ///
REGions(string) ///
maindir(string) ///
type(string) ///
pause ///
* ///
qui ///
]
version 16
*---------- conditions
if ("`pause'" == "pause") pause on
else pause off
qui {
/*==================================================
Dependencies
==================================================*/
if ("${pcn_ssccmd}" == "") {
*--------------- SSC commands
local cmds missings
noi disp in y "Note: " in w "{cmd:pcn} requires the packages below: " /*
*/ _n in g "`cmds'"
foreach cmd of local cmds {
capture which `cmd'
if (_rc != 0) {
ssc install `cmd'
noi disp in g "{cmd:`cmd'} " in w _col(15) "installed"
}
}
adoupdate `cmds', ssconly
if ("`r(pkglist)'" != "") adoupdate `r(pkglist)', update ssconly
global pcn_ssccmd = 1 // make sure it does not execute again per session
}
// ---------------------------------------------------------------------------------
// initial parameters
// ---------------------------------------------------------------------------------
* Directory path
if ("`drive'" == "") {
if ("`c(hostname)'" == "wbgmsbdat002") local drive "\\wbgmsbdat002"
else local drive "//wbntpcifs/povcalnet"
}
if ("`root'" == "") local root "01.PovcalNet/01.Vintage_control"
if ("`maindir'" == "") local maindir "`drive'//`root'"
if ("`qui'" == "") {
local nq "noi"
}
else {
local nq "qui"
}
//------------ Download functions
if regexm("`subcmd'", "^download") {
local dldb "gpwg pending wrk" // download databases
if wordcount("`subcmd'") != 2 {
noi disp as text "Options available to download"
local i = 0
noi disp _n "select survey to load"
foreach db of local dldb {
local ++i
noi disp `" `i' {c |} {stata `db'}"'
}
noi disp _n "Select Database to download" _request(_db)
}
else {
local db: word 2 of `subcmd'
local dldb_: subinstr local dldb " " "|", all
if !(regexm(lower("`db'"), "`dldb_'")) {
noi disp in red " Options available to download are "
foreach db of local dldb {
noi disp `" `i' {c |}`db'"'
}
error
}
}
}
//========================================================
// inventory
//========================================================
if regexm("`subcmd'", "^inv(e|en|ent|ento|entor|entor)") {
pcn_inventory, country(`countries') year(`years') maindir("`maindir'") /*
*/ `pause' `options'
exit
}
// ------------------------------------------------------------------------------
// Download GPWG
// -------------------------------------------------------------------------------
if regexm("`subcmd'", "download[ ]+gpwg") {
`nq' pcn_download_gpwg, countries(`countries') years(`years') /*
*/ maindir("`maindir'") `pause' `options'
return add
exit
}
//========================================================
// Data in primus
//========================================================
if regexm("`subcmd'", "primus[ ]+approved") {
if regexm("`options'", "down\(.*\)") {
`nq' pcn_primus_download, countries(`countries') years(`years') /*
*/ status(approved) `pause' `options'
}
else if regexm("`options'", "load\(.*\)"){
`nq' pcn_primus_load, s(approved) `pause' `options'
}
else err
return add
exit
}
if regexm("`subcmd'", "primus[ ]+pending") {
if regexm("`options'", "down\(.*\)") {
`nq' pcn_primus_download, countries(`countries') years(`years') /*
*/ status(pending) `pause' `options'
}
else if regexm("`options'", "load\(.*\)"){
`nq' pcn_primus_load, s(pending) `pause' `options'
}
else err
return add
exit
}
//========================================================
// Download wrk version data
//========================================================
if regexm("`subcmd'", "download[ ]+wrk") {
local maindir "p:\01.PovcalNet\03.QA\02.PRIMUS\pending"
`nq' pcn_download_wrk, countries(`countries') years(`years') /*
*/ `pause' `options' maindir("`maindir'")
return add
exit
}
// -------------------------------------------------------------------------------
// Load
// -------------------------------------------------------------------------------
if ("`subcmd'" == "load" | "`subcmd'" == "load[ ]+gpwg") {
`nq' pcn_load, country(`countries') year(`years') type(`type') /*
*/ maindir("`maindir'") `pause' `options'
return add
exit
}
//========================================================
// load wrk version data
//========================================================
if regexm("`subcmd'", "load[ ]+wrk") {
local maindir "p:\01.PovcalNet\03.QA\02.PRIMUS\pending"
`nq' pcn_load_wrk, country(`countries') year(`years') /*
*/ maindir("`maindir'") `pause' `clear' `options'
return add
exit
}
//========================================================
// load estimates data
//========================================================
if regexm("`subcmd'", "load[ ]+estimates") {
local maindir "p:\01.PovcalNet\03.QA\02.PRIMUS\pending"
`nq' pcn_load_estimates, maindir("`maindir'") /*
*/ `pause' `clear' `options'
return add
exit
}
// ----------------------------------------------------------------------------------
// create text file (collapsed)
// ----------------------------------------------------------------------------------
if ("`subcmd'" == "create") {
`nq' pcn_create, countries(`countries') years(`years') type(`type') /*
*/ maindir("`maindir'") `pause' `options'
return add
exit
}
//========================================================
// Group data
//========================================================
if inlist(lower("`subcmd'"), "group", "groupdata", "gd", "groupd") {
`nq' pcn_groupdata, country(`countries') years(`years') type(`type') /*
*/ `pause' `options'
return add
exit
}
//========================================================
// Update CPI
//========================================================
if regexm("`subcmd'", "update[ ]+cpi") {
if !inlist("`c(username)'", "wb384996","WB514665") {
noi disp in r "You're not authorized to execute this command"
error
}
`nq' pcn_update_cpi, `pause' `options'
return add
exit
}
//========================================================
// Load CPI
//========================================================
if regexm("`subcmd'", "load[ ]+cpi") {
`nq' pcn_load_cpi, `pause' `options'
return add
exit
}
//========================================================
// Price framework
//========================================================
//------------update
if regexm("`subcmd'", "update[ ]+(price|framework|pf)") {
if !inlist("`c(username)'", "wb384996","WB514665") {
noi disp in r "You're not authorized to execute this command"
error
}
`nq' pcn_update_price, `pause' `options'
return add
exit
}
//------------Load
if regexm("`subcmd'", "load[ ]+(price|framework|pf)") {
`nq' pcn_load_price, `options'
return add
exit
}
//========================================================
// Master File
//========================================================
if regexm("`subcmd'", "master") {
if regexm("`options'", "update\(.*\)") {
if !inlist("`c(username)'", "wb384996","WB514665") {
noi disp in r "You're not authorized to execute this command"
error
}
`nq' pcn_master_update, `pause' `options'
return add
}
if regexm("`options'", "load\(.*\)") {
`nq' pcn_master_load, `pause' `options' `qui'
return add
}
exit
}
//========================================================
// Compare w/ server
//========================================================
if inlist(lower("`subcmd'"), "compare") {
`nq' pcn_compare, country(`countries') year(`years') ///
region(`regions') ///
`pause' `options'
return add
exit
}
if regexm("`subcmd'", "compare[ ]+graph") {
`nq' pcn_compare_gr, `pause' `options'
return add
exit
}
if regexm("`subcmd'", "compare[ ]+report") {
`nq' pcn_compare_rp, country(`countries') year(`years') ///
region(`regions') ///
`pause' `options'
return add
exit
}
//========================================================
// Production vintages
//========================================================
if regexm(lower("`subcmd'"), "^production") {
if regexm(lower("`subcmd'"), "load") {
`nq' pcn_production load, `options'
return add
}
else if regexm(lower("`subcmd'"), "create") {
if !inlist(lower("`c(username)'"), "wb384996", "wb424681") {
noi disp in red "you don't have permission to create an official povcalnet production vintage"
error
}
`nq' pcn_production create, `options'
return add
}
else {
if regexm(lower("`subcmd'"), "^production[ ]+(.*)") local action = regexs(1)
`nq' pcn_production `action', `options'
return add
}
}
} // end of qui
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
//========================================================
// executables
//========================================================
//------------Create
pcn create, countries(all) replace
//------------download
pcn download gpwg
pcn download pending
pcn download wrk
pcn update cpi
pcn master, update(gdp)
pcn master, update(pce)
Version Control:
/*====================================================================
Create repository
====================================================================*/
*--------------------1.1: Load repository data
if ("`createrepo'" != "" & "`calcset'" == "repo") {
cap confirm file "`reporoot'\repo_gpwg2.dta"
if ("`gpwg2'" == "gpwg2" | _rc) {
indicators_gpwg2, out("`out'") datetime(`datetime')
}
local dt: disp %tdDDmonCCYY date("`c(current_date)'", "DMY")
local dt = trim("`dt'")
if ("`repofromfile'" == "") {
cap datalibweb, repo(erase `repository', force) reporoot("`reporoot'") type(GMD)
datalibweb, repo(create `repository') reporoot("`reporoot'") /*
*/ type(GMD) country(`countries') year(`years') /*
*/ region(`regions') module(`module')
noi disp "repo `repository' has been created successfully."
use "`reporoot'\repo_`repository'.dta", clear
append using "`reporoot'\repo_gpwg2.dta"
}
else {
use "`reporoot'\repo_`repository'.dta", clear
}
* Fix names of surveyid and files
local repovars filename surveyid
foreach var of local repovars {
replace `var' = upper(`var')
replace `var' = subinstr(`var', ".DTA", ".dta", .)
foreach x in 0 1 2 {
while regexm(`var', "_V`x'") {
replace `var' = regexr(`var', "_V`x'", "_v`x'")
}
}
}
duplicates drop filename, force
save "`reporoot'\repo_`repository'.dta", replace
* confirm file exists
cap confirm file "`reporoot'\repo_vc_`repository'.dta"
if (_rc) {
gen vc_`dt' = 1
save "`reporoot'\repo_vc_`repository'.dta", replace
noi disp "repo_vc_`repository' successfully updated"
exit
}
use "`reporoot'\repo_vc_`repository'.dta", clear
* Fix names of surveyid and files
local repovars filename surveyid
foreach var of local repovars {
replace `var' = upper(`var')
replace `var' = subinstr(`var', ".DTA", ".dta", .)
foreach x in 0 1 2 {
while regexm(`var', "_V`x'") {
replace `var' = regexr(`var', "_V`x'", "_v`x'")
}
}
}
duplicates drop filename, force
merge 1:1 filename using "`reporoot'\repo_`repository'.dta"
cap confirm new var vc_`dt'
if (_rc) drop vc_`dt'
recode _merge (1 = 0 "old") (3 = 1 "same") (2 = 2 "new"), gen(vc_`dt')
sum vc_`dt', meanonly
if r(mean) == 1 {
noi disp in r "variable {cmd:vc_`dt'} is the same as previous version. No update"
drop vc_`dt' _merge
error
}
else {
noi disp in y "New vintages:"
noi list filename if vc_`dt' == 2
}
drop _merge
save "`reporoot'\repo_vc_`repository'.dta", replace
exit
}