BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I have two datasets with the same variables and the same formats. How to export them into one excel file with different sheets.  Is there any simple way, or do I have to write ODS EXCEL ( or TAGSETS) with PORC REPORT for each dataset? Thank you for your input.

 

In the following example, is there any way I can create a macro and loop the required datasets?  The following is simple  export

 data class1;
 set sashelp.class;
 run;

 data class2;
 set sashelp.class;
 run;


PROC EXPORT DATA= WORK.class1
            OUTFILE= "D:\Desktop\classes.xls" 
            DBMS=EXCEL REPLACE;
     SHEET="Class1"; /* for class2 I have to run it again*/
     NEWFILE=YES;
RUN;

This is how I want two sheets in one excel file

SASuserlot_0-1679874353546.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

It's a lot easier if you use the XLSX LIBNAME:

data class1;
 set sashelp.class;
 run;

 data class2;
 set sashelp.class;
 run;

 libname CLASS XLSX "<MyDir>\Class.xlsx";

 proc datasets library = CLASS;
   copy in = WORK  out = CLASS;
  select class1 class2;
  run;
quit;

libname CLASS clear;

No loop needed.

View solution in original post

2 REPLIES 2
SASKiwi
PROC Star

It's a lot easier if you use the XLSX LIBNAME:

data class1;
 set sashelp.class;
 run;

 data class2;
 set sashelp.class;
 run;

 libname CLASS XLSX "<MyDir>\Class.xlsx";

 proc datasets library = CLASS;
   copy in = WORK  out = CLASS;
  select class1 class2;
  run;
quit;

libname CLASS clear;

No loop needed.

SASuserlot
Barite | Level 11

Thank you very much. It served my purpose.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2364 views
  • 1 like
  • 2 in conversation