7
Page 1 of 7 Using DDE to connect SAS® results with MS-Excel® Rebecca Symes, Maine Health Information Center, Manchester, Maine INTRODUCTION SAS® is clearly superior software for gathering and processing large amounts of data. However, most clients have skills in other areas and no inclination to learn SAS. They want output in a format they consider to be more user-friendly, and the tool of choice for many is Microsoft Excel®. PROC EXPORT will output SAS data to an Excel file, and ods is the new sophisticated cousin. However, the flexibility of a DDE link in a standard DATA step allows SAS results to be placed precisely into a specific location in the Excel file. Formulas, formatting, even graphs are untouched, yet the data can be updated in seconds. In addition to being useful, it is also loads of fun to watch – as close to magic as one can get inside a cubicle! DDE DDE stands for Dynamic Data Exchange, and is a standard utility for translating from one Windows application to another. DDE is often used for reading in data from an application, such as Excel, for further processing within the SAS program. The DDE link presented in this paper is going out from SAS to Excel. The DDE link is built as part of a FILENAME statement which includes the specific row and column location for output. Being a simple utility, DDE output must sometimes be managed within SAS to ensure maximum value. THE BASICS The core of DDE output is in a FILENAME statement and a FILE, PUT within a DATA step. The FILENAME defines the output location and PUT selects data fields to be output. filename outxls dde ‘EXCEL|loading!r6c1:r8c3’; where outxls is the nickname dde instructs the link to use DDE to exchange data EXCEL is the Windows application object loading is the page name in Excel (the default name would be Sheetn, but I have found renaming it for clarity to be a useful step) r6c1:r8c3 – Exact row and column numbers where the data will be loaded. Posters NESUG 18

Using DDE to connect SAS® results with MS-Excel® · Figure 3. shows the series used to start Excel, close the default workbook, and open the workbook where SAS will be loading data

  • Upload
    hatu

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1 of 7

Using DDE to connect SAS® results with MS-Excel®Rebecca Symes, Maine Health Information Center, Manchester, Maine

INTRODUCTION

SAS® is clearly superior software for gathering and processing large amounts of data. However, mostclients have skills in other areas and no inclination to learn SAS. They want output in a format theyconsider to be more user-friendly, and the tool of choice for many is Microsoft Excel®.

PROC EXPORT will output SAS data to an Excel file, and ods is the new sophisticated cousin.However, the flexibility of a DDE link in a standard DATA step allows SAS results to be placed preciselyinto a specific location in the Excel file. Formulas, formatting, even graphs are untouched, yet the datacan be updated in seconds. In addition to being useful, it is also loads of fun to watch – as close tomagic as one can get inside a cubicle!

DDE

DDE stands for Dynamic Data Exchange, and is a standard utility for translating from one Windowsapplication to another. DDE is often used for reading in data from an application, such as Excel, forfurther processing within the SAS program.

The DDE link presented in this paper is going out from SAS to Excel. The DDE link is built as part of aFILENAME statement which includes the specific row and column location for output. Being a simpleutility, DDE output must sometimes be managed within SAS to ensure maximum value.

THE BASICS

The core of DDE output is in a FILENAME statement and a FILE, PUT within a DATA step. TheFILENAME defines the output location and PUT selects data fields to be output.

filename outxls dde ‘EXCEL|loading!r6c1:r8c3’;

where outxls is the nickname dde instructs the link to use DDE to exchange data

EXCEL is the Windows application objectloading is the page name in Excel (the default name would be Sheetn, but I have found

renaming it for clarity to be a useful step)r6c1:r8c3 – Exact row and column numbers where the data will be loaded.

PostersNESUG 18

Page 2 of 7

The complete command set would look like the following:

Figure 1.

Figure 2.

Figure 2.

In Excel, the green section in Figure 2. shows where the filename statement will put raw data. Row andcolumn locations can be larger than needed, in case you’re not sure how many rows will be exported.If fewer rows and colums than needed are defined in the Filename statement, the extra data will not beoutput. For example, if you have eight columns of data and the output is set up as r1c1:r6c7, the eighthfield will not be output to Excel.

Defining your output area with more rows or columns than needed is fine, as long as there isn’tsomething else in that spot in your Excel loading area. Looking at Figure 1., if the row/column

filename bigout dde ‘EXCEL|loading!r6c1:r8c3’;

data _null_; /* no need to create a new dataset */set bigfile;file bigout;

put yearmth req paid;run;

PostersNESUG 18

Page 3 of 7

instruction were set as r6:c1:r20c3, everything in the Excel file from cell A9 through C20 would beoverwritten with blanks. This is another reason to have your graphs and formatted print areas on aseparated tab from where the data is being loaded.

OPENING THE EXCEL FILE

Over many years of using this DDE link, I have found it easiest to open the correct Excel file beforerunning the SAS File and Put code. However, it is possible to have SAS open the Excel file for you, fora completely automated process. The X command will activate the process for Windows to open Excel.

Figure 3.

Figure 3. shows the series used to start Excel, close the default workbook, and open the workbookwhere SAS will be loading data in a later step. The path may need to be modified for the user’ssystem. The sleep command allows a pause while Excel.exe is being executed.

THE QUIRKS

BUT WHAT IF….LABELS PARSE OUT ACROSS COLUMNS IN EXCEL?

File and Put work well with numeric data and text without imbedded blanks. However, the purpose ofDDE is to exchange data from a dataset into precise columns in Excel. If Excel meets a space, itassumes “new column.”

To prevent text from parsing, just add the NOTAB option to the DDE filename statement.

filename bigout dde ‘EXCEL|loading!r5c1:r50c1’ notab;

Each field using the NOTAB option needs it’s own FILENAME statement, otherwise it will put all yourdata into a single field. Use a mix of NOTAB and regular FILENAME statements to place data exactlywhere you need it.

options noxwait noxsync;

x “c:\program files\microsoft office\office\excel.exe”;

data _null_;rc = sleep(15);

run;

filename ddeopen DDE “Excel|system”;data _null_;

file ddeopen;put ‘[close(“Book1”)]’;put ‘[open(C:\sas\sample_pgms\sample”)]’;

run;

PostersNESUG 18

Page 4 of 7

Figure 4.

Note that the filename statement with NOTAB is only a single column wide. The second filenamestatement starts in the second column and places the rest of the data. You can use as many FILE andPUT statements as needed to output the data.

Figure 5.

filename bigname dde ‘EXCEL|loading!r37c3:r39c3’ notab;filename bigout dde ‘EXCEL|loading!r37c1:r39c2’;

data _null_;set bigfile;file bigname;

put hospname;file bigout;

put vendor paid;run;

PostersNESUG 18

Page 5 of 7

BUT WHAT IF….YOUR ROW COUNT IS DIFFERENT NEXT TIME YOU OUTPUT YOUR DATA?

This is where DDE and SAS join forces to make your life easier. One of the biggest advantages toExcel is that it will perform the same calculations or graphics analysis over and over on new datasupplied to a standard “loading” area. One of the biggest disadvantages of Excel is that it expects thedata to be in exactly the same place every time.

For example, you are creating a report by area and group. Most areas have 5 groups, but some onlyhave 3 or 4. To make Excel happy, SAS needs to modify your dataset so that every area looks likeevery other area, and all of them have 5 groups, even though some have dummy data. One way to dothis is to create a dataset with all the correct rows, and force your data to match.

Figure 6.

RESULTS

After running the SAS code with your DDE link, File and Put commands, data is placed in Excel in therows and columns where Excel can use it for standard displays. Figures 7. and 8. show the automatedresults which can be repeated many times with no reformatting. Again, usually the loading andgraphics areas would be separate tabs, but for clarity they are presented together here.

data looper (keep=group);do I=1 TO 5;

group=I; output;

end;

data _null_;array{8} var1-var8;merge bigfile(in=A) looper(in=B);

by group; if B and not A then do I=1 to 8; var(I)=0;

end;

file bigout;put area group var1-var8;

run;

PostersNESUG 18

Page 6 of 7

Figure 7.

Figure 8.

PostersNESUG 18

Page 7 of 7

IN EXCEL

The purpose of the SAS/Excel link is place intelligent SAS results into Excel for graphs, beautification,and sending to clients who always want to “play” with the data. Part of the magic is on the Excel side ofthe connection. There are a few basic hints which make the handoff easier.

1. Place your SAS results into a separate tab in the Excel workbook. I call mine “Loading” just toremind me why it’s there. Row and column headers, shading, borders, and especially graphs, canbe applied by using a formula to link from another Sheet to the Loading tab. Data can becontinually refreshed without disturbing the pretty part.

2. When sending formatted chart data to a client, use Copy> Save As> Values to remove the formulaslinking to the real data, then delete the Loading tab and save as a different filename. No point ingiving away your secrets. Graphs, however, need to keep their source data in the file.

3. Even when using this automated link, Excel sometimes has a hard time holding it’s formatting whengraphing new data. Check graphs for fonts and sizing before sending out.

THE BOTTOM LINE

Sometimes the most simple tools are the most effective. Excel spreadsheets are easy to maintain andshare. SAS is the superior tool to process data and summarize results. Using the simple DDE linkallows SAS power to populate a user-friendly spreadsheet, and update the results as needed. Withlarger SAS processes, there is sufficient time between starting the SAS run and data being loaded intothe Excel file. That time break allows the user to switch windows to Excel, and watch the data“magically” appear as each DDE link is executed. The DDE link is a simple tool, easy to execute,creates great results, and is fun to watch.

REFERENCES

SAS Help and Documentation, DDE (Dynamic Data Exchange), writing to Excel.

ACKNOWLEDGMENTS

SAS is a Registered Trademark of the SAS Institute, Inc. of Cary, North Carolina

Microsoft Excel is a Registered Trademark of the Microsoft Corporation.

CONTACT INFORMATION

Rebecca SymesMaine Health Information Center16 Association DriveP O Box 360Manchester, Maine 04351Work Phone: (207) 623-2555 x-134Fax: (207) 622-7086Email: [email protected]

PostersNESUG 18