Link to home
Start Free TrialLog in
Avatar of Lee R Liddick Jr
Lee R Liddick JrFlag for United States of America

asked on

How do I create one XLSX file from multiple datasets

I'm stumbling here.  I have a SAS program that creates a final dataset and I use an ods excel proc report that creates a formatted spreadsheet.  I also have 11 other sas datasets under a libname in which I need to get added to this single excel file as well.  Now I understand the limitations of ods excel not being able to create separate sheets easily into one file.  

What I'm struggling with is how to do this, whether I need to loop through the historical datasets in one single ods statement that creates these datasets on separate tabs or what.

/* so my libname statement is */
libname sasdata "mypath" access=readonly';

/* Then I have my macro at the end to create the excel file: */
%macro createReport(utildt);

options missing=' ';
ODS LISTING CLOSE;

ODS PATH (prepend) STD.template99(READ) SASHELP.TEMPLMST(READ);
ODS ESCAPECHAR='^';

TITLE;

ods excel file = " &rptpath./&filenm..xlsx" style=stdXLSX

/* CRITERIA PAGE - first sheet; specifies criteria for report */

options (orientation='portrait'
                   sheet_name=Criteria"
                           );

proc report data=CritReport no windwos headline headskip spacing = 2 missing split='|';

column description;

define description /display "REPORT DESCRIPTION;

run;

/* REPORT SHEET - second sheet; actual report data in a formatted sheet */

TITLE;

ods excel style =stdXLSX
options (orientation='portrait'
                   sheet_name=&utildt."
                    );
proc report data=FINAL_&utildt. nowd headline headskip split='|';
columns
name id field3 field4 field5;
define name / "Name";
define id / "ID";
define field3 / "Field Three";
define field4 / "Field Four";
define field5 / "Field Five";
run;

ods Excel close
ods _all_close;
run;

%mend createReport;

%createReport(&rptdate);

Open in new window

Avatar of Sam Malenfant
Sam Malenfant
Flag of Canada image

A fair amount of automation is possible with an Excel macro file.  It could pull 11 files into one, with either a tab for each, or append the data sets from each into one master. If the 11 files follow a naming convention, then it really just takes one parameter and a minute to run.


A manual merging would be a good dry run, and you might even just use a "Record Macro" to pull together most of the code.


Another odd option, if you want it totally command driven, could be to output to a CSV file instead and use an MS DOS batch process to concatenate the files (stipping the header out after the first one).


Thoughts ...

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.