-
Notifications
You must be signed in to change notification settings - Fork 5
/
puse.ado
123 lines (107 loc) · 3.1 KB
/
puse.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
program define puse, rclass
version 14
/*
puse tries to read data and register project functionality in the following
order (unless specified otherwise by user):
Read:
1. DTA
2. CSV
3. Excel
project:
1. CSV
2. DTA
3. Excel
*/
capture findfile init.ado
if "`r(fn)'" == "" {
di as txt "user-written package init needs to be installed first;"
di as txt "use -ssc install init- to do that"
exit 498
}
syntax, file(string asis) [clear debug opts(string) original preserve]
if "`preserve'" == "preserve"{
di "Option preserve is ignored in puse"
}
// Generate names of files based on extension
local newfile = subinstr(`file', ".csv", "", .)
local newfile = subinstr("`newfile'", ".dta", "", .)
local filecsv = "`newfile'" + ".csv"
local filedta = "`newfile'" + ".dta"
if strpos(`file', ".xls") > 0{
local filexls = `file'
}
// Check existance of files
capture confirm file "`filedta'"
local dtaExists = _rc
capture confirm file "`filecsv'"
local csvExists = _rc
capture confirm file "`filexls'"
local xlsExists = _rc
local exists = min(`dtaExists', `csvExists', `xlsExists')
if `exists' != 0{
di as error "No files found: `filedta' `filecsv' `filexls'"
error 601
}
// Throw exception if user specifies a file extension, but puse reads a different one.
if strpos(`file', ".csv") > 0 & `dtaExists' == 0{
di as error "CSV specified, but puse reads DTA file."
error 601
}
else if strpos(`file', ".xls") > 0 & `dtaExists' == 0{
di as error "XLS specified, but puse reads DTA file"
error 601
}
else if strpos(`file', ".xls") > 0 & `csvExists' == 0{
di as error "XLS/XLSX specified, but puse reads CSV file"
error 601
}
// Import data
if `dtaExists' ==0{ //If DTA file exists read DTA file
use "`filedta'" , `clear'
}
else if `csvExists' == 0{ // If no DTA present and CSV exists, read CSV
import delimited using "`filecsv'", ///
`clear' `opts'
}
else if `xlsExists' == 0 { // Otherwise read Excel
if "`opts'" == "" & "`clear'" == ""{
import excel using "`filexls'"
}
else {
import excel using "`filexls'" , `opts' `clear'
}
}
*** CSV files are better for project functionality since they don't
*** store metadata
// Register project functionality
if `csvExists' == 0{ // If CSV file exists register project functionality using CSV
if "`debug'" == ""{ // If debug option wasn't set, use project functionality
if "`original'" == ""{
project , uses("`filecsv'") preserve
}
else{
project , original("`filecsv'") preserve
}
}
}
else if strpos(`file', ".xls") == 0{ // IF CSV wasn't found and no Excel file specified, register project using DTA
if "`debug'" == ""{ // If debug option wasn't set, use project functionality
if "`original'" == ""{
project , uses("`filedta'") preserve
}
else{
project , original("`filedta'") preserve
}
}
}
else {
if "`debug'" == ""{ // If debug option wasn't set, use project functionality
if "`original'" == ""{
project , uses("`filexls'") preserve
}
else{
project , original("`filexls'") preserve
}
}
}
end