<?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 Proc export BY to many excel sheets in same file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577301#M163587</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to do proc export by&amp;nbsp; "Origin".&lt;/P&gt;
&lt;P&gt;I want that automatically will be created a sheet for each Origin&lt;/P&gt;
&lt;P&gt;I know to do it by preforming proc export for each "Origin" value but&amp;nbsp; my question is how to do it automatically?&lt;/P&gt;
&lt;P&gt;(For example if there were 1000 values for "Origin" then it is not clever to way to write so many proc exports)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export data=sashelp.cars (where=(Origin='Asia'))
outfile="/path/cars.xlsx"
DBMS=xlsx replace;
sheet='Asia';
run;
proc export
data=sashelp.cars (where=(Origin='Europe'))
file="path/cars.xlsx"
DBMS=xlsx replace;
sheet='Europe';
run;
proc export
data=sashelp.cars (where=(Origin='USA'))
file="/path/cars.xlsx"
DBMS=xlsx replace;
sheet='USA';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Jul 2019 07:26:59 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-07-29T07:26:59Z</dc:date>
    <item>
      <title>Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577301#M163587</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to do proc export by&amp;nbsp; "Origin".&lt;/P&gt;
&lt;P&gt;I want that automatically will be created a sheet for each Origin&lt;/P&gt;
&lt;P&gt;I know to do it by preforming proc export for each "Origin" value but&amp;nbsp; my question is how to do it automatically?&lt;/P&gt;
&lt;P&gt;(For example if there were 1000 values for "Origin" then it is not clever to way to write so many proc exports)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export data=sashelp.cars (where=(Origin='Asia'))
outfile="/path/cars.xlsx"
DBMS=xlsx replace;
sheet='Asia';
run;
proc export
data=sashelp.cars (where=(Origin='Europe'))
file="path/cars.xlsx"
DBMS=xlsx replace;
sheet='Europe';
run;
proc export
data=sashelp.cars (where=(Origin='USA'))
file="/path/cars.xlsx"
DBMS=xlsx replace;
sheet='USA';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 07:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577301#M163587</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-29T07:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577302#M163588</link>
      <description>&lt;P&gt;Step 1: identify the dynamic elements, and replace with macro variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let origin=Asia;

proc export data=sashelp.cars (where=(Origin="&amp;amp;origin."))
outfile="/path/cars.xlsx"
DBMS=xlsx replace;
sheet="&amp;amp;origin.";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note double quotes instead of single quotes; single quotes prevent macro resolution.&lt;/P&gt;
&lt;P&gt;Test if it works.&lt;/P&gt;
&lt;P&gt;Step 2: wrap into a macro definition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro excel_export(origin);
proc export data=sashelp.cars (where=(Origin="&amp;amp;origin."))
outfile="/path/cars.xlsx"
DBMS=xlsx replace;
sheet="&amp;amp;origin.";
run;
%mend;

%origin(Asia)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Test again.&lt;/P&gt;
&lt;P&gt;Step 3: create a control dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort
  data=sashelp.cars (keep=origin)
  out=control
  nodupkey
;
by origin;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and (step 4) use it to call the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set control;
call execute('%nrstr(%excel_export(' !! strip(origin) !! '))');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 07:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577302#M163588</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-29T07:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577303#M163589</link>
      <description>&lt;P&gt;Unfortunately there is no option to use BY in PROC EXPORT.&lt;/P&gt;
&lt;P&gt;You can automate your code by a macro program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro export(dsnin);
        proc sql;
             create table origins as
             select distinct origin from &amp;amp;dsnin;
       quit;
       %let no_of_org = &amp;amp;sqlobs;

       data _null_; 
         set origins;
             call symput('org'!!trim(_N_), strip(origin));
         run;

         %do i=1 %to &amp;amp;no_of_org;  
             proc export data=&amp;amp;dsnin (where=(Origin="&amp;amp;&amp;amp;org&amp;amp;i"))
                  outfile="/path/cars.xlsx"
                  DBMS=xlsx replace;
                  sheet="&amp;amp;&amp;amp;org&amp;amp;i";
             run;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%end;&lt;BR /&gt;%mend&amp;nbsp;export;&lt;BR /&gt;%export(sashelp.cars);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 07:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577303#M163589</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-07-29T07:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577311#M163593</link>
      <description>&lt;P&gt;You can do this with ods excel without any macro-code, but the names of the sheets may not fit your requirements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Demo:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.cars out=work.cars;
   by origin;
run;

ods excel file="cars.xlsx" options(sheet_interval='bygroup');

proc print data=work.cars;
   by Origin;
run;

ods excel close;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 10:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577311#M163593</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-07-29T10:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577325#M163601</link>
      <description>&lt;P&gt;You can add the SHEET_NAME option with #BYVAL see usage note. &lt;A href="http://support.sas.com/kb/56/543.html" target="_self"&gt;Usage Note 56543: The Excel worksheet&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options byline=0;
ods excel file="cars.xlsx" options(sheet_interval='bygroup' sheet_name="#byval1");
proc print data=work.cars;
   by Origin;
   run;
ods excel close;
options byline=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You can do this with ods excel without any macro-code, but the names of the sheets may not fit your requirements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Demo:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.cars out=work.cars;
   by origin;
run;

ods excel file="cars.xlsx" options(sheet_interval='bygroup');

proc print data=work.cars;
   by Origin;
run;

ods excel close;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 11:55:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577325#M163601</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2019-07-29T11:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577333#M163604</link>
      <description>&lt;P&gt;Libname statement ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;libname x xlsx 'c:\temp\x.xlsx';

data x.asia;
 set sashelp.cars (where=(Origin='Asia'));
run;

data x.usa;
 sashelp.cars (where=(Origin='USA'));
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 12:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577333#M163604</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-29T12:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577589#M163669</link>
      <description>&lt;P&gt;Hello and thank you for everyone.&lt;/P&gt;
&lt;P&gt;Please find here the all solutions that you provided&amp;nbsp; and one solution that I added.&lt;/P&gt;
&lt;P&gt;Which way do you think is the best?&lt;/P&gt;
&lt;P&gt;Which way provides us the best ability to control formats in excel file?&lt;/P&gt;
&lt;P&gt;Is there a more clever way to use way9(ods tagsets.excelxp) without writing 3 proc reports (Can we write one proc report with BY statement)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; /*Way1*/
proc export data=sashelp.cars (where=(Origin='Asia'))
outfile="/path/carsRRR1.xlsx"
DBMS=xlsx replace;
sheet='Asia';
run;
proc export
data=sashelp.cars (where=(Origin='Europe'))
file="/path/carsRRR1.xlsx"
DBMS=xlsx replace;
sheet='Europe';
run;
proc export
data=sashelp.cars (where=(Origin='USA'))
file="/path/carsRRR1.xlsx"
DBMS=xlsx replace;
sheet='USA';
run;

/*Way2*/
%macro excel_export(origin);
proc export data=sashelp.cars (where=(Origin="&amp;amp;origin."))
outfile="/path/carsRRR2.xlsx"
DBMS=xlsx replace;
sheet="&amp;amp;origin.";
run;
%mend;
%excel_export(Asia)
%excel_export(Europe)
%excel_export(USA)

/*Way3*/
proc sort data=sashelp.cars (keep=origin) out=control nodupkey;
by origin;
run;
proc sql noprint;                              
 select origin into :vector separated by '+'
 from control;
quit;
%put &amp;amp;vector.;

PROC SQL Noprint;
select count(*)  INTO:n 	   
from control
;
QUIT;
%put &amp;amp;n.;


%macro mmacro;
%DO i=1  %TO  &amp;amp;n.;
%let X=%scan(&amp;amp;vector.,&amp;amp;i.,+);
proc export data=sashelp.cars (where=(Origin="&amp;amp;x."))
outfile="/path/carsRRR3.xlsx"
DBMS=xlsx replace;
sheet="&amp;amp;x.";
run;
%end;
%mend;
%mmacro; 


/*Way4*/
/*names of the sheets will be:Bygroup1-Asia,Bygroup1-Europe,Bygroup1-USA*/
proc sort data=sashelp.cars out=cars;
by origin;
run;

ODS excel  file="/path/carsRRR4.xlsx"
options (sheet_interval='bygroup');
proc print data=cars noobs;
by origin;
Run;
ODS excel close;


/*Way5*/
/*Here we used SHEET_NAME option with #BYVAL */
/*Names of sheets will be: Asia,Europe,USA*/
proc sort data=sashelp.cars out=cars;
by origin;
run;

options byline=0;
ods excel file="/path/carsRRR5.xlsx" 
options(sheet_interval='bygroup' sheet_name="#byval1");
proc print data=work.cars  noobs;
by Origin;
run;
ods excel close;
options byline=1;


/*Way6*/
proc sort data=sashelp.cars (keep=origin) out=control  nodupkey;
by origin;
run;

%macro excel_export(origin);
proc export data=sashelp.cars (where=(Origin="&amp;amp;origin."))
outfile="/path/carsRRR6.xlsx"
DBMS=xlsx replace;
sheet="&amp;amp;origin.";
run;
%mend;


data _null_;
set control;
call execute('%nrstr(%excel_export(' !! strip(origin) !! '))');
run;

/*Way7*/
proc sql;
create table control as
select distinct origin
from sashelp.cars;
quit;

%let no_of_org = &amp;amp;sqlobs;
%put &amp;amp;no_of_org;


data _null_; 
set control;
call symput('org'||left(_n_),strip(origin));
run;
%put &amp;amp;org1;
%put &amp;amp;org2;
%put &amp;amp;org3;


%macro export;
%do i=1 %to &amp;amp;no_of_org;  
proc export data=sashelp.cars (where=(Origin="&amp;amp;&amp;amp;org&amp;amp;i"))
outfile="/path/carsRRR7.xlsx"
DBMS=xlsx replace;
sheet="&amp;amp;&amp;amp;org&amp;amp;i";
run;       
%end;
%mend export;
%export;

/*Way8*/

libname carsRRR8 xlsx "/path/carsRRR8.xlsx";
data carsRRR8.asia;
 set sashelp.cars (where=(Origin='Asia'));
run;
data carsRRR8.usa;
 set sashelp.cars (where=(Origin='USA'));
run;
data carsRRR8.Europe;
set sashelp.cars (where=(Origin='Europe'));
run;


/*Way9*/
filename myfile "/path/carsRRR12.XML";
ods tagsets.excelxp body=myfile STYLE=HTMLBLUE 
OPTIONS(SHEET_NAME="Asia" 
        Orientation='Landscape' 
        wraptext = 'YES' 
        sheet_interval='Proc'
        autofilter='ALL'
        FROZEN_HEADERS='YES'
		convert_percentages='YES'
		TITLE_FOOTNOTE_WIDTH='20'
		EMBEDDED_TITLES='YES' 
		EMBEDDED_FOOTNOTES='YES'
		pagebreaks='YES'
		gridlines='YES' 
		PAGE_ORDER_ACROSS='YES'
		contents='NO'
		gridlines='YES');
/*** sheet 1 ***/
title;
proc report data=sashelp.cars(where=(Origin='Asia'));
Run; 
/*** sheet  2 ***/
ods tagsets.excelxp OPTIONS(SHEET_NAME="USA");
title;
proc report data=sashelp.cars(where=(Origin='USA'));
Run; 
/*** sheet  3 ***/
ods tagsets.excelxp  OPTIONS(SHEET_NAME="Europe");
title;
proc report data=sashelp.cars(where=(Origin='Europe'));
Run; 
ODS tagsets.excelxp CLOSE;  
title;
footnote;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 04:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577589#M163669</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-07-30T04:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577602#M163673</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which way do you think is the best?&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Maxim 4: If in Doubt, Do a Test Run.&lt;/P&gt;
&lt;P&gt;Or in short: Try It.&lt;/P&gt;
&lt;P&gt;Either select upon how the result looks like in the target, or what performs best (Maxim 2: Read the Log).&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 05:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/577602#M163673</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T05:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/782660#M249515</link>
      <description>&lt;P&gt;Is it called Excel engine method?&lt;/P&gt;</description>
      <pubDate>Sat, 27 Nov 2021 10:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/782660#M249515</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-11-27T10:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/782662#M249517</link>
      <description>Maybe . I don't know. You can call it whatever you want .</description>
      <pubDate>Sat, 27 Nov 2021 10:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/782662#M249517</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-11-27T10:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: Proc export BY to many excel sheets in same file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/782663#M249518</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=sashelp.cars noprint;
table Origin/out=levels nopercent;
run;

libname x xlsx "c:\temp\want.xlsx";
data _null_;
 set levels;
 call execute(catt('data x.',origin,';set sashelp.cars;if origin="',origin,'";run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Nov 2021 10:47:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-export-BY-to-many-excel-sheets-in-same-file/m-p/782663#M249518</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-11-27T10:47:03Z</dc:date>
    </item>
  </channel>
</rss>

