You are on page 1of 35

SAS Export

Last Updated : 29 June, 2004

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

READING FROM WRITING TO


AN EXTERNAL FILE AN EXTERNAL FILE

The DATA statement The DATA statement


begins the DATA step. begins the DATA step.

The INFILE statement identifies an The FILE statement identifies an


external file to read with an INPUT external file to write with a PUT
statement. statement.

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.

General form of the FILE statement:

FILE
FILE file-specification
file-specification<options>;
<options>;

You can use the FILE statement in


conditional processing (IF-THEN/ELSE or
SELECT) because it is executable.
The PUT Statement
The PUT statement can write lines to the
external file that is specified in the most
recently executed FILE statement.
General form of the PUT statement:
PUT
PUTvariable-1
variable-1 variable-2
variable-2…
… variable-n;
variable-n;

With simple list output, you list the names


of the variables whose values you want
written. The PUT statement writes a
variable value, inserts a single blank, and
then writes the next value.
Modified List Output
Modified list output increases the
versatility of the PUT statement because
you can specify a SAS format to control
how the variable values are written.
To use modified list output, use the colon
(:) format modifier in the PUT statement
between the variable name and the
format.
.. PUT
PUTvariable-1
variable-1 :: format-1.
format-1.
. variable-2
variable-2:: format-2.
format-2.

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

Colonial 13803 14001 355182.74


Townhouse 13894 14016 241225.17
Townhouse 14108 14392 238135.98
Ranch 14585 14736 219391.80
Victorian 14805 15106 358186.78
Writing to an External File

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

NOTE: 5 records were written to the file


‘export.dat'.
The minimum record length was 34.
The maximum record length was 38.
NOTE: There were 5 observations read from
the data set PROG2.MAYSALES.

Can you use PROC PRINT to view the raw


data file?
The FSLIST Procedure
The FSLIST procedure enables you to
browse external files that are not SAS data
sets within an interactive SAS session.

PROC
PROCFSLIST
FSLISTFILEREF=file-specification
FILEREF=file-specification<option(s)>;
<option(s)>;
RUN;
RUN;

Remember to close the FSLIST window


when you have finished browsing your
external file.
Reading from an External File

proc fslist fileref='raw-data-file';


run;

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

How can you add a single row of column headers


before the rows of data?
The _N_ Automatic Variable
(Review)
The _N_ automatic variable is created by
every DATA step.
Each time the DATA step loops past the
DATA statement, _N_ is incremented by 1.
Therefore, the value of _N_ represents the
number of times the DATA step has
iterated.
_N_ is added to the program data vector
but is not output.
Writing to an External File
data _null_;

set prog2.maysales;

file 'raw-data-file';
if _N_=1 then

Why is the second PUT statement not


contained
putin'Description
an ELSE statement?
' 'ListDate '
'SellDate ' 'SellPrice';
Writing to an External File

proc fslist fileref='raw-data-file';


run;

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

How can you add a footer record after the


rows of data?
The END= Option in the SET
Statement
The END= option in the SET statement creates and
names a temporary variable that acts as an end-of-
file indicator.

SET
SET SAS-data-set
SAS-data-set END=variable
END=variable<options>;
<options>;

The temporary variable, which is initialized to 0, is


set to 1 when the SET statement reads the last
observation of the data set listed.
The variable is not added to any new data set.
Writing to an External File
 data _null_;

set prog2.maysales end=IsLast;

file 'raw-data-file';
if _N_=1 then

put 'Description ' 'ListDate '


'SellDate ' 'SellPrice';
Execute 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

...
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

proc fslist fileref='raw-data-file';


run;

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
Specifying an Alternate
Delimiter
Use the DLM= option in the FILE
statement to create a file with an
alternate delimiter (other than a blank).

FILE
FILE file-specification
file-specificationDLM='quoted-string'
DLM='quoted-string'
<other-options>;
<other-options>;

You can also specify a character variable


whose value contains your delimiter,
instead of a quoted string.
Writing to an External File
 data _null_;

set prog2.maysales end=IsLast;

file 'raw-data-file' dlm=',';


if _N_=1 then

put 'Description,ListDate,'
'SellDate,SellPrice';
Writing to an External File

proc fslist fileref='raw-data-file';


run;

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

These commas act as delimiters. The DLM= option in the


FILE statement causes their appearance.

 Partial Output

Colonial,16OCT1997,02MAY1998,$355,183
Colonial,16OCT1997,02MAY1998,$355,183

This comma acts as part of a SAS format. The DOLLAR8.


format in the PUT statement causes its appearance.
How many data values are contained in each record?
How many comma-delimited fields are in each record?
...
...
Accounting for Embedded
Delimiters
Use the DSD option in the FILE statement to
write data items that contain embedded
delimiters.

FILE
FILEfile-specification
file-specification DLM='quoted-string'
DLM='quoted-string'DSD
DSD
<other-options>;
<other-options>;

Any data item that contains the specified


delimiter is quoted with the double quotation
mark (") when the data item is output.
Writing to an External File
 data _null_;

set prog2.maysales;

file 'raw-data-file' dlm=',' dsd;


put Description
ListDate : date9.
SellDate : date9.
SellPrice : dollar8.;
run;
Writing to an External File

proc fslist fileref='raw-data-file';


run;

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

You might also like