<?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: Exporting same dataset to multiple excel files in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649787#M78691</link>
    <description>&lt;P&gt;Make your life easier:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro export(make);
proc export
  data=testing (where=(make="&amp;amp;make."))
  outfile="C:\Users\&amp;amp;make..xlsx"
  dbms=xlsx
  replace
;
sheet="&amp;amp;make.";
run;
%mend;

proc sort
  data=testing (keep=make)
  out=control
  nodupkey
;
by make;
run;

data _null_;
set control;
call execute(cats('%nrstr(%export(',make,'))'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 22 May 2020 06:39:25 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-05-22T06:39:25Z</dc:date>
    <item>
      <title>Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649780#M78686</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to export the dataset to multiple excel files based on a certain variable:&lt;/P&gt;&lt;P&gt;---------------------&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table try as&lt;BR /&gt;select distinct make from sashelp.cars;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table try2 as&lt;BR /&gt;select count(make) as aaa from sashelp.cars;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;set try;&lt;BR /&gt;by make;&lt;BR /&gt;call symputx ('make',compress(make,' .'),'g');&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;set try2;&lt;BR /&gt;call symputx('n',aaa);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro a;&lt;BR /&gt;%do i=1 %to &amp;amp;n;&lt;BR /&gt;%let var= %scan(&amp;amp;make,&amp;amp;i,"@");&lt;BR /&gt;proc export data=testing (where=(make="&amp;amp;make."))&lt;BR /&gt;outfile="C:\Users\&amp;amp;make..xlsx"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;sheet="&amp;amp;make." ;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%mend ;&lt;BR /&gt;%a;&lt;/P&gt;&lt;P&gt;------------------------------------------------------&lt;/P&gt;&lt;P&gt;My goal is to get all the 38 excel files with the maker name as the filename.&lt;/P&gt;&lt;P&gt;However, all I am able to get here is the last maker name's file.&lt;/P&gt;&lt;P&gt;Would you please point out where I am missing out here? Many thanks!!&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 05:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649780#M78686</guid>
      <dc:creator>cccc0111</dc:creator>
      <dc:date>2020-05-22T05:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649782#M78687</link>
      <description>&lt;P&gt;Here are some improvements/issues you can apply/work on:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Utilise "select into" to simplify the portion of your code that gets the distinct values and count and then assigns them to macro variables&lt;/LI&gt;
&lt;LI&gt;Regarding "count(make)", this counts the variable &lt;U&gt;make&lt;/U&gt; as a whole, regardless if it is null or is a repeating value. I believe you are after "count (distinct make)"&lt;/LI&gt;
&lt;LI&gt;Incorrect placing of the closing ")" for the compress function. &lt;BR /&gt;This -&amp;gt;&amp;nbsp;&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx ('make',compress(make,' .'),'g')&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;SPAN&gt;&lt;SPAN&gt;Should be -&amp;gt;&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symputx ('make',compress(make,' .','g'))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;SPAN&gt;You are assigning to macro variable "var",&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var= %scan(&amp;amp;make,&amp;amp;i,"@");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;SPAN&gt;but you are not using it anywhere in your code, looks like you are confusing it with &amp;amp;make&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Would also recommend to use descriptive names for macro programs&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;Remember to always end your proc sql; with quit;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct make,
count (distinct make) 
into 
:make_list separated by ' ', 
:n
from sashelp.cars;
quit;

%put &amp;amp;=make_list;
%put &amp;amp;=n;

%macro export_make;
	%do i=1 %to &amp;amp;n;
		%let make=%scan(&amp;amp;make_list,&amp;amp;i);

		proc export data=testing (where=(make="&amp;amp;make."))
		outfile="C:\Users\&amp;amp;make..xlsx"
		dbms=xlsx replace;
		sheet="&amp;amp;make." ;
		run;
	%end;
%mend export_make;

%export_make;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 06:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649782#M78687</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-05-22T06:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649783#M78688</link>
      <description>&lt;P&gt;In your %let statement below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var= %scan(&amp;amp;make,&amp;amp;i,"@");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is a difference between macro %scan and scan.&amp;nbsp; The delimiter in %scan should be exactly what it is in the macro variable value without quotes if you don't want the quotes as part of the delimiter.&amp;nbsp; For example if your macro variable make equaled something like: &lt;A href="mailto:One@Two@Three" target="_blank"&gt;One@Two@Three &lt;/A&gt;and you specify "@" as your delimiter it won't actually see three words because the delimiter "@" doesn't appear in the macro text.&amp;nbsp; Instead you can try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var= %scan(&amp;amp;make,&amp;amp;i,@);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I don't know the value of your MAKE macro variable, but this is one potential issue that came to mind.&lt;/P&gt;
&lt;P&gt;I would change your loop code to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro a;
%do i=1 %to &amp;amp;n;
%let var= %scan(&amp;amp;make,&amp;amp;i,@);
proc export data=testing (where=(make="&amp;amp;var."))
outfile="C:\Users\&amp;amp;var..xlsx"
dbms=xlsx replace;
sheet="&amp;amp;var." ;
run;
%end;
%mend ;
%a;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since I think you're trying to use that scanned value as the name instead of the whole macro variable.&amp;nbsp; The macro variable VAR holds the value from the %scan.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 06:15:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649783#M78688</guid>
      <dc:creator>JeffMeyers</dc:creator>
      <dc:date>2020-05-22T06:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649784#M78689</link>
      <description>&lt;P&gt;I just wanted to point out that this is not true:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Incorrect placing of the closing ")" for the compress function. &lt;BR /&gt;This -&amp;gt;&amp;nbsp;&lt;SPAN&gt;call symputx ('make',compress(make,' .'&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;)&lt;/FONT&gt;&lt;/STRONG&gt;,'g')&amp;nbsp;&lt;BR /&gt;Should be -&amp;gt;&amp;nbsp;call symputx ('make',compress(make,' .','g'&lt;FONT color="#00FF00"&gt;&lt;STRONG&gt;)&lt;/STRONG&gt;&lt;/FONT&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The 'g' option for the call symputx function specifies that the macro will be a global macro variable.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 06:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649784#M78689</guid>
      <dc:creator>JeffMeyers</dc:creator>
      <dc:date>2020-05-22T06:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649785#M78690</link>
      <description>&lt;P&gt;Thanks for pointing that out, that is correct (if you use &lt;A title="Syntax: CALL SYMPUTX(macro-variable, value &amp;lt;, symbol-table&amp;gt;);" href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=p1fa0ay5pzr9yun1mvqxv8ipzd4d.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;symputx&lt;/A&gt;). It is also possible that 'g' was used as a modifier to the compress function.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 06:39:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649785#M78690</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-05-22T06:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649787#M78691</link>
      <description>&lt;P&gt;Make your life easier:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro export(make);
proc export
  data=testing (where=(make="&amp;amp;make."))
  outfile="C:\Users\&amp;amp;make..xlsx"
  dbms=xlsx
  replace
;
sheet="&amp;amp;make.";
run;
%mend;

proc sort
  data=testing (keep=make)
  out=control
  nodupkey
;
by make;
run;

data _null_;
set control;
call execute(cats('%nrstr(%export(',make,'))'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 May 2020 06:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649787#M78691</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-05-22T06:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649789#M78692</link>
      <description>&lt;P&gt;the code works perfect right now and thank you so much for your advise &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 06:46:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649789#M78692</guid>
      <dc:creator>cccc0111</dc:creator>
      <dc:date>2020-05-22T06:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649928#M78697</link>
      <description>&lt;P&gt;I illustrate exactly this problem here but with an HTML file, not an Excel file. You can change the ODS to ODS EXCEL and get the exact same results. The file name is dynamic as well. I generally prefer this method over loops and creating multiple macro variables as this is easier to debug and mentally develop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It even happens to use the exact same input data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 15:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/649928#M78697</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-22T15:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/650607#M78723</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/2153"&gt;@JeffMeyers&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In your %let statement below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let var= %scan(&amp;amp;make,&amp;amp;i,"@");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is a difference between macro %scan and scan.&amp;nbsp; The delimiter in %scan should be exactly what it is in the macro variable value without quotes if you don't want the quotes as part of the delimiter.&amp;nbsp; For example if your macro variable make equaled something like: &lt;A href="mailto:One@Two@Three" target="_blank" rel="noopener"&gt;One@Two@Three &lt;/A&gt;and you specify "@" as your delimiter it won't actually see three words because the delimiter "@" doesn't appear in the macro text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not correct. %SCAN works just like SCAN in that respect, if you put more than one character as a delimiter parameter, the %scan function treats all characters as possible delimiters. The difference is that %SCAN does not expect text in quotes, if you put the parameter in quotes, the quote characters will also be treated as delimiters. So %SCAN(&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/116880"&gt;@one&lt;/a&gt;@Two@Three,2,"@") returns Two. But so does&amp;nbsp;%SCAN(@One@"Two"@Three,2,"@"), as the double quote is also considered a delimiter.&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 06:36:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/650607#M78723</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-26T06:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Exporting same dataset to multiple excel files</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/650609#M78724</link>
      <description>&lt;P&gt;The problem is that CALL SYMPUTX overwrites the previous value for every iteration of the data step, so after the data step has run, &amp;amp;MAKE has the last value encountered in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are several ways to solve this. One is simply to create a macro variable for each make:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set try;
  call symputx (cats('make',_N_),compress(make,' .'),'g');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will then have a series of macro variables, which can be used in your macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But this can be accomplished much simpler, by using SQL SELECT INTO:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct make) into :N trimmed from sashelp.cars;
  select distinct make into :make1-:make&amp;amp;N from sashelp.cars;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then adapt your macro to use the make variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it is better to make a simpler macro, and use SQL to generate a series of macro calls:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro a(make);
proc export data=testing (where=(make="&amp;amp;make."))
  outfile="C:\Users\&amp;amp;make..xlsx"
  dbms=xlsx replace;
  sheet="&amp;amp;make." ;
run;
%mend ;

proc sql noprint;
  select distinct cats('%a(',make,')') into :doit separated by ';';
quit;

&amp;amp;doit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 May 2020 06:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Exporting-same-dataset-to-multiple-excel-files/m-p/650609#M78724</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-26T06:56:29Z</dc:date>
    </item>
  </channel>
</rss>

