<?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 Using a macro to export to excel freq tables and assign sheet name by categorical variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-to-export-to-excel-freq-tables-and-assign-sheet/m-p/861974#M340448</link>
    <description>&lt;P&gt;I'm trying to run a code that will print frequency tables showing demographics of each health condition by different hospitals and export it to excel. The excel export would be one file with several sheets named after each hospital. The code I created is below. In the log, there is nothing indicating an error except an exclamation point on the macro line so I am unsure how to go about solving it. Any help would be great!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%macro export_freq_excel(data=, var=, outpath=);&lt;/P&gt;&lt;P&gt;/* Error handling */&lt;BR /&gt;%if %sysevalf(%superq(data)=, boolean) %then %do;&lt;BR /&gt;%put ERROR: Data parameter not specified.;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if %sysevalf(%superq(var)=, boolean) %then %do;&lt;BR /&gt;%put ERROR: Variable parameter not specified.;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if %sysevalf(%superq(outpath)=, boolean) %then %do;&lt;BR /&gt;%put ERROR: Output path parameter not specified.;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;/* Initialize macro variables */&lt;BR /&gt;%local var_value hosp_list i;&lt;BR /&gt;%let i=1;&lt;/P&gt;&lt;P&gt;/* Get distinct values of the variable */&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select distinct &amp;amp;var into :hosp_list separated by ' '&lt;BR /&gt;from &amp;amp;data&lt;BR /&gt;where &amp;amp;var ne ' '&lt;BR /&gt;order by &amp;amp;var;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;/* Loop over variable values */&lt;BR /&gt;%do %while (%scan(&amp;amp;hosp_list, &amp;amp;i) ne %str());&lt;BR /&gt;%let var_value=%scan(&amp;amp;hosp_list, &amp;amp;i);&lt;/P&gt;&lt;P&gt;/* Create frequency table */&lt;BR /&gt;proc freq data=&amp;amp;data;&lt;BR /&gt;where &amp;amp;var="&amp;amp;var_value";&lt;BR /&gt;table dxcodes_1-dxcodes_9*(sex_ext race_ext agecat)/list nocum out=freq_table;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* Write table to Excel file */&lt;BR /&gt;%let outfilename=&amp;amp;outpath.\freq_&amp;amp;var._&amp;amp;var_value..xlsx;&lt;BR /&gt;ods excel(file=&amp;amp;outfilename) options(sheet_name="&amp;amp;var_value");&lt;BR /&gt;proc print data=freq_table noobs;&lt;BR /&gt;run;&lt;BR /&gt;ods excel(close);&lt;/P&gt;&lt;P&gt;%let i=%eval(&amp;amp;i+1);&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;/* Cleanup */&lt;BR /&gt;%let var_value=;&lt;BR /&gt;%let hosp_list=;&lt;BR /&gt;%let i=;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Log:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;121! %macro export_freq_excel(data=hospdemo,var=ChronHosp,outpath=S:\Informatics\Hospital Demographics);&lt;BR /&gt;122&lt;BR /&gt;123 %local i var_value hosp_list;&lt;BR /&gt;124 %macro export_freq_excel(data=hospdemo,var=ChronHosp,outpath=S:\Informatics\Hospital Demographics);&lt;BR /&gt;125 %macro export_freq_excel(data=, var=, outpath=);&lt;BR /&gt;126&lt;BR /&gt;127 /* Error handling */&lt;BR /&gt;128 %if %sysevalf(%superq(data)=, boolean) %then %do;&lt;BR /&gt;129 %put ERROR: Data parameter not specified.;&lt;BR /&gt;130 %return;&lt;BR /&gt;131 %end;&lt;BR /&gt;132&lt;BR /&gt;133 %if %sysevalf(%superq(var)=, boolean) %then %do;&lt;BR /&gt;134 %put ERROR: Variable parameter not specified.;&lt;BR /&gt;135 %return;&lt;BR /&gt;136 %end;&lt;BR /&gt;137&lt;BR /&gt;138 %if %sysevalf(%superq(outpath)=, boolean) %then %do;&lt;BR /&gt;139 %put ERROR: Output path parameter not specified.;&lt;BR /&gt;140 %return;&lt;BR /&gt;141 %end;&lt;BR /&gt;142&lt;BR /&gt;143 /* Initialize macro variables */&lt;BR /&gt;144 %local var_value hosp_list i;&lt;BR /&gt;145 %let i=1;&lt;BR /&gt;146&lt;BR /&gt;147 /* Get distinct values of the variable */&lt;BR /&gt;148 proc sql noprint;&lt;BR /&gt;149 select distinct &amp;amp;var into :hosp_list separated by ' '&lt;BR /&gt;150 from &amp;amp;data&lt;BR /&gt;151 where &amp;amp;var ne ' '&lt;BR /&gt;152 order by &amp;amp;var;&lt;BR /&gt;153 quit;&lt;BR /&gt;154&lt;BR /&gt;155 /* Loop over variable values */&lt;BR /&gt;156 %do %while (%scan(&amp;amp;hosp_list, &amp;amp;i) ne %str());&lt;BR /&gt;157 %let var_value=%scan(&amp;amp;hosp_list, &amp;amp;i);&lt;BR /&gt;158&lt;BR /&gt;159 /* Create frequency table */&lt;BR /&gt;160 proc freq data=&amp;amp;data;&lt;BR /&gt;161 where &amp;amp;var="&amp;amp;var_value";&lt;BR /&gt;162 table dxcodes_1-dxcodes_9*(sex_ext race_ext agecat)/list nocum out=freq_table;&lt;BR /&gt;163 run;&lt;BR /&gt;164&lt;BR /&gt;165 /* Write table to Excel file */&lt;BR /&gt;166 %let outfilename=&amp;amp;outpath.\freq_&amp;amp;var._&amp;amp;var_value..xlsx;&lt;BR /&gt;167 ods excel(file=&amp;amp;outfilename) options(sheet_name="&amp;amp;var_value");&lt;BR /&gt;168 proc print data=freq_table noobs;&lt;BR /&gt;169 run;&lt;BR /&gt;170 ods excel(close);&lt;BR /&gt;171&lt;BR /&gt;172 %let i=%eval(&amp;amp;i+1);&lt;BR /&gt;173 %end;&lt;BR /&gt;174&lt;BR /&gt;175 /* Cleanup */&lt;BR /&gt;176 %let var_value=;&lt;BR /&gt;177 %let hosp_list=;&lt;BR /&gt;178 %let i=;&lt;BR /&gt;179 %mend;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Mar 2023 16:01:06 GMT</pubDate>
    <dc:creator>Kaeslock223_</dc:creator>
    <dc:date>2023-03-02T16:01:06Z</dc:date>
    <item>
      <title>Using a macro to export to excel freq tables and assign sheet name by categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-to-export-to-excel-freq-tables-and-assign-sheet/m-p/861974#M340448</link>
      <description>&lt;P&gt;I'm trying to run a code that will print frequency tables showing demographics of each health condition by different hospitals and export it to excel. The excel export would be one file with several sheets named after each hospital. The code I created is below. In the log, there is nothing indicating an error except an exclamation point on the macro line so I am unsure how to go about solving it. Any help would be great!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%macro export_freq_excel(data=, var=, outpath=);&lt;/P&gt;&lt;P&gt;/* Error handling */&lt;BR /&gt;%if %sysevalf(%superq(data)=, boolean) %then %do;&lt;BR /&gt;%put ERROR: Data parameter not specified.;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if %sysevalf(%superq(var)=, boolean) %then %do;&lt;BR /&gt;%put ERROR: Variable parameter not specified.;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if %sysevalf(%superq(outpath)=, boolean) %then %do;&lt;BR /&gt;%put ERROR: Output path parameter not specified.;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;/* Initialize macro variables */&lt;BR /&gt;%local var_value hosp_list i;&lt;BR /&gt;%let i=1;&lt;/P&gt;&lt;P&gt;/* Get distinct values of the variable */&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select distinct &amp;amp;var into :hosp_list separated by ' '&lt;BR /&gt;from &amp;amp;data&lt;BR /&gt;where &amp;amp;var ne ' '&lt;BR /&gt;order by &amp;amp;var;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;/* Loop over variable values */&lt;BR /&gt;%do %while (%scan(&amp;amp;hosp_list, &amp;amp;i) ne %str());&lt;BR /&gt;%let var_value=%scan(&amp;amp;hosp_list, &amp;amp;i);&lt;/P&gt;&lt;P&gt;/* Create frequency table */&lt;BR /&gt;proc freq data=&amp;amp;data;&lt;BR /&gt;where &amp;amp;var="&amp;amp;var_value";&lt;BR /&gt;table dxcodes_1-dxcodes_9*(sex_ext race_ext agecat)/list nocum out=freq_table;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* Write table to Excel file */&lt;BR /&gt;%let outfilename=&amp;amp;outpath.\freq_&amp;amp;var._&amp;amp;var_value..xlsx;&lt;BR /&gt;ods excel(file=&amp;amp;outfilename) options(sheet_name="&amp;amp;var_value");&lt;BR /&gt;proc print data=freq_table noobs;&lt;BR /&gt;run;&lt;BR /&gt;ods excel(close);&lt;/P&gt;&lt;P&gt;%let i=%eval(&amp;amp;i+1);&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;/* Cleanup */&lt;BR /&gt;%let var_value=;&lt;BR /&gt;%let hosp_list=;&lt;BR /&gt;%let i=;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Log:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;121! %macro export_freq_excel(data=hospdemo,var=ChronHosp,outpath=S:\Informatics\Hospital Demographics);&lt;BR /&gt;122&lt;BR /&gt;123 %local i var_value hosp_list;&lt;BR /&gt;124 %macro export_freq_excel(data=hospdemo,var=ChronHosp,outpath=S:\Informatics\Hospital Demographics);&lt;BR /&gt;125 %macro export_freq_excel(data=, var=, outpath=);&lt;BR /&gt;126&lt;BR /&gt;127 /* Error handling */&lt;BR /&gt;128 %if %sysevalf(%superq(data)=, boolean) %then %do;&lt;BR /&gt;129 %put ERROR: Data parameter not specified.;&lt;BR /&gt;130 %return;&lt;BR /&gt;131 %end;&lt;BR /&gt;132&lt;BR /&gt;133 %if %sysevalf(%superq(var)=, boolean) %then %do;&lt;BR /&gt;134 %put ERROR: Variable parameter not specified.;&lt;BR /&gt;135 %return;&lt;BR /&gt;136 %end;&lt;BR /&gt;137&lt;BR /&gt;138 %if %sysevalf(%superq(outpath)=, boolean) %then %do;&lt;BR /&gt;139 %put ERROR: Output path parameter not specified.;&lt;BR /&gt;140 %return;&lt;BR /&gt;141 %end;&lt;BR /&gt;142&lt;BR /&gt;143 /* Initialize macro variables */&lt;BR /&gt;144 %local var_value hosp_list i;&lt;BR /&gt;145 %let i=1;&lt;BR /&gt;146&lt;BR /&gt;147 /* Get distinct values of the variable */&lt;BR /&gt;148 proc sql noprint;&lt;BR /&gt;149 select distinct &amp;amp;var into :hosp_list separated by ' '&lt;BR /&gt;150 from &amp;amp;data&lt;BR /&gt;151 where &amp;amp;var ne ' '&lt;BR /&gt;152 order by &amp;amp;var;&lt;BR /&gt;153 quit;&lt;BR /&gt;154&lt;BR /&gt;155 /* Loop over variable values */&lt;BR /&gt;156 %do %while (%scan(&amp;amp;hosp_list, &amp;amp;i) ne %str());&lt;BR /&gt;157 %let var_value=%scan(&amp;amp;hosp_list, &amp;amp;i);&lt;BR /&gt;158&lt;BR /&gt;159 /* Create frequency table */&lt;BR /&gt;160 proc freq data=&amp;amp;data;&lt;BR /&gt;161 where &amp;amp;var="&amp;amp;var_value";&lt;BR /&gt;162 table dxcodes_1-dxcodes_9*(sex_ext race_ext agecat)/list nocum out=freq_table;&lt;BR /&gt;163 run;&lt;BR /&gt;164&lt;BR /&gt;165 /* Write table to Excel file */&lt;BR /&gt;166 %let outfilename=&amp;amp;outpath.\freq_&amp;amp;var._&amp;amp;var_value..xlsx;&lt;BR /&gt;167 ods excel(file=&amp;amp;outfilename) options(sheet_name="&amp;amp;var_value");&lt;BR /&gt;168 proc print data=freq_table noobs;&lt;BR /&gt;169 run;&lt;BR /&gt;170 ods excel(close);&lt;BR /&gt;171&lt;BR /&gt;172 %let i=%eval(&amp;amp;i+1);&lt;BR /&gt;173 %end;&lt;BR /&gt;174&lt;BR /&gt;175 /* Cleanup */&lt;BR /&gt;176 %let var_value=;&lt;BR /&gt;177 %let hosp_list=;&lt;BR /&gt;178 %let i=;&lt;BR /&gt;179 %mend;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2023 16:01:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-to-export-to-excel-freq-tables-and-assign-sheet/m-p/861974#M340448</guid>
      <dc:creator>Kaeslock223_</dc:creator>
      <dc:date>2023-03-02T16:01:06Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro to export to excel freq tables and assign sheet name by categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-to-export-to-excel-freq-tables-and-assign-sheet/m-p/861976#M340449</link>
      <description>&lt;P&gt;You only showed the log for the compilation (definition) of the macro.&lt;/P&gt;
&lt;P&gt;You do not show any code that actually tried to call the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why do you need use a macro (or any type of code generation) to do this?&lt;/P&gt;
&lt;P&gt;Why not just use BY statement?&amp;nbsp; You can adjust the name of the sheet using the value of the BY variable.&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=&amp;amp;data;
  table &amp;amp;var*(dxcodes_1-dxcodes_9)*(sex_ext race_ext agecat)/list nocum out=freq_table;
run;

options nobyline;
ods excel(file=&amp;amp;outfilename)  options(sheet_name="#byval(&amp;amp;var)");
proc print data=freq_table noobs;
  by &amp;amp;var;
run;
options byline;
ods excel(close);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Mar 2023 16:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-to-export-to-excel-freq-tables-and-assign-sheet/m-p/861976#M340449</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-02T16:15:29Z</dc:date>
    </item>
  </channel>
</rss>

