-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcn_groupdata.ado
313 lines (225 loc) · 8.3 KB
/
pcn_groupdata.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: Create group data files from raw information
and update master file with means
Author: R.Andres Castaneda Aguilar
E-email: [email protected]
url:
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 23 Oct 2019 - 09:04:24
Modification Date:
Do-file version: 01
References:
Output:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pcn_groupdata, rclass
syntax [anything(name=subcmd id="subcommand")], ///
COUNtry(string) ///
[ ///
Years(numlist) ///
maindir(string) ///
type(string) ///
clear ///
pause ///
vermast(string) ///
veralt(string) ///
replace ///
* ///
]
version 14
*---------- conditions
if ("`pause'" == "pause") pause on
else pause off
if (wordcount("`country'") != 1) {
noi disp in red "{it: country()} must have only one countrycode"
error
}
//------------set up
if ("`maindir'" == "") cd "p:\01.PovcalNet\03.QA\01.GroupData"
else cd "`maindir'"
*------------------ Time and system Parameters ------------
local date = c(current_date)
local time = c(current_time)
local datetime = clock("`date'`time'", "DMYhms") // number, not date
local user = c(username)
local dirsep = c(dirsep)
local vintage: disp %tdD-m-CY date("`c(current_date)'", "DMY")
//------------load data
import excel "04.formatted/`country'/`country'_raw_GroupData.xlsx", /*
*/ sheet("raw_GroupData") firstrow clear
tostring survey, replace // in case survey is unknown
gen id = countrycode + " " + strofreal(year) + " " + /*
*/ coverage + " " + datatype + " 0" + /*
*/ strofreal(formattype) + " " + survey
//------------saving data vintages
levelsof id, local(ids)
qui foreach id of local ids {
local cc: word 1 of `id'
local yr: word 2 of `id'
local cov: word 3 of `id'
local dt: word 4 of `id'
local ft: word 5 of `id'
local sy: word 6 of `id'
local l2y = substr("`yr'", 3,.)
if (inlist("`sy'", "", ".")) local sy = "USN" // Unknown Survey Name
//------------Create directories
local cc = upper("`cc'")
cap mkdir "../../01.Vintage_control/`cc'"
local sydir "../../01.Vintage_control/`cc'/`cc'_`yr'_`sy'"
cap mkdir "`sydir'"
preserve
keep if id == "`id'"
keep weight welfare
replace welfare = welfare/12
local dirs: dir "`sydir'" dirs "*GMD", respect
if (wordcount(`"`dirs'"') == 0) {
local fileid "`cc'_`yr'_`sy'_v01_M_v01_A_GMD"
}
else if (wordcount(`"`dirs'"') == 1) {
local fe = "" // file exists
local va = ""
local dir `dirs'
local fileid `dir'
if regexm("`dir'", "v([0-9]+)_A") local va = "`va' " + regexs(1)
local exfile: dir "`sydir'/`dir'/Data" files "*GMD_GD-`cov'.dta", respect
if (`"`exfile'"' == "") local fe = "`dir'" // file does not exists
}
else{
local fe = "" // file exists
local va = ""
foreach dir of local dirs {
if regexm("`dir'", "v([0-9]+)_A") local va = "`va' " + regexs(1)
local exfile: dir "`sydir'/`dir'/Data" files "*GMD_GD-`cov'.dta", respect
if (`"`exfile'"' != "") continue
else local fe = "`dir'" // file does not exists
}
local va: subinstr local va " " ",", all
local va = "0" + "`va'"
local va = max(`va')
if length("`va'") == 1 local va = "0"+"`va'"
local fileid "`cc'_`yr'_`sy'_v01_M_v`va'_A_GMD"
}
local signature "`cc'_`yr'_`sy'_GMD_GD-`cov'"
cap datasignature confirm using /*
*/ "`sydir'/`fileid'/Data/`signature'", strict
local dsrc = _rc
if (`dsrc' == 601) {
// nothing
}
if (`dsrc' == 9) {
if ("`fe'" == "") local va = `va' + 1
else local va = `va'
if length("`va'") == 1 local va = "0"+"`va'"
local fileid "`cc'_`yr'_`sy'_v01_M_v`va'_A_GMD"
}
if (`dsrc' != 0) {
local verid "`sydir'/`fileid'"
cap mkdir "`verid'"
cap mkdir "`verid'/Data"
noi datasignature set, reset /*
*/ saving("`verid'/Data/`signature'", replace)
//------------Include Characteristics
local datetimeHRF: disp %tcDDmonCCYY_HH:MM:SS `datetime'
local datetimeHRF = trim("`datetimeHRF'")
char _dta[filename] `fileid'_GD-`cov'.dta
char _dta[id] `fileid'
char _dta[welfaretype] `dt'
char _dta[weighttype] "PW"
char _dta[countrycode] `cc'
char _dta[year] `yr'
char _dta[survey_coverage] `cov'
char _dta[groupdata] 1
char _dta[formattype] `ft'
char _dta[datetime] `datetime'
char _dta[datetimeHRF] `datetimeHRF'
char _dta[gdtype] "T`ft'"
char _dta[datatype] "`dt'"
cap mkdir "`sydir'/_vintage"
save "`sydir'/_vintage/`signature'_`datetime'.dta", replace
save "`verid'/Data/`fileid'_GD-`cov'.dta", replace
export delimited using "`verid'/Data/`fileid'_GD-`cov'.txt", ///
novarnames nolabel delimiter(tab) `replace'
export delimited using "`verid'/Data/`cc'`cov'`l2y'.T`ft'", ///
novarnames nolabel delimiter(tab) `replace'
}
else {
noi disp in y "File " in w "`fileid'_GD-`cov'.dta" in /*
*/ y " is up to date."
}
*get the mean
sum welfare [w = welfare], meanonly
local `cc'`yr'`cov'm = r(mean)
restore
}
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control:
local date = c(current_date)
local time = c(current_time)
local datetime = clock("`date'`time'", "DMYhms")
disp "`datetime'"
disp %tcDDmonCCYY_HH:MM:SS `datetime'
disp %tcCCYYmonDDHHMMSS `datetime'
disp %tcCCYYNNDDHHMMSS `datetime'
local a: disp %tcCCYYNNDDHHMMSS `datetime'
local b = clock("`a'", "YMDhms")
disp %tcDDmonCCYY_HH:MM:SS `datetime'
disp %tcDDmonCCYY_HH:MM:SS `b'
local vcnumbers: dir "." files "zzz*"
local vcnumbers: subinstr local vcnumbers "zzz" "", all
local exfile: dir "../../01.Vintage_control/CHN/CHN_2016_USN/chn_2016_usn_v01_m_v01_a_pcngd/Data" files "*PCNGD-2.dta"
disp `"`exfile'"'
//------------ Check both files are in the most recent folder
qui foreach id of local ids {
local cc: word 1 of `id'
local yr: word 2 of `id'
local cov: word 3 of `id'
local dt: word 4 of `id'
local ft: word 5 of `id'
local sy: word 6 of `id'
local l2y = substr("`yr'", 3,.)
if (inlist("`sy'", "", ".")) local sy = "USN" // Unknown Survey Name
local cc = upper("`cc'")
local sydir "../../01.Vintage_control/`cc'/`cc'_`yr'_`sy'"
local dirs: dir "`sydir'" dirs "*GMD", respect
local fe = "" // file exists
local va = ""
foreach dir of local dirs {
if regexm("`dir'", "v([0-9]+)_A") local va = "`va' " + regexs(1)
local exfile: dir "`sydir'/`dir'/Data" files "*GMD_GD-`cov'.dta", respect
if (`"`exfile'"' != "") continue
else local fe = "`dir'" // file does not exists
}
if ("`fe'" != "") {
local mfiles: dir "`sydir'/_vintage" files "`signature'*.dta", respect
disp `"`mfiles'"'
local vcs: subinstr local mfiles "`signature'_" "", all
local vcs: subinstr local vcs ".dta" "", all
local vcs: list sort vcs
disp `"`vcs'"'
mata: VC = strtoreal(tokens(`"`vcs'"')); /*
*/ st_local("mvc", strofreal(max(VC), "%15.0f"))
copy "`sydir'/vintage/`signature'_`mvc'.dta" "`sydir'/`fe'/Data/`fe'_GD-`cov'.dta"
use "`sydir'/`fe'/Data/`fe'_GD-`cov'.dta", clear
local datetimeHRF: disp %tcDDmonCCYY_HH:MM:SS `datetime'
local datetimeHRF = trim("`datetimeHRF'")
char _dta[filename] `fe'_GD-`cov'.dta
char _dta[id] `fe'
char _dta[datetime] `datetime'
char _dta[datetimeHRF] `datetimeHRF'
save, replace
export delimited using "`sydir'/`fe'/Data/`fileid'_GD-`cov'.txt", ///
novarnames nolabel delimiter(tab) `replace'
export delimited using "`sydir'/`fe'/Data/`cc'`cov'`l2y'.T`ft'", ///
novarnames nolabel delimiter(tab) `replace'
}
}