-
Notifications
You must be signed in to change notification settings - Fork 96
/
csv.sas
120 lines (98 loc) · 4.21 KB
/
csv.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
/*
From: Andrew Smith ([email protected])
Subject: Re: Writing tab-delimited data
Newsgroups: comp.soft-sys.sas
Date: 1996/05/23
I've included the macro code below. It's written and named with csv files
in mind, but you can easily change it to write tab-separated data instead
by setting tab as the delimiter where indicated by comments in the code.
Andrew Smith
University of Reading
*/
------------------------------------------------------------------------
%macro csv(
data=_last_,
csvfile=,
quoteall=0, /* put quote marks around all text values? */
quote='"',
showname=1, /* write variable names as column headers? */
delim=','
);
/*********************************************************************
Export data from SAS data set &dataset to csv file &csvfile.
data is simply the name of a SAS data set
(optionally followed by data set options).
csvfile is a predefined fileref or a quoted file name
(optionally followed by data step 'file' statement
options).
Use quoteall = 1 to put quote marks around all text values
Use quoteall = 0 to quote only values that contain delimiters
(Note: In Excel, for quoted text values that contain double
quotes to be read correctly, the contained quotes need to be
duplicated; this program does not do that. Also, text values
that look numeric may be read as numeric even when quoted.)
Use quote = "'" to use single quotes on quoted text values.
Use quote = '"' for double quotes to be used (suits Excel).
Use showname = 1 to write variable names as column headers.
Use showname = 0 to suppress variable names as column headers.
Specifies delimiter between fields in the output data file.
Use delim = ',' for comma-separated values.
Use delim = '09'x for tab-delimited data on ASCII systems.
Adapted from a macro published in 'SAS Observations' in 1995.
Note: does not handle special numeric formats (such as date
or time formats) intelligently. To use special formats, copy
the program and simply insert the desired format statement
after the "format _numeric_ best12.;" statement.
*********************************************************************/
proc contents data=&data out=_temp_ (keep=name type npos) noprint;
run;
proc sort data=_temp_;
by npos; run;
data _null_;
set _temp_ end=eof;
call symput('var'||(left(put(_n_,5.))), name);
call symput('typ'||(left(put(_n_,5.))), left(put(type,8.)));
if eof then call symput('total', left(put(_n_,8.)));
run;
%if &showname %then %do;
data _null_;
file &csvfile noprint;
set _temp_ end=eof;
%if "eall %then %do;
put "e name +(-1) "e &delim @;
%end;
%else %do;
put name +(-1) &delim @;
%end;
if (eof) then put +(-1) ' '; /* remove the extra delimiter at the end */
run;
%end;
data _null_;
file &csvfile noprint %if &showname %then mod; ;
set &data;
format _numeric_ best12.;
%do i = 1 %to &total;
%if &&typ&i=1 %then %do; /* numeric variable */
if (n(&&var&i))
then put &&var&i +(-1) &delim @;
else put &delim @;
%end;
%else %do; /* character variable */
%if "eall %then %do;
if (&&var&i = '')
then put "e "e &delim @;
else put "e &&var&i +(-1) "e &delim @;
%end;
%else %do;
if (&&var&i = '') then put &delim @;
else do;
if (index(&&var&i,&delim))
then put "e &&var&i +(-1) "e &delim @;
else put &&var&i +(-1) &delim @;
end;
%end;
%end;
%end;
put +(-1) ' '; /* remove the extra delimiter at the end */
run;
%mend csv;