-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcn_load_wrk.ado
313 lines (235 loc) · 7.72 KB
/
pcn_load_wrk.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
/*==================================================
project: Load working data stored in P drive
Author: David L. Vargas
E-email: [email protected]
url:
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 5 Feb 2020 -
Modification Date:
Do-file version: 01
References:
Output:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pcn_load_wrk, rclass
syntax [anything(name=subcmd id="subcommand")], ///
[ ///
country(string) ///
Year(numlist) ///
REGions(string) ///
maindir(string) ///
survey(string) ///
replace ///
vermast(string) ///
clear ///
pause ///
]
version 14
*---------- pause
if ("`pause'" == "pause") pause on
else pause off
* ---- Initial parameters
local date = date("`c(current_date)'", "DMY") // %tdDDmonCCYY
local time = clock("`c(current_time)'", "hms") // %tcHH:MM:SS
local date_time = `date'*24*60*60*1000 + `time' // %tcDDmonCCYY_HH:MM:SS
local datetimeHRF: disp %tcDDmonCCYY_HH:MM:SS `date_time'
local datetimeHRF = trim("`datetimeHRF'")
local user=c(username)
//========================================================
// Check version of pending data
//========================================================
qui {
* working month
local cmonth: disp %tdnn date("`c(current_date)'", "DMY")
*Working year
local wkyr: disp %tdCCyy date("`c(current_date)'", "DMY")
* Either Annual meeting (AM) or Spring meeting (SM)
if inrange(`cmonth', 1, 4) | inrange(`cmonth', 11, 12) local meeting "SM"
if inrange(`cmonth', 5, 10) local meeting "AM"
if inrange(`cmonth', 11, 12) {
local wkyr = `wkyr' + 1 // workign for the next year's meeting
}
return local wkyr = `wkyr'
return local meeting = "`meeting'"
local maindir "`maindir'/`wkyr'_`meeting'/wrk"
//========================================================
// conditions
//========================================================
* ----- Initial conditions
local country = upper("`country'")
}
/*==================================================
1: Find path
==================================================*/
*----------1.1: Country and Year
/* for testing purposes
local maindir "p:\01.PovcalNet\01.Vintage_control"
local country "MEX"
local year "2012"
local survey "ENIGH"
*/
mata: st_local("direxists", strofreal(direxists("`maindir'/`country'")))
if (`direxists' != 1) { // if folder does not exist
noi disp as err "`country' (`type') not found"
error
}
if ("`year'" == "") {
local dirs: dir "`maindir'/`country'" dirs "`country'*", respectcase
foreach dir of local dirs {
if regexm(`"`dir'"', "(.+)_([0-9]+)_(.+)") local a = regexs(2)
local years = "`years' `a'"
}
local years = trim("`years'")
local years: subinstr local years " " ", ", all
local year = max(0, `years')
}
local dirs: dir "`maindir'/`country'" dirs "`country'_`year'*", respectcase
*----------1.2: Path
if ("`survey'" == "") {
if (wordcount(`"`dirs'"') == 0) {
noi disp in r "no survey in `country'-`year'"
error
}
else if (wordcount(`"`dirs'"') == 1) {
if regexm(`dirs', "([0-9]+)_(.+)$") local survey = regexs(2)
}
else { // if more than 1 survey per year
foreach dir of local dirs {
if regexm(`"`dir'"', "([0-9]+)_(.+)") local a = regexs(2)
local surveys = "`surveys' `a'"
}
noi disp as text "list of available surveys for `country'- `year'"
local i = 0
foreach survey of local surveys {
local ++i
noi disp `" `i' {c |} {stata `survey'}"'
}
noi disp _n "select survey to load" _request(_survey)
}
}
else {
local survey = upper("`survey'")
}
*-------- 1.3 version
local surdir "`maindir'/`country'/`country'_`year'_`survey'"
* vermast
if ("`vermast'" == "") {
local dirs: dir "`surdir'" dirs "*`type'", respectcase
if (`"`dirs'"' == "") {
noi disp as err "no GMD collection for the following combination: " ///
as text "`country'_`year'_`survey'"
error
}
foreach dir of local dirs {
if regexm(`"`dir'"', "_[Vv]([0-9]+)_[Mm]_") local a = regexs(1)
local vms = "`vms' `a'"
}
local vms = trim("`vms'")
local vms: subinstr local vms " " ", ", all
local vm = max(0, `vms')
if (length("`vm'") == 1) local vermast = "0`vm'"
else local vermast = "`vm'"
}
else {
if regexm("`vermast'", "^[Vv]([0-9]+)") local vermast = regexs(1)
if (length("`vermast'") == 1) local vermast = "0`vermast'"
}
/*==================================================
2: Loading according to type
==================================================*/
*----------2.2: Load data
local survid = "`country'_`year'_`survey'_V`vermast'_M_WRK_A_GMD"
local dirname "`maindir'/`country'/`country'_`year'_`survey'/`survid'/Data"
local files: dir "`dirname'" files "*.dta", respectcase
/** There should be only one file per folder **/
if (wordcount(`"`files'"') == 0) {
noi disp in r "no survey in `country'-`year'"
error
}
else if (wordcount(`"`files'"') == 1) {
local filename = regexr(`files',`".dta"',"")
}
else { // if more than 1 file (should not happen, but just in case)
noi disp as text "list of available surveys for `country'- `year'"
local i = 0
foreach file of local files {
local ++i
noi disp `" `i' {c |} {stata `file'}"'
}
noi disp _n "select file to load" _request(_file)
local filename = regexr(`"`file'"',".dta",`""')
}
return local surdir = "`surdir'"
return local survid = "`survid'"
return local survin = "`country'_`year'_`survey'_v`vermast'_M_WRK_A"
return local filename = "`filename'"
use "`surdir'/`survid'/Data/`filename'.dta", clear
noi disp as text "`filename'.dta" as res " successfully loaded"
/*==================================================
3:
==================================================*/
*----------3.1:
*----------3.2:
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control:
*** Feb 11
if (wordcount(`"`files'"') == 0) {
noi disp in r "no survey in `country'-`year'"
error
}
else if (wordcount(`"`files'"') == 1) {
if regexm(`files', "_v`vermast'_M_WRK_A_GMD_(.+).dta") local ending = regexs(1)
}
else { // if more than 1 survey per year
foreach file of local files {
if regexm(`"`file'"', "_v`vermast'_M_WRK_A_GMD_(.+).dta") local a = regexs(1)
local ending = "`ending' `a'"
}
noi disp as text "list of available surveys for `country'- `year'"
local i = 0
foreach e of local ending {
local ++i
noi disp `" `i' {c |} {stata `survey' GMD `e'}"'
}
noi disp _n "select survey to load" _request(_ending)
}
*** Feb 13
** serch for the ending
local k = 0
foreach file of local files {
local ++k
if regexm("`file'", "`survid'_(.+)\.dta"){
local e = regexs(1)
local endings "`endings' `e'"
}
else{
local endings "`endings'"
}
}
* select file ending
if (`k' == 0) {
noi disp in r "no survey in `country'-`year'"
error
}
else if (`k' == 1) {
local ending = subinstr("`endings'"," ","",.)
}
else{
foreach ending of local endings {
local ++i
noi disp `" `i' {c |} {stata `survey' GMD `ending'}"'
}
noi disp _n "select survey to load" _request(_ending)
}
if ("`ending'" != "") local filename "`survid'_`ending'"
else local filename "`survid'"