<?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: Macro Variable Name Used as Excel Sheet Name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390591#M277628</link>
    <description>&lt;P&gt;If your model (&amp;amp;sheetname) is called XXX, and you want to have a sheet called XXX_HWA, then just do this:&lt;/P&gt;
&lt;PRE&gt;sheet_name="&amp;amp;sheetname._HWA"&lt;/PRE&gt;
&lt;P&gt;The dot symbolises the end of the macro variable name and will not appear in the resolved string. Note that macro variables are not resolved inside single quotes.&lt;/P&gt;</description>
    <pubDate>Thu, 24 Aug 2017 13:07:20 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-08-24T13:07:20Z</dc:date>
    <item>
      <title>Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390587#M277626</link>
      <description>&lt;P&gt;Hey all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a macro that does a series of forecasting. The bits I have an issue with have to do with using the variables being models as sheet names in Excel. It goes like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro forecast_all (var_list=);
/*
Macro language does not use quotes around the values of macro variables
Macro language %DO loops are limited, and can iterate over a range of numeric values only
*/

%local n f_var sheetname;
%do n=1 %to %sysfunc(countw(&amp;amp;var_list));
	%let f_var=%scan(&amp;amp;var_list,&amp;amp;n);
	data _null_;
    	%let nametmp1=%sysfunc(substr(&amp;amp;f_var,1,2));
		%let nametmp2=%sysfunc(substr(&amp;amp;f_var,7,4));
	%if &amp;amp;nametmp1="OV" %then %let sheetname=&amp;amp;nametmp1||&amp;amp;nametmp2;


forecast code here

ods excel file="R:\Traffic Forecasting\TFM 3.0\Forecast Input Data.xlsx";
			ods excel options(start_at="1,1" sheet_name='&amp;amp;sheetname'+"_HWA" sheet_interval="table");
				proc print data=EGTASK.HF_HWA_OV_ECU_M11 noobs; run;

%end;
%mend;&lt;BR /&gt;&lt;BR /&gt;%forecast_all (var_list=OV_ECU_M11 OV_ECU_M14);&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, I want to create sheet names&amp;nbsp;for Excel based on the variable name, with an append depnding on the forecast. I have tried everything in the line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;ods excel options(start_at=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"1,1"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; sheet_name='&amp;amp;sheetname'+"_HWA" sheet_interval=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"table"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;So as you can see, for a variable called OV_ECU_M11, the sheet name for Excel would be OV_M11_HWA. Obviously the + above does not work, I just added it in to show what I wanted to do.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 12:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390587#M277626</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-08-24T12:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390590#M277627</link>
      <description>&lt;P&gt;Guessing as can't run it (in meeting), but maybe:&lt;/P&gt;
&lt;PRE&gt;sheet_name="&amp;amp;sheetname._HWA"&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2017 13:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390590#M277627</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-08-24T13:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390591#M277628</link>
      <description>&lt;P&gt;If your model (&amp;amp;sheetname) is called XXX, and you want to have a sheet called XXX_HWA, then just do this:&lt;/P&gt;
&lt;PRE&gt;sheet_name="&amp;amp;sheetname._HWA"&lt;/PRE&gt;
&lt;P&gt;The dot symbolises the end of the macro variable name and will not appear in the resolved string. Note that macro variables are not resolved inside single quotes.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 13:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390591#M277628</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-24T13:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390596#M277629</link>
      <description>&lt;P&gt;You're opening ODS EXCEL statement needs to be moved to outside your loop, otherwise you're creating a new file each time? I'm not sure what would happen actually but I wouldn't expect it to work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 13:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390596#M277629</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-24T13:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390606#M277630</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There will be about 10 variables, each forecasting with 4 methods. Excel will be poplulated with forecast data from each model as a separate sheet. The ODS seems to work fine so far as it will simply make a new worksheet for each output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;ods graphics on / width=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;8&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;in;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;ods tagsets.sasreport13(id=EGSR) gtitle gfootnote;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should propbably be moved outside though.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As was posted just before you: sheet_name="&amp;amp;sheetname._HWA" works fine, but the sheet name ends up as _HWA.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems the following does not work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; nametmp1=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(substr(&amp;amp;f_var,1,2));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; nametmp2=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%sysfunc&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(substr(&amp;amp;f_var,7,4));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;nametmp1=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'OV'&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; sheetname=&amp;amp;nametmp1||&amp;amp;nametmp2;&lt;/FONT&gt;&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;&lt;FONT color="#800080" face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 13:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390606#M277630</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-08-24T13:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390608#M277631</link>
      <description>&lt;P&gt;The macro preprocessor knows only one datatype, namely text. Quotes are therefore not needed, unless you want those quotes to be part of the text. There is also no concatenation operator, as concatenation is simply done this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;nametmp1=OV %then %let sheetname=&amp;amp;nametmp1&amp;amp;nametmp2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if &amp;amp;nametmp1 contains OV and &amp;amp;nametmp2 contains XXX, you'll get &amp;amp;sheetname=OVXXX&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS there is also no need for the data _null_&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 13:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390608#M277631</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-08-24T13:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390631#M277632</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/142314"&gt;@BCNAV&lt;/a&gt;&amp;nbsp;just tested it and it appears to not work on my system at least, SAS 9.4 TS1M3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;

select distinct age into : age1-
from sashelp.class;
quit;

%let num_ages = &amp;amp;sqlobs.;



%macro loop;

%do i=1 %to &amp;amp;num_ages;

%put &amp;amp;i;

ods excel file='C:\_localdata\temp\test.xlsx';
ods excel options (sheet_name="Age = &amp;amp;&amp;amp;age&amp;amp;i");

proc print data=sashelp.class;
where age=&amp;amp;&amp;amp;age&amp;amp;i;
run;

%end;

%mend;

ods excel close;

%loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You're trying to create the file each time, but it's still in use. The ODS EXCEL OPTIONS statement should stay in your loop, but the ODS EXCEL FILE statement should be outside of the loop. Unless you have another loop there to create the sheets.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 14:43:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390631#M277632</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-24T14:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390639#M277633</link>
      <description>&lt;P&gt;So this is going well with the replies here...getting there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By moving the ods excel outside of the loop SAS is adding sheets to the excel file I did not ask for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;They are Directory Information and Library members. Is there a way to suppress this output. Not sure why it is there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, is there a way to refer to multiple Excel files? For example, I would like another separate spreadsheet to house the model estimates&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much for the help so far everyone!&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 14:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390639#M277633</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-08-24T14:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Name Used as Excel Sheet Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390644#M277634</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/142314"&gt;@BCNAV&lt;/a&gt;&amp;nbsp;Since you're now facing different issues, you need to post your code. Remember all output is directed to ODS EXCEL, so if you have any procs in that loop that generate output it will go to the Excel file, unless you tell it not to. You can do that using ODS EXCLUDE or ODS SELECT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 15:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Name-Used-as-Excel-Sheet-Name/m-p/390644#M277634</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-24T15:14:39Z</dc:date>
    </item>
  </channel>
</rss>

