<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858156#M339060</link>
    <description>&lt;P&gt;Probably the devil is in the details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Your PROC step is missing the RUN statement to end it, so it will stay open until you start some other step.&lt;/P&gt;
&lt;P&gt;2) Your macro does not take any parameters, so you cannot call it with parameters.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Feb 2023 23:07:51 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-02-09T23:07:51Z</dc:date>
    <item>
      <title>Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857586#M338854</link>
      <description>&lt;P&gt;hi all:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise this trivia &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;, it seems the output of excel sheet will be overwritten by the new proc report excel output.&lt;/P&gt;
&lt;P&gt;Goal: Need to output multiple sheets to One excel file.&lt;/P&gt;
&lt;P&gt;1. I need to first run multiple macros for different data sets (%mm) and then Output multiple final datasets (A,C) to one excel file- test.xlsx&lt;/P&gt;
&lt;P&gt;2. &amp;amp;&amp;amp;var&amp;amp;i and d_&amp;amp;&amp;amp;var&amp;amp;i&amp;nbsp; came from the %mm , which are used to identify difference for each variables between old and new version of the data set. (this &amp;amp;&amp;amp;var&amp;amp;i will resolve to different value based on %mm)&lt;/P&gt;
&lt;P&gt;3.%sqlobs is the total number of variables in each dataset (for example A: has 22 variables)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;purple&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%mm(A,  &amp;amp;_va1.,term,test);
/*-----------------------------------------------------------------------------------------------------*																																							Step6. Output
*-----------------------------------------------------------------------------------------------------*/	
title;
footnote;
ods results off;
ods listing close;
ods escapechar="^";

ods excel file="C:\Users\test.xlsx" 
		style=excel
		options 
    (row_repeat="header"
		 frozen_headers="ON" 
		 frozen_rowheaders="OFF" 
		 ROWBREAKS_INTERVAL= "OUTPUT"
		 sheet_label=" "
		 embedded_titles="YES"
		 FitToPage="ON"
		 orientation="landscape"
		 flow="ROWHEADERS");

%macro rpt;
%let sqlobs=22;
ods excel options (sheet_name = "A");
proc report data=A nowd split='~' missing style(header)=[just=left]
	style(report)=[font_size=9pt rules=all bordercolor=#f0f0f0 borderstyle=solid borderwidth=2pt frame=box] 
	style(column)=[font=(Arial, 9pt) bordercolor=#f0f0f0 borderstyle=solid borderwidth=2pt cellwidth=1in vjust=m] 
	style(header)=[font=(Arial, 9pt) bordercolor=#f0f0f0 borderstyle=solid borderwidth=1pt font_weight=bold foreground=white background=#ADD8E6 vjust=m];

  %IF &amp;amp;num. &amp;gt; 0 %THEN %DO; 
 	 column subject recno cat %do i=1 %to &amp;amp;sqlobs.; &amp;amp;&amp;amp;var&amp;amp;i  %end; %do i=1 %to &amp;amp;sqlobs.; d_&amp;amp;&amp;amp;var&amp;amp;i %end;  ;		
			  define subject / display "Subject ID"   							style(column)=[cellwidth=0.7in];
				define recno  /display order=data noprint;
				define cat    / display  "Category ";
                                define a     / display "	"						style(column) = [just = l cellwidth = 2.2in];
				define b    / display "" 								style(column) = [just = l cellwidth = .8in];
				define c    / display "";
        	%do i=1 %to &amp;amp;sqlobs. ; 
        define D_&amp;amp;&amp;amp;var&amp;amp;i / order=data noprint;
        %end;  ;						
		 %do i=1 %to &amp;amp;sqlobs; 	

			compute &amp;amp;&amp;amp;var&amp;amp;i;
					if cat="New Record" then do;
						call define("cat", "style", "style=[background=lightgreen]");
						call define("&amp;amp;&amp;amp;var&amp;amp;i", "style", "style=[background=lightgreen]");
					end;
					else if cat="Deleted Record" then do;
						call define("cat", "style", "style=[background=lightred]");
						call define("&amp;amp;&amp;amp;var&amp;amp;i", "style", "style=[background=lightred]"); 
					end; 
			 endcomp;

			 compute D_&amp;amp;&amp;amp;var&amp;amp;i;
					if D_&amp;amp;&amp;amp;var&amp;amp;i="Yes" then do;
						call define("cat", "style", "style=[background=yellow]");
						call define("&amp;amp;&amp;amp;var&amp;amp;i", "style", "style=[background=yellow]");
					end;
			 endcomp;
		%end;
	run;
	%END;
	%ELSE %DO;
			 column x;
			 define x / display "";
		 run;
	%END;

%mend;
%rpt;
ods excel close;

/*-----------------c-----------------*/
%mm(C,  &amp;amp;_va3.,term,test);			
title;
footnote;
ods results off;
ods listing close;
ods escapechar="^";

ods excel file="C:\Users\test.xlsx" 
		style=excel
		options 
    (row_repeat="header"
		 frozen_headers="ON" 
		 frozen_rowheaders="OFF" 
		 ROWBREAKS_INTERVAL= "OUTPUT"
		 sheet_label=" "
		 embedded_titles="YES"
		 FitToPage="ON"
		 orientation="landscape"
		 flow="ROWHEADERS");

%macro rpt;
%let sqlobs=27;
ods excel options(autofilter="ALL" sheet_name="C");
	proc report data=C nowd split="~" missing
	style(report)=[font_size=9pt rules=all bordercolor=#f0f0f0 borderstyle=solid borderwidth=2pt frame=box] 
	style(column)=[font=(Arial, 9pt)  bordercolor=#f0f0f0 borderstyle=solid borderwidth=2pt cellwidth=0.6in vjust=m] 
	style(header)=[font=(Arial, 9pt) background=#ADD8E6 bordercolor=#f0f0f0 borderstyle=solid borderwidth=1pt font_weight=bold foreground=black vjust=m];

   %IF &amp;amp;num. &amp;gt; 0 %THEN %DO; 
 	  column subject recno cat %do i=1 %to &amp;amp;sqlobs.; &amp;amp;&amp;amp;var&amp;amp;i  %end; %do i=1 %to &amp;amp;sqlobs.; D_&amp;amp;&amp;amp;var&amp;amp;i %end;  ;		
	
			    define subject	 / display "Subject ID"   			style(column)=[cellwidth=0.7in];
					define co      /display order=data noprint;
					define seq  / display noprint      				style(column)=[cellwidth=1.5in];
          define et   / display ""             	style(column)=[cellwidth=1.5in];
    		  define rv	 	/ display "";
				  define es		/ display "";
		
					%do i=1 %to &amp;amp;sqlobs. ; 
          define D_&amp;amp;&amp;amp;var&amp;amp;i / order=data noprint;
          %end;  
						
					 %do i=1 %to &amp;amp;sqlobs; 	

						compute &amp;amp;&amp;amp;var&amp;amp;i;
								if cat="New Record" then do;
									call define("cat", "style", "style=[background=lightgreen]");
									call define("&amp;amp;&amp;amp;var&amp;amp;i", "style", "style=[background=lightgreen]");
								end;
								else if cat="Deleted Record" then do;
									call define("cat", "style", "style=[background=lightred]");
									call define("&amp;amp;&amp;amp;var&amp;amp;i", "style", "style=[background=lightred]"); 
								end; 
						 endcomp;

						 compute D_&amp;amp;&amp;amp;var&amp;amp;i;
								if D_&amp;amp;&amp;amp;var&amp;amp;i="Yes" then do;
									call define("cat", "style", "style=[background=yellow]");
									call define("&amp;amp;&amp;amp;var&amp;amp;i", "style", "style=[background=yellow]");
								end;
						 endcomp;
					%end;
			run;
	 %End;

	 %Else %Do;
			 column x;
			 define x / display "";
		 run;
	 %End;
%mend;
%rpt;

ods excel close;
ods results on;
ods listing;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Feb 2023 16:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857586#M338854</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2023-02-07T16:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857589#M338857</link>
      <description>&lt;P&gt;Hi:&lt;BR /&gt;Each time you run an ODS EXCEL "sandwich" if the FILE= value is the same as any previous time, the new report will overwrite the previous report. That is the way ODS is designed. Typically, if you want multiple sheets from multiple PROC REPORT steps in one WORKBOOK, you do something like this, conceptually:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1675787482902.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80174i31BACB7A0F473FBC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1675787482902.png" alt="Cynthia_sas_0-1675787482902.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, there should be one workbook created with 4 separate sheets because each PROC REPORT step will start a new sheet by default (unless you change the default behavior with suboptions). As you can see, what is highlighted in yellow could be replaced by a Macro program loop that generated multiple PROC REPORT steps. However, if the ODS EXCEL statement for FILE= was inside the loop, you'd see the behavior you mentioned. The ODS EXCEL FILE= statement is usually placed OUTSIDE of a Macro loop. The placement of the CLOSE will depend on your program logic. Usually the ODS EXCEL CLOSE is also outside of the a Macro program loop.&lt;BR /&gt;&lt;BR /&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 17:22:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857589#M338857</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2023-02-07T17:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857598#M338861</link>
      <description>&lt;P&gt;You need to move the ODS EXCEL FILE= statemen to outside of the loop if you want to write multiple worksheets into the same physical file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* starting ;
ods excel file='myfile.xlsx';

*Looping;
ods excel options (sheet_name="SHEET1");
proc ....
ods excel options (sheet_name="SHEET2");
proc ...
...

* ending;
ods excel close;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also have seem to re-using the same macro name over and over.&amp;nbsp; If they are different then give them different name.&amp;nbsp; If they are the same then define it only once. That will reduce confusion for the poor humans that have to try to understand the code.&amp;nbsp; Define the macros first. Then start writing the executable part of the code.&amp;nbsp; So your program layout might look more like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rpt(parameter);
...
%mend rpt;


%mm;
%rpt(firsttime);

%mm;
%rpt(secondtime);

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 16:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857598#M338861</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-07T16:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857648#M338882</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Thanks so much for the tip. I resolved the issue. Without your expertise, I won't be able to figure out so quickly. thanks again.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this is final flow please find in attachment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;have a wonderful day,&lt;/P&gt;
&lt;P&gt;p&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 19:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/857648#M338882</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2023-02-07T19:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858140#M339050</link>
      <description>&lt;P&gt;Hi all &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas&lt;/a&gt;&amp;nbsp;&amp;nbsp;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was trying to find out&amp;nbsp; if there is a way to suppress the One of the proc report&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for eg. I have 3 data sets (&lt;FONT color="#FF0000"&gt;A, B ,C&lt;/FONT&gt;) in the macro, but I only want 2 of them &lt;FONT color="#FF0000"&gt;(A, B)&lt;/FONT&gt; create excel.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How may I suppress one of&amp;nbsp; dataset for proc report output? currently the&amp;nbsp; "layer O " will automatically create a sheet for&lt;FONT color="#FF0000"&gt; B&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*Layer O*/
title;
ods results off;
ods listing close;
ods escapechar="^";
ods excel file=c\want.xlsx";

/*Layer inside*/
%macro tt;
%if &amp;amp;ds=A %then %do;
proc report;
%end;

%if &amp;amp;ds=C %then %do;
proc report;
%end;

%mend;
%tt (A);&lt;BR /&gt;%tt(B);&lt;BR /&gt;%tt(C);

/*Layer O*/
ods excel close;
ods results on;
ods listing;






&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2023 21:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858140#M339050</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2023-02-09T21:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858156#M339060</link>
      <description>&lt;P&gt;Probably the devil is in the details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Your PROC step is missing the RUN statement to end it, so it will stay open until you start some other step.&lt;/P&gt;
&lt;P&gt;2) Your macro does not take any parameters, so you cannot call it with parameters.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 23:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858156#M339060</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-09T23:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858177#M339076</link>
      <description>Hi: I expect you'll have trouble with your code because you don't specify the path for the FILE= option correctly. As far as I know, on Windows, it needs to be C:\, not just C\ as the starting drive letter. Really, if you don't want the PROC REPORT for dataset C in the ODS EXCEL file, then you should only need to move your macro call OUTSIDE of the ODS EXCEL CLOSE;   Assuming your PROC REPORT code is complete for the C data, then you should only need to move that single invocation outside the ODS EXCEL "sandwich". &lt;BR /&gt;Cynthia</description>
      <pubDate>Fri, 10 Feb 2023 01:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858177#M339076</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2023-02-10T01:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858272#M339109</link>
      <description>hi Tom:&lt;BR /&gt;Thanks for reminder, I give an example not the full codes, but next time I will make the detail more accurate.</description>
      <pubDate>Fri, 10 Feb 2023 14:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858272#M339109</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2023-02-10T14:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Why the proc report overwrite the previous excel sheet when output multiple sheets on one excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858276#M339110</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas&lt;/a&gt;&amp;nbsp;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I fully understand the sandwich concept. have a great weekend.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code and I tested.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Layer O*/
title;
ods results off;
ods listing close;
ods escapechar="^";
ods excel file="C:\showABONLY.xlsx";

data A;
set sashelp.class;
keep name;
run;
data B;
set sashelp.shoes;
keep region;
run;

data C;
set sashelp.cars;
keep make;
run;

/*Layer inside*/
%macro tt(ds);&lt;BR /&gt;
%if &amp;amp;ds=A %then %do;
ods excel options(autofilter="ALL" sheet_name="A");
proc report data=&amp;amp;ds;
column name;
define name/display;
run;
%end;

ods excel options(autofilter="ALL" sheet_name="B");
%if &amp;amp;ds=B %then %do;
proc report data=&amp;amp;ds;
column region;
define region/display;
run;
%end;

%mend;
%tt (A);
%tt(B);

/*Layer O*/
ods excel close;
ods results on;
ods listing;
&lt;BR /&gt;/*-----Dataset C- to suppress the output----*/
%tt(C);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="purpleclothlady_0-1676041406533.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80305iAFB0082AB65BAE7D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="purpleclothlady_0-1676041406533.png" alt="purpleclothlady_0-1676041406533.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 15:03:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-proc-report-overwrite-the-previous-excel-sheet-when/m-p/858276#M339110</guid>
      <dc:creator>purpleclothlady</dc:creator>
      <dc:date>2023-02-10T15:03:35Z</dc:date>
    </item>
  </channel>
</rss>

