-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcn_primus_query.ado
166 lines (125 loc) · 3.84 KB
/
pcn_primus_query.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
/*==================================================
project: Primus Query
Author: R.Andres Castaneda
E-email: [email protected]
url:
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 9 Aug 2019 - 12:23:48
Modification Date:
Do-file version: 01
References:
Output:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pcn_primus_query, rclass
syntax [anything(name=subcmd id="subcommand")], ///
[ ///
COUNTries(string) ///
Years(numlist) ///
REGions(string) ///
maindir(string) ///
type(string) ///
survey(string) ///
replace ///
vermast(string) ///
veralt(string) ///
MODule(string) ///
clear ///
pause ///
status(string) ///
gpwg ///
]
version 14
*---------- conditions
if ("`pause'" == "pause") pause on
else pause off
/*==================================================
1:
==================================================*/
if ("`status'" == "") local status "approved"
primus query, overalls(`status')
if ("`gpwg'" != "") {
preserve
primus query, overalls(pending)
local pr = `" "PENDING", "REJECTED", "-" "'
keep if !inlist(regional, `pr') & !inlist(decdg, `pr') & !inlist(povcalnet, `pr')
local nobs = _N
tempfile pendingf
save `pendingf', emptyok
restore
if (`nobs' != 0) {
append using `pendingf'
}
}
* replace those that finish in GPWG and SARMD or something else.
replace survey_id = regexs(1)+"GMD" if regexm(survey_id , "(.*)(GPWG.*)$")
generate datetime = string(date_modified, "%tc")
tostring _all, replace force
*----------1.1: Send to MATA
split survey_id, p("_")
local i = 1
local n = 0
local names survey vermast veralt type
cap confirm var survey_id`i'
while _rc == 0 {
if inlist(`i', 1, 2, 5, 7) {
drop survey_id`i'
}
else {
local ++n
rename survey_id`i' `: word `n' of `names''
}
local ++i
cap confirm var survey_id`i'
}
order country year survey vermast veralt type survey_id datetime
replace veralt = substr(veralt, 2, .)
replace vermast = substr(vermast, 2, .)
drop if type == "PCN"
* : list posof "country" in varlist
/*==================================================
2: Condition to filter data
==================================================*/
* Countries
if (lower("`countries'") != "all" ) {
local countrylist ""
local countries = upper("`countries'")
local countrylist: subinstr local countries " " "|", all
keep if regexm(country, "`countrylist'")
}
** years
if ("`years'" != "") {
numlist "`years'"
local years `r(numlist)'
local yearlist: subinstr local years " " "|", all
keep if regexm(year, "`yearlist'")
}
if ("`vermast'" != "") {
local vmlist: subinstr local vermast " " "|", all
keep if regexm(vermast, "`vmlist'")
}
if ("`veralt'" != "") {
local valist: subinstr local veralt " " "|", all
keep if regexm(veralt, "`valist'")
}
pause primus_query - before sending to mata
// ------------------------------------------------------------------------
// Send to mata
// ------------------------------------------------------------------------
*----------1.1:
qui ds
local varlist = "`r(varlist)'"
mata: R = st_sdata(.,tokens(st_local("varlist")))
return local varlist = "`varlist'"
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control: