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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1747 views
  • 0 likes
  • 3 in conversation