-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwrite_reshape_repeat.do
55 lines (48 loc) · 1.33 KB
/
write_reshape_repeat.do
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
args(pointer(`RepeatS') scalar repeat)
<% `SS' mergekey %>
<% // Drop KEY and the SET-OF variable, which will be unused. %>
<% if (repeat->child_set_of()->insheet() == `InsheetOK') { %>
drop KEY <%= repeat->child_set_of()->st_long() %>
<% } %>
<% else { %>
drop KEY
foreach var of varlist _all {
if "`:char `var'[<%= char_name("name") %>]'" == "SET-OF-<%= repeat->name() %>" {
drop `var'
continue, break
}
}
<% } %>
<% mergekey = "PARENT_KEY" %>
* Add an underscore to variable names that end in a number.
ds <%= mergekey %>, not
foreach var in `r(varlist)' {
if inrange(substr("`var'", -1, 1), "0", "9") & length("`var'") < <%= strofreal(32 - repeat->level()) %> {
capture confirm new variable `var'_
if !_rc ///
rename `var' `var'_
}
}
if _N {
tempvar j
sort <%= mergekey %>, stable
by <%= mergekey %>: generate `j' = _n
ds <%= mergekey %> `j', not
reshape wide `r(varlist)', i(<%= mergekey %>) j(`j')
* Restore variable labels.
foreach var of varlist _all {
mata: st_varlabel("`var'", st_global("`var'[<%= char_name("label") %>]"))
}
}
else {
ds <%= mergekey %>, not
foreach var in `r(varlist)' {
ren `var' `var'1
}
drop <%= mergekey %>
gen <%= mergekey %> = ""
}
rename PARENT_KEY KEY
local pos : list posof <%= adorn_quotes(repeat->long_name()) %> in repeats
local child : word `pos' of `childfiles'
save `child'