DATA Step, Macro, Functions and more

Macro output multiple data to multiple tabs into one excel.

Reply
Contributor
Posts: 61

Macro output multiple data to multiple tabs into one excel.

Hi, All,

Is there a way that we can append results from macro runs to multiple tabs in one single excel file?

Take an example like below, each time the macro run, new output will replace the old. However, I want both results to be present in one excel file (picture show).

I also tried ODS TAGSETS, it doesn't seem to work either.  Please offer opinions. Thanks. Joe

data have1;

sex='M';run;

data have2;

sex='F';run;

%macro test (data, var);

proc export

data=&data (where=(sex="&var"))

outfile="/SASData/SASUSER/Range/why2.xlsx"

dbms=xlsx replace;

sheet="&var";

run;

%mend test;

%test(have1, M)

%test(have2, F)

bothinexcel.JPG

Super User
Posts: 19,770

Re: Macro output multiple data to multiple tabs into one excel.

Try DBMS=ExcelCS

You can do it using XLSX if you have SAS 9.4 or applied a hot fix to SAS 9.3

Contributor
Posts: 61

Re: Macro output multiple data to multiple tabs into one excel.

What kind of hotfix?

Super User
Posts: 19,770

Re: Macro output multiple data to multiple tabs into one excel.

Hot Fixes for SAS/ACCESS Interface to PC Files 9.3_M1

The XLSX engine is enhanced to write multiple sheets per Microsoft Excel file in the first maintenance release of SAS® 9.3 (TS1M1)


I don't know if you have 9.3TS1M2 if this would be fixed or not.


At any rate, tagsets.excelxp will definitely work, but it's a different structure for the code.


Untested, but it would be something like the following:



data have1;

sex='M';run;

data have2;

sex='F';run;

%macro test (data, var);

options tagsets.excelxp(sheet_name="&var");

proc print data=&data (where=(sex="&var"));

run;

%mend test;

ods tagsets.excelxp file="C:\temp\out.xlsx" style=journal;

%test(have1, M)

%test(have2, F)

ods tagsets.excelxp close;

Contributor
Posts: 61

Re: Macro output multiple data to multiple tabs into one excel.

Thanks, Reeza,

I actually read your old post on the same issue:

I am running SAS EG on server, I don't have install privilege either.


The ' DBMS=ExcelCS' doesn't seem to work.

Putting macros in ods tagsets isn't promising either, after copying pasting the code and several modification tries.

It may has something to do with my connection to the server, cause I got error msg like 'ERROR: Server Name is invalid or missing.'. I will surely try them on my local SAS later today.

Thanks again.

Ask a Question
Discussion stats
  • 4 replies
  • 494 views
  • 0 likes
  • 2 in conversation