-
Notifications
You must be signed in to change notification settings - Fork 22
/
EXPORT.sas
132 lines (103 loc) · 2.8 KB
/
EXPORT.sas
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
/*
Author: Edwin Hu
Date: 2013-05-24
# EXPORT #
## Summary ##
Exports SAS Datasets to a variety of formats (.csv, .tsv, .dta, .xls, .xlsx)
and also exports the column descriptions to make it easy to port tables to
other data storage format.
## Variables ##
- lib: library where SAS file is located
- dsetin: SAS file to export
- dir: path to export file to (do not use quotes)
- outfile: name of file to create (do not include suffix)
- dbms: file format (e.g. .csv)
- format: manually set column formats using DATA Step format syntax
- debug: keep or delete temporary files
## Usage ##
```
%IMPORT "~/git/sas/EXPORT.sas";
%EXPORT(lib=user,dsetin=&syslast.,
dir=/path/to/output/file,outfile=outfile,
dbms=csv,
format=,
debug=n);
```
*/
%MACRO EXPORT(lib=,dsetin=,
dir=,outfile=,
dbms=csv,
format=,
debug=n);
/* Summarize dataset metadata */
proc sql;
create view _data_info as
select name as Column,
label as Description,
type as Column_Type,
format,
informat
from sashelp.vcolumn
where libname="%UPCASE(&lib.)" and memname="%UPCASE(&dsetin.)"
;
quit;
/* Create a standardized copy of the data
this is important when there are 'special missing' numeric values
which will print out as chars in the output and interfere with I/O
*/
data _std_&dsetin. / view = _std_&dsetin.;
set &lib..&dsetin.;
array a(*) _numeric_;
do i=1 to dim(a);
if a(i) <= .Z then a(i) = .;
end;
drop i;
format _character_ $quote. &format.;
run;
%if %SUBSTR(%LOWCASE(&dbms.),1,1) = c %then %do;
/* Export CSVs */
proc export data=_data_info
outfile="&dir./&outfile._desc.csv"
dbms=csv replace;
run;
proc export data=_std_&dsetin.
outfile="&dir./&outfile..csv"
dbms=csv replace;
run;
%end;
%else %if %SUBSTR(%LOWCASE(&dbms.),1,1) = t %then %do;
/* Export TSVs */
proc export data=_data_info
outfile="&dir./&outfile._desc.tsv"
dbms=tab replace;
run;
proc export data=_std_&dsetin.
outfile="&dir./&outfile..tsv"
dbms=tab replace;
run;
%end;
%else %if %SUBSTR(%LOWCASE(&dbms.),1,1) = x | %SUBSTR(%LOWCASE(&dbms.),1,1) = e %then %do;
/* Export XLS with two sheets */
proc export data=_data_info
outfile="&dir./&outfile..xlsx"
dbms=xlsx replace;
sheet="description";
run;
proc export data=_std_&dsetin.
outfile="&dir./&outfile..xlsx"
dbms=xlsx replace;
sheet="data";
run;
%end;
%else %do;
/* Throw error if not implemented */
ERROR 'Other file formats not implemented';
%ABORT;
%end;
%if %SUBSTR(%LOWCASE(&dbms.),1,1) = n %then %do;
* Delete Description File ;
proc datasets nolist;
delete _data_info, _std_&dsetin. / memtype=view;
run;
%end;
%MEND;