<?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: Print to Excel with multiple sheets from a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758313#M239420</link>
    <description>&lt;P&gt;This isn't correct macro code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;i=1 then 
ods excel file="&amp;amp;path.&amp;amp;unit longitudinal vaccine data, %sysfunc(trim(%qsysfunc(left(%qsysfunc(date(),worddate.))))).xlsx"
  options(sheet_name="&amp;amp;unit");
  else if &amp;amp;i&amp;gt;1 then ods excel options(sheet_name="unit");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Possibly this might work better:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;i=1 %then %do; 
ods excel file="&amp;amp;path.&amp;amp;unit longitudinal vaccine data, %sysfunc(trim(%qsysfunc(left(%qsysfunc(date(),worddate.))))).xlsx"
  options(sheet_name="&amp;amp;unit");
%end;
%else %if &amp;amp;i&amp;gt;1 %then %do;
ods excel options(sheet_name="unit");
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 30 Jul 2021 02:36:06 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2021-07-30T02:36:06Z</dc:date>
    <item>
      <title>Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758303#M239414</link>
      <description>&lt;P&gt;Hi, I have a macro which cycles through 58 California counties. I would like to create a single Excel workbook where each county's output has a separate tab. This would be simple enough except that there are 220 records per county and I don't want to have all that print to the regular SAS output in the process. Much to my surprise, after only about an hour searching through existing help questions and google pages, I managed to create what I want with the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let counties=California Alameda ... Yuba;
DATA _null_;
  if _n_=1 then do;
    set one;
	numcnts=countw(symget('counties'));
	call symputx('numcounties',numcnts,'g');
  end;
run;

%macro geo;
ods _all_ close;
ods tagsets.ExcelXP path="&amp;amp;path" file="Longitudinal vaccine data.xml";
  %do i=1 %to &amp;amp;numcounties;
    %let unit=%scan(&amp;amp;counties, &amp;amp;i);
%put geographical unit=&amp;amp;unit;
DATA &amp;amp;unit;
  set one;
  geounit=symget('unit');
  if county=geounit;
run;
if &amp;amp;i=1 then 
ods excel file="&amp;amp;path.&amp;amp;unit longitudinal vaccine data, %sysfunc(trim(%qsysfunc(left(%qsysfunc(date(),worddate.))))).xlsx"
  options(sheet_name="&amp;amp;unit");
  else if &amp;amp;i&amp;gt;1 then ods excel options(sheet_name="unit");
PROC PRINT data=&amp;amp;unit; var county--doses1p; run;
ods excel close;
%end;
ods tagsets.ExcelXP close;
%mend;
%geo;
ods listing;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, while it successfully creates the Excel workbook without writing to the output window, it also puts the following error messages in the log (showing the first loop only):&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dbjosiah_0-1627607559682.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62116i4E86B71F405CABE9/image-size/large?v=v2&amp;amp;px=999" role="button" title="dbjosiah_0-1627607559682.png" alt="dbjosiah_0-1627607559682.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;To be clear, I'm happy with the product, just wondering what the error issue is.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 01:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758303#M239414</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-07-30T01:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758312#M239419</link>
      <description>I've seen messages like that before.  The messages aren't particularly helpful.  I guess if it's working, don't get too excited about it.&lt;BR /&gt;&lt;BR /&gt;Jim</description>
      <pubDate>Fri, 30 Jul 2021 02:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758312#M239419</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-07-30T02:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758313#M239420</link>
      <description>&lt;P&gt;This isn't correct macro code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;i=1 then 
ods excel file="&amp;amp;path.&amp;amp;unit longitudinal vaccine data, %sysfunc(trim(%qsysfunc(left(%qsysfunc(date(),worddate.))))).xlsx"
  options(sheet_name="&amp;amp;unit");
  else if &amp;amp;i&amp;gt;1 then ods excel options(sheet_name="unit");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Possibly this might work better:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;i=1 %then %do; 
ods excel file="&amp;amp;path.&amp;amp;unit longitudinal vaccine data, %sysfunc(trim(%qsysfunc(left(%qsysfunc(date(),worddate.))))).xlsx"
  options(sheet_name="&amp;amp;unit");
%end;
%else %if &amp;amp;i&amp;gt;1 %then %do;
ods excel options(sheet_name="unit");
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jul 2021 02:36:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758313#M239420</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-07-30T02:36:06Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758314#M239421</link>
      <description>&lt;P&gt;Turn on the MPRINT option so you can see the SAS code that your macro is generating.&amp;nbsp; That will make the error messages much clearer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This first step before the macro definition does not make much sense.&amp;nbsp; Why are you referencing the dataset? The code does not use the data at all.&amp;nbsp; Just simplify to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  numcnts=countw(symget('counties'));
  call symputx('numcounties',numcnts,'g');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the macro code is confusing.&amp;nbsp; Your macro is generating a IF statement outside of any data step (or proc that allows that statement).&amp;nbsp; Did you intend to use macro %IF statement to conditionally generate different blocks of SAS code instead?&lt;/P&gt;
&lt;P&gt;But then what is the different SAS code that you intend to generate?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you opening both an TAGSETS.EXCELXP output destination and an EXCEL destination?&lt;/P&gt;
&lt;P&gt;Do you want one file with all sheets and a series of separate files with one sheet each?&lt;/P&gt;
&lt;P&gt;Is there any reason why one of them needs be and XML file instead of an XLSX file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you have an extra DATA step that is just subsetting the data? Why not just add a WHERE statement to the PROC PRINT step?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 03:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758314#M239421</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-30T03:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758315#M239422</link>
      <description>&lt;P&gt;I must have missed something, I don't understand why such complex code.&lt;/P&gt;
&lt;P&gt;Why not this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let counties=California Alameda ... Yuba;

%macro geo;
  %local i county;
  ods _all_ close;
  ods excel file="&amp;amp;path.&amp;amp;unit longitudinal vaccine data, %qsysfunc(strip(%qsysfunc(date(),worddate.))).xlsx";
  %do i=1 %to %sysfunc(countw(&amp;amp;counties));
    %let county=%scan(&amp;amp;counties, &amp;amp;i);
    %put geographical &amp;amp;=county;
    ods excel options(sheet_name="&amp;amp;county");
    proc print data=ONE ; where COUNTY="&amp;amp;county"; var COUNTY--DOSES1P; run;
  %end;
  ods excel close;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also, I generally use this for my time stamps&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let timestamp=%sysfunc(translate( %sysfunc(datetime(),timetamp.),-,:));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as the files are naturally sorted by date when sorted by name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 04:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758315#M239422</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-07-30T04:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758325#M239430</link>
      <description>&lt;P&gt;Why are you using the outdated tagset.excelxp and not "excel"? Both have an option to create multiple sheets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=work.class;
  by Sex;
run;

options nobyline;

ods excel file="&amp;amp;benutzer\temp\class.xlsx" 
   options(sheet_interval='bygroup' sheet_name="#byval1");

proc print data=work.class noobs;
   by Sex;
run;

ods excel close;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jul 2021 05:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758325#M239430</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-07-30T05:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758433#M239474</link>
      <description>This worked great! Not sure how/where to incorporate your timestamp though.</description>
      <pubDate>Fri, 30 Jul 2021 16:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758433#M239474</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-07-30T16:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758435#M239475</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/377938"&gt;@dbjosiah&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;This worked great! Not sure how/where to incorporate your timestamp though.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I believe&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;is referring to the filename for your Excel file.&amp;nbsp; Putting a date stamp into the file name in the format that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;suggests will automatically put the files in order (and guarantee unique file names of course).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 16:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758435#M239475</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-07-30T16:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758438#M239477</link>
      <description>Oh, ok, thanks. I got it to work, but since I only organize these by week, I don't really need unique names.</description>
      <pubDate>Fri, 30 Jul 2021 16:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758438#M239477</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-07-30T16:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758440#M239479</link>
      <description>&lt;P&gt;You can also just allow the PROC and ODS EXCEL to generate separate sheets for each value of COUNTY.&amp;nbsp;&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sastraining/2017/04/18/control-name-excel-sheets-created/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2017/04/18/control-name-excel-sheets-created/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So no macro needed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let counties=California Alameda ... Yuba;
ods excel file="&amp;amp;path.Longitudinal vaccine data by county %sysfunc(date(),yymmdd10.).xlsx";
ods excel options(sheet_interval='bygroup' sheet_name='#byval1');
options nobyline;
proc print data=ONE;
  where findw("&amp;amp;counties",county,' ','t');
  by county;
  var COUNTY--DOSES1P;
run;
options byline;
ods excel close;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 16:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758440#M239479</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-30T16:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Print to Excel with multiple sheets from a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758442#M239481</link>
      <description>Wow Tom, that's great too! But I've learned so much from the other macro solutions... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;Thanks everyone. I'll credit jimbarbour with the Solution since he answered my actual question, but all of the suggestions are greatly appreciated.</description>
      <pubDate>Fri, 30 Jul 2021 17:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Print-to-Excel-with-multiple-sheets-from-a-macro/m-p/758442#M239481</guid>
      <dc:creator>dbjosiah</dc:creator>
      <dc:date>2021-07-30T17:04:14Z</dc:date>
    </item>
  </channel>
</rss>

