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

HI.  I am trying to use a combination of PROC EXPORT and PROC REPORT. 

I have 1 SAS dataset and I'd like to export that to Excel into multiple worksheets based on customer ID  (one worksheet per customer).  I also want to add the same title to each worksheet.  Seems I cannot add titles using PROC EXPORT and I cannot figure out how to breakout my dataset into multiple worksheets using PROC REPORT.

 

Here is the code for exporting to multiple worksheets:

 
proc sql;
 
  select distinct ID into :category_list separated by ' '
  from SUMMARY;
quit;
 
/* WORKS FOR OUTPUTTING TO SEPERATE WORKSHEETS */
 
%macro export_to_excel;
  %do i = 1 %to %sysfunc(countw(&category_list));
    %let current_category = %scan(&category_list, &i);
    
    proc export data=SUMMARY(where=(ID="&current_category"))
                outfile="c:\users\my_name\documents\test.xlsx"
                dbms=excel replace;
      sheet="&current_category";
 
    run;
  %end;
%mend;
 
Here is the code for exporting with titles:

 

/*WORKS FOR EXPORTING WITH FORMAT AND 3 TITLE ROWS*/
%let rundate = %SYSFUNC(today(),yymmddn8.);


/* export report */
ods escapechar="~";
ods excel file="c:\users\my_name\documents\test..xlsx" options(sheet_name="Sheet1");
ods text="~{style[color=green fontstyle=italic] BANKRUPTCY CASE NO.";
ods text="~{style[color=green fontstyle=italic] FILED";

proc report data=work.SUMMARY/*(obs=5) */spanrows

style(report)={pretext=" "};

run;
ods excel close;

 

How can I combine the two?  1 SAS dataset exported to 1 Excel workbook with 1 worksheet per customer with a title.  

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use BY statement and ODS EXCEL option 

SHEET_INTERVAL='BYGROUP'

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Use BY statement and ODS EXCEL option 

SHEET_INTERVAL='BYGROUP'

deblee73
Calcite | Level 5
Yea!!! It works. I will sleep tonight. Thank you.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1008 views
  • 0 likes
  • 2 in conversation