BookmarkSubscribeRSS Feed
statistician13
Quartz | Level 8

I am trying to create Excel output with ODS Excel and gain control over the sheet names.  The difficulty I'm encountering is that I have a freq procedure (actually the surveyfreq procedure) with multiple variables on the tables statement.  This produces one sheet per variable (as I'd hope), but I can't seem to figure out how to control the name of each tab in this case since both tabs are created with the same procedure and I can't use a byvariable as others have suggested using with proc report.

 

Here's same sample code:

ods excel file="D:\Output1.xls" style=printer;
title "Survey Results";
ods excel options(sheet_name="q0001");
proc surveyfreq data=surveydata missing nosummary;
stratum stratum;
weight FinalWt;
tables cohort*(q0001 q0002)/cl nostd;
run;
quit;
ods excel close;


As you can see from the tables statement, the output creates two sheets:  One for cohort*q0001 and the other for cohort*q0002.  Unfortunately, this produces an Excel file with two Sheets named "q0001" and "q0001 2."  I'm trying to name the names "q0001" and "q0002" corresponding to the names of the variables.  I realize I can do this by breaking out the tables statement into two separate proc freq calls preceded with the ods excel options(sheet_name=questionname) statement, but I'd like to avoid this if possible.

Any suggestions on how to do this?

Thanks. 

 

2 REPLIES 2
DWilson
Pyrite | Level 9

@statistician13 wrote:

I am trying to create Excel output with ODS Excel and gain control over the sheet names.  The difficulty I'm encountering is that I have a freq procedure (actually the surveyfreq procedure) with multiple variables on the tables statement.  This produces one sheet per variable (as I'd hope), but I can't seem to figure out how to control the name of each tab in this case since both tabs are created with the same procedure and I can't use a byvariable as others have suggested using with proc report.

 

Here's same sample code:

ods excel file="D:\Output1.xls" style=printer;
title "Survey Results";
ods excel options(sheet_name="q0001");
proc surveyfreq data=surveydata missing nosummary;
stratum stratum;
weight FinalWt;
tables cohort*(q0001 q0002)/cl nostd;
run;
quit;
ods excel close;


As you can see from the tables statement, the output creates two sheets:  One for cohort*q0001 and the other for cohort*q0002.  Unfortunately, this produces an Excel file with two Sheets named "q0001" and "q0001 2."  I'm trying to name the names "q0001" and "q0002" corresponding to the names of the variables.  I realize I can do this by breaking out the tables statement into two separate proc freq calls preceded with the ods excel options(sheet_name=questionname) statement, but I'd like to avoid this if possible.

Any suggestions on how to do this?

Thanks. 

 


 

Proc surveyfreq creates two separate internal data sets ; one for each table. You can see this if you do:

ods trace on;

before proc surveyfreq;

ods trace off;

 

You can use ODS to grab those data sets and print each one seprately afer your survyefreq procedure. This way you can use sheet_name to name each tab in your excel file.

 

 

This may not be what you want because it would be equivalent to running two different proc surveyfreqs.

 

Dave

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would advise to split the program logic out.  Something like three distinct steps
Get data

Process data

Report data

 

So in your example, I would process the survey data such that you get a dataset out from it:

ods output <object>=workingdata;
proc surveyfreq data=surveydata missing nosummary;
stratum stratum;
weight FinalWt;
tables cohort*(q0001 q0002)/cl nostd;
run;

In the above you would need to get the object name from an ods trace on; statement which will show you all the objects which are created (ods trace off; after).  The objects can all be saved and processed further - this is a big plus to the process as you can format and manipulate as you want.

Then when you have the data all nicely processed, then open the Excel ods and report the data. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 2288 views
  • 0 likes
  • 3 in conversation