Use the SHEET=
statement in a Proc EXPORT
step.
For example:
filename myxl 'c:\temp\sandbox.xlsx';
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name)
;
sheet='Name';
run;
proc export replace file=myxl dbms=excel
data=sashelp.class (keep=name age weight)
;
sheet='Weight';
run;
A macro can be coded to generate repetitive parts
%macro excel_push (file=, data=, always=, each=);
%local i n var;
%let n = %sysfunc(countw(&each));
%do i = 1 %to &n;
%let var = %scan(&each,&i);
proc export replace file=&file dbms=excel
data=&data(keep=&always &var)
;
sheet="&var";
run;
%end;
%mend;
options mprint;
filename myxl2 'c:\temp\sandbox2.xlsx';
%excel_push (
file=myxl2,
data=sashelp.class,
always=name age sex,
each=height weight
)
If you open the Excel output, leave it open and rerun the code, you will get an error, albeit slightly obscure:
ERROR: Error attempting to CREATE a DBMS table. ERROR: Execute: The Microsoft Access database
engine could not find the object ********. Make sure the object exists and that you spell
its name and the path name correctly. If ******** is not a local object, check your
network connection or contact the server administrator..