The SAS Output Delivery System and reporting techniques

Exporting To Excel - multiple worksheets

Reply
Regular Contributor
Posts: 164

Exporting To Excel - multiple worksheets

Hello

I'm not even sure this is the right community so I apologize if I'm incorrect.

I have a dataset that I import into SAS.  The data is quite large with one line of data being a single patient visit. 

What I would like to do is export the data to Excel but where one patient visit is on one Excel worksheet in the format I require.  For example, in cell A2 I would like the word "chart number" and then the actual chart number to be in cell B2.  Diagnoses that are currently in wide format to be exported in long format with Diagnosis 1 thru 20 showing in column A with the diagnosis prefix in column C, diagnosis in column D etc.

Is it possible to map in this fashion with only having Base SAS?  Thanks very much. 

PROC Star
Posts: 7,492

Exporting To Excel - multiple worksheets

First two disclaimers:  (1) I am not an expert with the hash object and (2) I am not sure that I correctly understand what you are trying to do.

The following use the libname method, combined with the excel engine, to create a libname called "art" and an excel spreadsheet called "want".

Then, for some example data, it creates a file called "have" by taking sashelp.class, and renaming age, height and weight to be named diagnosis1, diagnosis2 and diagnosis3, respectively.

Then, it transposes the file creating a file called "long", that has the fields name, chart_number and diagnosis (there are 3 records for each patient).

Presuming that the result of long approximates the file you want to end up with, the code then uses a datastep to do two things.   First, since I didn't know how else to indicate a two-level within a hash object, I created a new variable called oname that was a concatenation of the string 'art.' and the patients' names.  Second, it indexes the records according to oname.

Finally, the code uses a hash to create separate worksheets for each patient, within the workbook

"c:\art\want.xls", with each worksheet containing three lines, namely one for each diagnosis.

Hopefully, it at least approximates what you are trying to accomplish:

libname art excel "c:\art\want.xls";

data have;

  set sashelp.class (drop=sex

     rename=(

     age=diagnosis1

     height=diagnosis2

     weight=diagnosis3

     ));

run;

proc transpose data=have

  out=long (rename=(

    _name_=chart_number

    col1=diagnosis

    ));

  by name;

run;

data long (index = (oname));

  set long;

  oname='art.'||name;

run;

data _null_ ;

  dcl hash hh   (             ) ;

  hh.definekey  ('k'          ) ;

  hh.definedata ('name', 'chart_number', 'diagnosis') ;

  hh.definedone () ;

  do k = 1 by 1 until ( last.oname ) ;

    set long;

    by oname ;

    hh.add () ;

  end ;

  hh.output (dataset: oname) ;

run ;

libname art clear;

Super User
Posts: 19,877

Exporting To Excel - multiple worksheets

Not sure you can use the libname engine with only BASE SAS. I thought you still needed some form fo SAS/ACCESS or CONNECT (never sure which) to talk to excel directly.

But then not sure what licenses you have.

Another way is to get your data in the format you'd like to print it and then use the tagsets.excelxp to print your output to an excel workbook and control the spreadsheets.

Basically, yes you can, but it won't be a one step procedure.

You may want to look at the following site and see if Roland already has something that would work for what you're trying to do.

http://www.datasavantconsulting.com/roland/Spectre/

Super User
Posts: 3,260

Exporting To Excel - multiple worksheets

Reeza is correct. You would need SAS/ACCESS to PC FILE FORMATS to use the EXCEL LIBNAME option.

If you only have Base SAS then there are at least three other options for exporting to Excel I am aware of:

1) DDE - very fiddly and requires access to the SAS X command and does not work on remote servers which cuts out using it with EG.

2) PROC EXPORT to CSV - easy but only does one worksheet at a time.

3) ODS EXCELXP tagset - very flexible and powerful but not efficient if you have large data volumes.

Regular Contributor
Posts: 164

Exporting To Excel - multiple worksheets

Hi

Sorry but I didn't see the responses because I didn't realize I wouldn't be notified by e-mail when someone responded!

How would I know if I have SAS/ACCESS?  For instance, I can currently export using Proc Export and I can also import/export within SAS.

I will try Art's response and see what happens (thanks Art!) but also consider Reeza and SASKiwi's comments when trying to get this to work.

Thanks again for all responses!

Super User
Posts: 3,260

Exporting To Excel - multiple worksheets

If you run the SETINIT procedure: PROC SETINIT; RUN; it will tell you if SAS/ACCESS to PC FILE FORMATS is licensed or not.

If you already use PROC EXPORT to Excel then you have this product licensed. That also means the LIBNAME EXCEL option will work.

Regular Contributor
Posts: 164

Exporting To Excel - multiple worksheets

Thanks SASKiwi!! 

I ran as you suggested and it indicated I had: 

Base SAS

SAS/STAT

SAS/GRAPH

SAS/ACCESS Interface to PC files

SAS/ACCESS Interface to ODBC

I also have EG.  Thanks very much....I'll let you know how this goes! 

PROC Star
Posts: 7,492

Exporting To Excel - multiple worksheets

Since you have SAS/ACCESS Interface to PC files, the code I suggested should work.

Ask a Question
Discussion stats
  • 7 replies
  • 806 views
  • 3 likes
  • 4 in conversation