-
Notifications
You must be signed in to change notification settings - Fork 74
/
export.sas
355 lines (290 loc) · 11.1 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*=====================================================================
Program Name : export.sas
Purpose : Replacement macro for PROC EXPORT
SAS Version : SAS 9.4
Input Data : N/A
Output Data : N/A
Macros Called : parmv
Originally Written by : Scott Bass
Date : 14JAN2016
Program Version # : 1.0
=======================================================================
Scott Bass (sas_l_739@yahoo.com.au)
This code is licensed under the Unlicense license.
For more information, please refer to http://unlicense.org/UNLICENSE.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
=======================================================================
Modification History : Original version
Note: This macro is superceded by %export_dlm.
=====================================================================*/
/*---------------------------------------------------------------------
Usage:
%export(
data=sashelp.class,
file="C:\Temp\file.csv"
);
Creates C:\Temp\file.csv as a comma separated values file
(default output file format).
Aborts if C:\Temp\file.csv already exists.
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.csv",
dbms=csv
);
Creates C:\Temp\file.csv as a comma separated values file
(explicitly specified output file format).
Aborts if C:\Temp\file.csv already exists.
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.csv",
dbms=csv,
replace=Y
);
Overwrites C:\Temp\file.csv if it already exists.
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.tdf",
dbms=tab,
replace=Y
);
Creates C:\Temp\file.tdf as a tab delimited file.
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.txt",
dbms=dlm,
replace=Y
);
Creates C:\Temp\file.txt as a space delimited file
(probably not what you want).
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.txt",
dbms=dlm,
replace=Y,
delimiter=|
);
Creates C:\Temp\file.txt as a pipe delimited file.
You can use any sequence of characters (including hexadecimal constants)
as the field delimiter.
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.txt",
dbms=dlm,
replace=Y,
dlm=|
);
dlm can be used as an alias for delimiter.
=======================================================================
data class;
set sashelp.class;
label
name="Full Name"
sex="Gender"
height="How Tall?"
;
run;
%export(
data=class,
file="C:\Temp\file.txt",
dbms=dlm,
delimiter=|,
replace=Y,
label=Y
);
Creates C:\Temp\file.txt as a pipe delimited file
Uses the labels from the data set for the header column name
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.txt"
dbms=dlm,
delimiter=|,
replace=Y,
header=N
);
Creates C:\Temp\file.txt as a pipe delimited file.
Does not write a header row.
=======================================================================
filename temp temp;
%export(
data=sashelp.class,
file=temp
dbms=dlm,
delimiter=|
);
data _null_;
infile temp;
input;
putlog _infile_;
run;
Writes sashelp.class to the fileref temp.
=======================================================================
%export(
data=sashelp.class,
file="C:\Temp\file.txt
dbms=dlm,
delimiter=|,
replace=Y,
header=Y,
lrecl=999999
);
Increases the output logical record length from the default of 32767.
Use if your combined output linesize overflows 32767 characters.
-----------------------------------------------------------------------
Notes:
One of the main reasons for this macro is that PROC EXPORT puts
unwanted double-quotes around the header field names when the LABEL
option is used.
This macro also allows specifying an output logical record length.
This code is based on (i.e. virtually copied from):
https://communities.sas.com/t5/ODS-and-Base-Reporting/How-to-use-labels-in-proc-export-or-create-tab-dlm-file-using/m-p/76789#M8698
---------------------------------------------------------------------*/
%macro export
/*---------------------------------------------------------------------
Replacement macro for PROC EXPORT.
---------------------------------------------------------------------*/
(DATA= /* Dataset to export (REQ). */
/* Data set options, such as a where clause, may be */
/* specified. */
,FILE= /* Output file (REQ). */
/* Either a properly quoted physical file (single or) */
/* double quotes), or an already allocated fileref */
/* may be specified. */
,DBMS=CSV /* Output file type (REQ). */
/* CSV, TAB, or DLM may be specified. */
/* Default value is CSV. */
/* Ignored if DELIMITER is specified. */
,DELIMITER= /* Output field delimiter (Opt). */
/* If specified, the DBMS parameter is ignored. */
/* If not specified: */
/* If DBMS=CSV, DELIMITER=, */
/* If DBMS=TAB, DELIMITER="09"x (tab) */
/* If DBMS=DLM, DELIMITER=" " (space) */
,DLM= /* Output field delimiter (Opt). */
/* DLM is an alias for DELIMITER. */
/* If both are specified then DELIMITER is used. */
,REPLACE=N /* Replace output file? (Opt). */
/* Default value is NO. Valid values are: */
/* 0 1 OFF N NO F FALSE and ON Y YES T TRUE */
/* OFF N NO F FALSE and ON Y YES T TRUE */
/* (case insensitive) are acceptable aliases for */
/* 0 and 1 respectively. */
,LABEL=N /* Use data set labels instead of names? (Opt). */
/* Default value is NO. Valid values are: */
/* 0 1 OFF N NO F FALSE and ON Y YES T TRUE */
/* OFF N NO F FALSE and ON Y YES T TRUE */
/* (case insensitive) are acceptable aliases for */
/* 0 and 1 respectively. */
/* If the data set variable does not have a label */
/* then the variable name is used. */
,HEADER=Y /* Output a header row? (Opt). */
/* Default value is YES. Valid values are: */
/* 0 1 OFF N NO F FALSE and ON Y YES T TRUE */
/* OFF N NO F FALSE and ON Y YES T TRUE */
/* (case insensitive) are acceptable aliases for */
/* 0 and 1 respectively. */
/* If the data set variable does not have a label */
/* then the variable name is used. */
,LRECL=32767 /* Logical record length for output file (Opt). */
/* Default value is 32767 (32K) */
);
%local macro parmerr _dlm _type _exist;
%let macro = &sysmacroname;
%* check input parameters ;
%parmv(DATA, _req=1,_words=1,_case=N) /* words=1 allows ds options */
%parmv(FILE, _req=1,_words=1,_case=N)
%parmv(DBMS, _req=1,_words=0,_case=U,_val=CSV TAB DLM)
%parmv(DELIMITER, _req=0,_words=0,_case=N)
%parmv(DLM, _req=0,_words=0,_case=N)
%parmv(REPLACE, _req=0,_words=0,_case=U,_val=0 1)
%parmv(LABEL, _req=0,_words=0,_case=U,_val=0 1)
%parmv(HEADER, _req=0,_words=0,_case=U,_val=0 1)
%parmv(LRECL, _req=0,_words=0,_case=U,_val=POSITIVE)
%if (&parmerr) %then %goto quit;
%* additional error checking ;
%* if DELIMITER or DLM was specified then explicitly set the delimiter ;
%* and ignore the DBMS setting ;
%if (%superq(DELIMITER)%superq(DLM) ne ) %then %do;
%let _dlm = %superq(DELIMITER);
%if (%superq(_dlm) eq ) %then %let _dlm = %superq(DLM);
%let _dlm = %sysfunc(compress(&_dlm,%str(%"%')));
%let _dlm = "&_dlm";
%end;
%else %do;
%if (&DBMS eq CSV) %then %let _dlm = %str(",");
%else
%if (&DBMS eq TAB) %then %let _dlm = %str("09"x);
%else
%if (&DBMS eq DLM) %then %let _dlm = %str(" ");
%end;
%* does the output file exist? ;
%let _exist=0;
%* quoted physical file ;
%if (%sysfunc(indexc(%superq(file),%str(%"%')))) %then %do;
%let _type=FILE;
%let _exist=%sysfunc(fileexist(%superq(file)));
%end;
%* fileref ;
%else %do;
%let _type=FILEREF;
%* is the fileref allocated? ;
%if (%sysfunc(fileref(%superq(file))) gt 0) %then %do;
%let msg=%str(NO)TE: The &file fileref is not allocated. No output created.;
%put &msg;
%goto quit;
%end;
%let _exist=%sysfunc(fexist(%superq(file)));
%end;
%* file exists but replace was not specified ;
%if (&_exist and not &replace) %then %do;
%if (&_type eq FILE) %then %do;
%let file=%sysfunc(compress(%superq(file),%str(%"%')));
%let msg=%str(NO)TE: &file already exists.;
%let msg=&msg Specify REPLACE to overwrite.;
%let msg=&msg No output created.;
%end;
%else
%if (&_type eq FILEREF) %then %do;
%let msg=%str(NO)TE: %sysfunc(pathname(%superq(file))) already exists.;
%let msg=&msg Specify REPLACE to overwrite.;
%let msg=&msg No output created.;
%end;
%put &msg;
%goto quit;
%end;
data _null_;
file &file dsd dlm=&_dlm lrecl=&lrecl;
set &data;
%if (&header) %then %do;
if _n_ eq 1 then link header;
%end;
put (_all_)(:);
return;
%if (&header) %then %do;
header:
length _LABEL_ $128 _NAME_ $32;
if _n_ eq 1 then do;
do while(1);
call vnext(_name_);
if _name_ eq '_LABEL_' then leave;
_label_ = ifc(&label,vlabelx(_name_),_name_);
put _label_ @;
end;
put;
end;
return;
%end;
run;
%quit:
%mend;
/******* END OF FILE *******/