You are on page 1of 8

Importing Excel Files Into SAS Using DDE

Curtis A. Smith, Defense Contract Audit Agency, La Mirada, CA

ABSTRACT
W ith the popularity of Excel files, the SAS user could use an easy way to get Excel files into SAS. There are
m any good m ethods to do so, one of them being Dynam ic Data Exchange (DDE). A SAS user can use DDE
within SAS code to m ake im porting an Excel file routine and easily repeatable. The author will show how he
uses DDE within SAS to routinely im port into SAS lookup tables downloaded from an ERP system as Excel
files. The basic DDE statem ents needed to accom plish this are surprising sim ple.

INTRODUCTION
W hat in the world is DDE and why use it? Good question... DDE is “Dynam ic Data Exchange” and is a m ethod
of accessing data from one MS-W indows application by another. As a SAS user, you can use DDE within a
DATA step to im port data into SAS and export data from SAS. Using DDE involves SAS code and statem ents
that other MS-W indows applications understand. But, why bother with DDE when other m ethods are
available? W hat about the IMPORT and EXPORT procedures? Or, Open Database Connectivity (ODBC)?
How about SAS Enterprise Guide? These alternatives are useful under the right conditions. For exam ple, the
PROC IMPORT and PROC EXPORT are sim ple to use, but are lim ited in the way you can define your data
and these procedures require you license SAS/ACCESS For PC Form ats. Bum m er. ODBC is also sim ple to
use, but im porting data into SAS using ODBC requires you license SAS/ACCESS For ODBC. Bum m er again.
And, the new Excel LIBNAME engine? Oh, that also requires you license SAS/ACCESS For PC Form ats.
Bum m er tim es three. SAS Enterprise Guide is very sim ple, and does not require you license anything other
than BASE SAS. Hm m . Oh yes, SAS Enterprise Guide doesn’t offer quite the flexibility as DDE, because when
using DDE you have all the features of the SAS DATA step. Also, using DDE in a SAS DATA step m akes it
easy to adapt the im port process for repeatable uses. A SAS DATA step using DDE only requires BASE SAS
running under MS-W indows.

In m y applications, I routinely download tables from an ERP system that likes to dum p the data into an Excel
file. Each tim e I download a fresh table of data into the Excel file, I want to re-run m y SAS im port process to
create a fresh SAS table. I download several sim ilar tables from the ERP system and like to use m acro
variables within m y SAS code to im port any one of the several Excel files downloaded from the ERP system .
So, a DATA step using DDE works very well for m y situation. Throughout the rem ainder of this paper I will
describe only the process of im porting Excel worksheets into a SAS data set, as the purpose of this paper is
to provide a working introduction to the use of DDE to get Excel data into SAS. T o that end, I will use a
straightforward exam ple.

DDE BASICS
Dynam ic Data Exchange is a m ethod to dynam ically exchange data between MS-W indows applications. DDE
uses a client-server relationship to enable client applications to request data from a server application. In the
context of SAS and DDE, SAS is always the client, regardless of which application is receiving the data. SAS
request data from server applications, sends data to server applications, or sends com m ands to server
applications. DDE has m any uses: the one that we will explore is acquiring data from another MS-W indows
application, nam ely Excel.

A search of the Microsoft web page for “m acrofun” will reward you with the MS-W indows “HLP” file
“m acrofun.hlp” that will provide all of the wonderful Excel m acro com m ands that you can use within the DDE
statem ents. Although these m acros are older technology, for use with DDE they get the job done.

COMMUNICATION IS KEY

To use DDE, both SAS (the client) and the application owning the data you wish to im port (the server) m ust
be running, and the data that is to be read m ust be open. Com m unication between two MS-W indows
applications is accom plished using what is known as the DDE Triplet. The triplet requires three param eters,
in the following form :

1
'application-nam e | topic ! item'

where:
application-nam e
is the executable filenam e of the server application. For exam ple, the application-nam e for Microsoft Excel
is “excel”.

topic
is the subject of conversation (between SAS and the DDE server application). This is typically the full drive,
path, and filenam e of the spreadsheet with which you want to share data. As W atts points out, only enough
inform ation for identifying a specific worksheet is actually required. For exam ple, If a single workbook is
opened on the desktop,"excel|sheet1!..." will be enough to identify it. W hen specifying the entire Excel
workbook and worksheet, you enclose the workbook nam e in brackets [ ].

item
is the range of conversation specified between the client and server applications. W hen reading data from
Excel, this will be a range of cells.

Notice that the triplet is quoted, the application-name and topic are separated by a vertical bar |, and the topic
and item are separated by an exclam ation m ark !.

There is an easy to use trick to determ ine the exact string for the DDE Triplet. Use Excel to copy the desired
range of cells to the MS-W indows clipboard. Then, while in SAS for MS-W indows, use the pull-down m enu
option Solutions\Accessories\DDE triplet. You will see som ething like the following:

You can then select the text, copy, and paste it into your SAS code. In m y exam ple the DDE Triplet looks like
this:

Excel|C:\WUSS\[Names Workbook.xls]Names Worksheet!R1C1:R11C6

Microsoft defined the DDE topic “SYSTEM” to enable com m ands to be sent to the server application. W hen
sending com m ands to a server application, we need only specify the application-name and the special DDE
topic “SYSTEM”. Therefore, there is no need for an item param eter when only sending com m ands to Excel.
Thus, the DDE triplet for sending com m ands to Excel contains only two param eters, with the application-name
being set to “EXCEL” and the topic being set to “SYSTEM”. The functionality of this special two level DDE
Triplet is different from the norm al DDE Triplet: whereas the norm al DDE Triplet opens a link to a specific
cell-range in an Excel workbook/worksheet, the two-level triplet will allow SAS to send system -level
com m ands to the server application (which in our discussion is Excel).

IMPORTANT SYSTEM OPTIONS

NOXSYNC

The NOXSYNC instructs SAS to continue processing as soon as the operating system com m and is issued.

NOXW AIT

The NOXW AIT instructs SAS to autom atically close the spawned prom pt window after the specified com m and

2
has com pleted. W ithout this option, the com m and prom pt window just won’t close.

IMPORTANT FILENAME/INFILE OPTIONS

NOTAB

The NOTAB option instructs SAS to ignore tab characters between variables. SAS expects to see a tab
character placed between each variable that is sent across the DDE link when im porting into SAS. Sim ilarly,
SAS places a tab character between variables when SAS sends data across the DDE link when exporting
from SAS. W hen the NOTAB option is used, SAS accepts character delim iters other than tabs between
variables and the NOTAB option tells SAS not to convert tabs sent from the Excel worksheet into blanks.
Therefore, the tab character can be used as the delim iter between data values.

DLM

The DLM= option specifies the delim iter character, and '09'x is the hexadecim al representation of the tab
character.

MISSOVER

The MISSOVER option prevents SAS from going to a new input line if it does not find values in the current
line for all the INPUT statem ent variables, i.e. one or m ore cells are blank. W hen the MISSOVER option is
used, if SAS encounters a blank cell then SAS sets the input value to m issing, and continues to read until the
expected end of the input line.

DSD

The DSD option specifies that two consecutive delim iters represent a m issing value. The default delim iter is
a com m a.

USING DDE TO READ EXCEL DATA


Okay, we are ready to put som e code together to read Excel data into a SAS DATA step. I’m excited. The first
step is to launch (invoke) Excel.

INVOKE MICROSOFT EXCEL

You can invoke Excel using the SAS X com m and.

options noxwait noxsync;


x '"c:\program files\microsoft office\office11\excel.exe"';

The path for Excel will differ depending on the version you are using and whether or not you installed Excel
using the default destination. Searching the hard drive for “excel.exe” will pinpoint the proper location for the
path.

The entire x com m and m ust be enclosed in single quotes. If the path contains blanks, then the entire path
m ust be enclosed in double quotes. You use the NOXW AIT and NOXSYNC options to ensure that the x
com m and executes independently from the SAS session and the x com m and window closes. Obviously, if
you enter the path or executable incorrectly, Excel will fail to launch.

SAS m ust wait until Excel has com pleted loading itself before it can begin to exchange data. You cause SAS
to wait for Excel to load by using a DATA _NULL_ step with the SLEEP function. How long does SAS need
to sleep? That will depend on your com puter - trial and error will help to determ ine the tim e value to use in the
SLEEP function. A value larger than necessary will sim ply m ean SAS will pause longer than necessary, which
is not critical.

3
data _null_;
x=sleep(5);
run;

If you try to proceed with your DDE application before Excel finishes loading, you will get an error m essage
in the SAS log that looks like the following:

ERROR: Physical file does not exist, excel|system.


NOTE: The SAS System stopped processing this step because of errors.

Once Excel is running we can define and then open our Excel workbook and worksheet.

DEFINE AND OPEN A MICROSOFT EXCEL FILE

You are now ready to tell SAS to open an existing Excel workbook. You start by creating a special fileref that
will allow the DATA step to com m unicate with Excel via DDE. This is that two-level DDE Triplet.

filename ddecmd dde 'excel|system';

Here you define ddecm d as a fileref that signifies “DDE Com m and” (of course, the fileref can be any literal
you want) and DDE is the SAS device-type keyword that tells SAS you want to use DDE.

Next, you use a DATA _NULL_ step to pass to Excel via DDE the Excel m acro com m and to open the desired
Excel workbook. You link to the previous FILENAME statem ent using a FILE statem ent that references the
previously defined fileref, ddecm d. Then, you issue a PUT statem ent to pass to the two-level DDE Triplet a
string containing the Excel m acro com m and FILE-OPEN and the literal drive\path\filenam e of the desired
Excel workbook.

data _null_;
file ddecmd;
put '[FILE-OPEN("c:\wuss\names workbook.xls")]';
run;

Note that the entire PUT string is in single quotes and the entire FILE-OPEN m acro statem ent is enclosed in
brackets [ ]. This will also work using the Excel m acro “OPEN”, as seen below.

data _null_;
file ddecmd;
put '[OPEN("c:\wuss\names workbook.xls")]';
run;

Running this DATA _NULL_ step will cause Excel to open the desired workbook. The results in the SAS log
will look som ething like the following:

NOTE: The file DDECMD is:


DDE Session,
SESSION=excel|system,RECFM=V,LRECL=256
NOTE: 1 record was written to the file DDECMD.
The minimum record length was 41.
The maximum record length was 41.

If you enter the path or filenam e incorrectly, Excel will fail to open your workbook. You will get an error
m essage in the SAS log that looks like the following:

NOTE: The file DDECMD is:


DDE Session,
SESSION=excel|system,RECFM=V,LRECL=256
ERROR: DDE session not ready.

4
FATAL: Unrecoverable I/O error detected in the execution of the data step program.
Aborted during the EXECUTION phase.
NOTE: 0 records were written to the file DDECMD.
NOTE: The SAS System stopped processing this step because of errors.

Another unfortunate m istake you can m ake is to try to open your Excel workbook without first launching Excel.
If you try that, you will see an error m essage in the SAS log that looks som ething like the following:

ERROR: Physical file does not exist, excel|system.


NOTE: The SAS System stopped processing this step because of errors.

If you try opening your Excel workbook with Excel successfully launched but without first establishing the DDE
link with the FILENAME statem ent as shown above, then the SAS log won’t reflect an error, but the Excel
workbook won’t open.

Paul Choate suggests an alternate m ethod for launching Excel and opening the desired Excel workbook:
sim ply use %sysexec to launch the desired Excel workbook.

%sysexec "c:\wuss\names workbook.xls";

As long as the “xls” extension is registered in MS-W indows to Excel, then MS-W indows will launch Excel and
open the desired Excel workbook.

You are now ready to define a SAS fileref to the range of cells within the Excel workbook and worksheet you
wish to read data from .

READ THE MICROSOFT EXCEL FILE INTO SAS

Let’s see what your Excel workbook/worksheet looks like.

5
Now that you have established an open DDE link to your open Excel workbook, you are ready to tell SAS just
where within that workbook you want to read data. You do this with a FILENAME statem ent that uses the DDE
Triplet.

filename xlin DDE "excel|c:\wuss\[names workbook.xls]names worksheet!r2c1:r65536c6";


run;

Here you define xlin as a fileref that sim ply indicates “Excel In” (of course, the fileref can be any literal you
want) and DDE is the SAS device-type keyword that tells SAS you want to use DDE. This statem ent includes
the full DDE Triplet that defines Excel as the application-nam e, the “c:\wuss\nam es workbook.xls”, “nam es
worksheet” workbook/worksheet as the topic, and the range of cells r2c1:r65536c6 as the item. Notice that
of the full drive\path\filenam e, only the entire Excel workbook nam e is placed inside brackets [ ]. Notice the
separation of the three param eters using the vertical bar | and the exclam ation m ark ! . Notice also that the
item starts on row 2 to skip the colum n headings and ends with row 65536 (Excel’s m axim um num ber of rows
- how did they decide on that num ber?). I have found that SAS won’t create blank rows in the resulting SAS
file when I define m ore rows in the item that contain data. So, I always set the range to the m axim um . But,
I set the colum ns to just those that contain data. W hy? Because when I download fresh data the structure will
be the sam e, so I know the num ber of colum ns that will have data. But, the num ber of rows will vary each tim e
I re-download the table. Running this FILENAME statem ent does not cause SAS to write anything interesting
to the SAS log.

Next, you can use the data you just defined from your Excel workbook in a DATA step. Sim ply use you favorite
DATA step statem ents and options to create the perfect SAS data set from the Excel data. The only trick is
to use the fileref you created in the above FILENAME statem ent to reference the Excel worksheet via DDE.
There are som e im portant options that m ay be necessary, as we discussed earlier. Notice on the INFILE
statem ent that you m ay need to define the DLM=, NOTAB, MISSOVER, and DSD options to get SAS to read
your Excel workbook the way you want. Other than that, the rest of the DATA step is com posed of your
favorite statem ents and options (the advance of using DDE).

data wuss.employee(label='EMPLOYEE ROSTER' index=(EMP_ID));


infile xlin dlm='09'x notab missover dsd;
informat FIRST $20. LAST $20. DEPT $4. EMP_ID $6. TITLE $30. RATE 8.2;
input FIRST LAST DEPT EMP_ID TITLE RATE;
format FIRST $20. LAST $20. DEPT $4. EMP_ID $6. TITLE $30. RATE 8.2;
label FIRST = 'EMPLOYEE FIRST NAME'
LAST = 'EMPLOYEE LAST NAME'
DEPT = 'DEPARTMENT'
EMP_ID = 'EMPLOYEE ID'
TITLE = 'EMPLOYEE TITLE'
RATE = 'PAY RATE';
run;

After running the DATA step, SAS will write to the SAS log som ething like the following:

NOTE: The infile XLIN is:


DDE Session,
SESSION=excel|c:\wuss\[names workbook.xls]names worksheet!r2c1:r65536c6,
RECFM=V,LRECL=256

NOTE: 794 records were read from the infile XLIN.


The minimum record length was 23.
The maximum record length was 58.
NOTE: The data set WUSS.EMPLOYEE has 794 observations and 6 variables.

If you m istype the drive\path\filenam e\worksheet nam e in the FILENAME statem ent, or if you don’t open the
workbook in Excel first, SAS will let you know with the following error in the SAS log:

6
ERROR: Physical file does not exist,
excel|c:\wuss\[names workbook1.xls]names worksheet!r2c1:r65536c6.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WUSS.EMPLOYEE may be incomplete. When this step was stopped there were 0
observations and 6 variables.
WARNING: Data set WUSS.EMPLOYEE was not replaced because this step was stopped.

CLOSE THE EXCEL FILE

Lastly, you m ay want to close the Excel workbook using the sam e m ethod you used to open it, but with the
FILE-CLOSE Excel m acro. The DATA _NULL_ step below has an added bonus: the QUIT Excel m acro will
close Excel.

data _null_;
file xlin;
put '[FILE-CLOSE("c:\wuss\names workbook.xls")]';
put ‘[QUIT()]’;
run;

THE COM PLETE PROGRAM


Here’s the com plete code, suitable to copying and pasting to your favorite SAS session.

*;
/* set options and invoke Excel using DDE */
*;
options noxwait noxsync;
x '"c:\program files\microsoft office\office11\excel.exe"';
data _null_;
x=sleep(5);
run;
*;
/* open Excel workbook */
*;
filename ddecmd dde 'excel|system';
data _null_;
file ddecmd;
put '[FILE-OPEN("c:\wuss\names workbook.xls")]';
run;
*;
/* specify desired Excel worksheet cell range */
*;
filename xlin DDE "excel|c:\wuss\[names workbook.xls]names worksheet!r2c1:r65536c6";
run;
*;
/* read Excel files using DDE into SAS data set*/
*;
data wuss.employee(label='EMPLOYEE ROSTER' index=(EMP_ID));
infile xlin dlm='09'x notab missover dsd;
informat FIRST $20. LAST $20. DEPT $4. EMP_ID $6. TITLE $30. RATE 8.2;
input FIRST LAST DEPT EMP_ID TITLE RATE;
format FIRST $20. LAST $20. DEPT $4. EMP_ID $6. TITLE $30. RATE 8.2;
label FIRST = 'EMPLOYEE FIRST NAME'
LAST = 'EMPLOYEE LAST NAME'
DEPT = 'DEPARTMENT'
EMP_ID = 'EMPLOYEE ID'
TITLE = 'EMPLOYEE TITLE'
RATE = 'PAY RATE';
run;
*;
/* close Excel workbook and close Excel */
*;

7
data _null_;
file xlin;
put '[FILE-CLOSE("c:\wuss\names workbook.xls")]';
put ‘[QUIT()]’;
run;

CONCLUSION
Don’t let anyone tell you DDE is dead. It continues to do its job reading and writing data between MS-W indows
applications. It is especially useful because we can use all the power of the SAS DATA step with DDE, and
DDE requires we only have BASE SAS.

REFERENCES
SAS Institute Inc. SAS Companion For W indows. Cary, NC: SAS.

SAS Institute Inc. SAS Institute Technical Support docum ent TS325.

Christopher A. Roper, “Intelligently Launching Microsoft Excel from SAS, Using SCL Functions Ported to Base
SAS.” Proceedings of the Twenty-Fifth Annual SAS Users Group International Conference.

RHA (Minisystem s) Ltd. “Dynam ic Data Exchange (DDE) and NetDDE FAQ.”
http://www.angelfire.com /biz/rham inisys/ddeinfo.htm l#DDEDownload

UCLA Academ ic Technology Services “SAS Library, Reading Data into SAS.”
http://www.ats.ucla.edu/stat/sas/library/SASRead_os.htm

Koen Vyverm an “Using Dynam ic Data Exchange to Export Your SAS® Data to MS Excel — Against All ODS,
Part I.” Proceedings of the Twenty-Seventh Annual SAS Users Group International Conference.

Perry W atts “Using Single-Purpose SAS® Macros to Form at EXCEL Spreadsheets with DDE.” Proceedings
of the Thirtieth Annual SAS Users Group International Conference.

SAS-L Posting, Paul Choate

ACKNOW LEDGM ENTS


I appreciate all the fine technical support and advice from the sources listed above as well as the m any fine
posts on SAS-L.

CONTACT INFORM ATION


Your com m ents and questions are valued and encouraged. Contact the author at:
Curtis A,. Sm ith
P.O. Box 20044
Fountain Valley, CA 92728-0044
Fax: 413-383-6395
Em ail: casm ith@ m indspring.com

SAS and all other SAS Institute Inc. product or service nam es are registered tradem arks or tradem arks of
SAS Institute Inc. in the USA and other countries. ® indicates USA registration.

Other brand and product nam es are tradem arks of their respective com panies.

You might also like