-
Notifications
You must be signed in to change notification settings - Fork 6
/
dt_restart_stata.mata
85 lines (71 loc) · 1.59 KB
/
dt_restart_stata.mata
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
mata
/**
* @brief Restarts stata
* @param cmd Comand to execute after restarting stata
* @returns Restarts stata, loads data and executes -cmd-
*/
void function dt_restart_stata(| string scalar cmd, real scalar ext) {
real scalar fh
real scalar isdta, ismata
string scalar tmpcmd
/* Checking if a dataset is loaded */
if ( (isdta = c("N") | c("k")) )
{
string scalar tmpdta, dtaname
dtaname = c("filename")
tmpdta = dt_random_name()+".dta"
stata("save "+tmpdta+", replace")
}
/* Checking if profile file already exists */
real scalar isprofile
isprofile = fileexists("profile.do")
string scalar tmpprofile
tmpprofile=dt_random_name()+".do"
if (isprofile)
{
dt_copy_file("profile.do", tmpprofile)
fh = fopen("profile.do","a")
}
else fh = fopen("profile.do","w")
if (isdta) fput(fh, "use "+tmpdta+", clear")
/* if (ismat) fput( */
if (cmd != J(1,1,"")) fput(fh, cmd)
/* Operations to occur right after loading the data*/
if (isprofile)
{
dt_copy_file(tmpprofile, "profile.do", tmpcmd)
fput(fh, tmpcmd)
fput(fh, "erase "+tmpprofile)
}
else
{
dt_erase_file("profile.do", tmpcmd)
fput(fh, tmpcmd)
}
/* Modifying data */
if (isdta)
{
fput(fh, "erase "+tmpdta)
fput(fh, "global S_FN ="+dtaname)
}
fclose(fh)
if (ext==J(1,1,.)) ext=1
string scalar statapath
statapath = dt_stata_path(1)
if (!dt_stata_capture("winexec "+statapath))
{
if (ext) stata("exit, clear")
}
else
{
if (isprofile)
{
dt_copy_file(tmpprofile,"profile.do")
unlink(tmpprofile)
}
else dt_erase_file("profile.do")
if (isdta) unlink(tmpdta)
}
return
}
end