SAS Export
SAS Export
Center of
Excellence
Objectives
Write observations from a SAS data set to
a
comma-delimited external file.
Use DATA step logic to insert a header
record and a footer record into an
external file.
Introduction
You can use the DATA step to write
a custom report
data to an external file to be read by other
programming languages or software.
Introduction
The INPUT statement describes the The PUT statement describes the
arrangement of values in the input arrangement of values in the output
data record. data record.
The DATA Statement
Usually, the DATA statement specifies at
least one data set name that the SAS
System uses to create an output data set.
Using the keyword _NULL_ as the data set
name causes SAS to execute the DATA
step without writing observations to a
data set.
DATA
DATA _NULL_;
_NULL_;
The FILE Statement
The FILE statement can be used to specify
the output destination for subsequent PUT
statements.
FILE
FILE file-specification
file-specification<options>;
<options>;
variable-n
variable-n:: format-n.;
format-n.;
Writing to an External File
The prog2.maysales data set contains
information about houses. Read this data
set and write the data to an external file.
prog2.maysales
List Sell
Description Date Date SellPrice
data _null_;
set prog2.maysales;
file 'raw-data-file';
Why is the $ omitted after Description in the
PUT statement?
put Description
ListDate : date9.
Writing to an External File
Partial SAS Log
PROC
PROCFSLIST
FSLISTFILEREF=file-specification
FILEREF=file-specification<option(s)>;
<option(s)>;
RUN;
RUN;
set prog2.maysales;
file 'raw-data-file';
if _N_=1 then
SET
SET SAS-data-set
SAS-data-set END=variable
END=variable<options>;
<options>;
file 'raw-data-file';
if _N_=1 then
PDV
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
run;
PDV
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
0 . . . 1
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
run;
PDV
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
0 Colonial 13803 14001 355182 1
...
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
run;
PDV
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
0 Colonial 13803 14001 355182 1
...
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
Continue executing DATA run;
step. _N_ is equal to 1.
Write header record PDV
to raw-data-file.
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
0 Colonial 13803 14001 355182 1
...
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
IsLast is equal to 0. run;
PUT statement is not
executed. PDV
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
0 Colonial 13803 14001 355182 1
...
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
IsLast is equal to 0. run;
PUT statement is not
executed. PDV
LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
0 Townhome 13894 14108 241225 2
...
...
data _null_;
set prog2.maysales end=IsLast;
Partial Listing of prog2.maysales file 'raw-data-file';
List Sell if _N_=1 then
Description Date Date put 'Description ' 'ListDate '
'SellDate ' 'SellPrice';
Colonial 13803 14001 put Description
Townhouse 13894 14016 ListDate : date9.
Townhouse 14108 14392 SellDate : date9.
Ranch 14585 14736 SellPrice : dollar8.;
Victorian 14805 15106 if IsLast=1 then
put 'Data: PROG2.MAYSALES';
IsLast is equal to 1. run;
PUT statement is
executed. Write footer PDV
record to raw-data-file. LIST SELL SELL
ISLAST DESCRIPTION DATE DATE PRICE _N_
D N $ N N N D N
8 9 8 8 8 8
1 Victorian 14805 15106 358156 5
...
...
Writing to an External File
FILE
FILE file-specification
file-specificationDLM='quoted-string'
DLM='quoted-string'
<other-options>;
<other-options>;
put 'Description,ListDate,'
'SellDate,SellPrice';
Writing to an External File
Description,ListDate,SellDate,SellPrice
Colonial,16OCT1997,02MAY1998,$355,183
Townhouse,15JAN1998,17MAY1998,$241,225
Townhouse,17AUG1998,28MAY1999,$238,136
Ranch,07DEC1999,06MAY2000,$219,392
Victorian,14JUL2000,11MAY2001,$358,187
Data: PROG2.MAYSALES
What is the role of each comma in
the records containing data?
Embedded Delimiters
Partial Output
Colonial,16OCT1997,02MAY1998,$355,183
Colonial,16OCT1997,02MAY1998,$355,183
FILE
FILEfile-specification
file-specification DLM='quoted-string'
DLM='quoted-string'DSD
DSD
<other-options>;
<other-options>;
set prog2.maysales;
Colonial,16OCT1997,02MAY1998,"$355,183"
Townhouse,15JAN1998,17MAY1998,"$241,225"
Townhouse,17AUG1998,28MAY1999,"$238,136"
Ranch,07DEC1999,06MAY2000,"$219,392"
Victorian,14JUL2000,11MAY2001,"$358,187"
Questions