<?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 Create multiple multi-tab Excel worksheets and email as attachments in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920210#M362428</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I’m using SAS 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal is to have a program to create multiple multi-tab worksheets in Excel.&amp;nbsp; The output should have each worksheet as a department and each worksheet/department could have 0-3 additional tabs (units).&amp;nbsp;&amp;nbsp; l then want to email each worksheet to the respective supervisor.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bonus 1:&amp;nbsp; One unit is a bit rogue (SOLO) and doesn’t have a department but a unit with a supervisor.&amp;nbsp; I can manually do that one if overly complicated.&lt;/P&gt;&lt;P&gt;Bonus 2.&amp;nbsp; A few units/departments have an &amp;amp; in the name. I assume I need to use %SUPERQ but used tranwrd function in my program to replace ‘&amp;amp;’ with ‘and’ to avoid that.&lt;/P&gt;&lt;P&gt;Bonus 3: A few departments have subunits.&amp;nbsp; Those subunits would replace the units.&amp;nbsp; Again, I can do this programmatically in the prep part to replace a subunit with unit if need be.&lt;/P&gt;&lt;P&gt;Bonus 4:&amp;nbsp; Plant managers would also like a copy of the units but if I can get the bulk down, I can probably figure out the code for that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*getting departments in a list and the count*/

proc sql; select distinct department, count(distinct department)  into :dept_list1-  , :number_deps from employees where department ^=''; quit;

/*creating an excel sheet for each instance of a department*/
filename target '[filepath]\departments.xlsx';
ods excel file=target options (frozen_headers='on' embedded_titles='on'  autofilter="2" flow = 'table');



%macro helpme;
  ods results off;  
%do i=1 %to &amp;amp;number_deps;
  ods excel file="[filepath]\&amp;amp;&amp;amp;dept_list&amp;amp;i...xlsx";

    ods excel options(sheet_name="Dept - &amp;amp;&amp;amp;dept_list&amp;amp;i" flow = "tables" );
    proc print data=employees noobs; 
	var Employee_Identification department_name department;
	where department = "&amp;amp;&amp;amp;dept_list&amp;amp;i"; run;

  %end;
  ods excel close;

/*Need help here - can I add tab(s)/unit(s) to the existing &amp;amp;&amp;amp;dept_list&amp;amp;i worksheets the units where &lt;BR /&gt;the department = &amp;amp;&amp;amp;deptlist&amp;amp;i?  */
%mend helpme;

%helpme


/*prepping for the email macro*/

/*list of supervisors*/

proc sql; select distinct employee_identification, email, Department into :SupervisorID1-, :SupervisorEmail1-, :SupervisorDepartment1- from employees
where departmental_supervisor = 'x';
quit;



proc options group=email; run;


options 
emailsys=smtp 
emailhost = [myhostname]
emailid = "[my email]"
emailport = 25;

/*then I am hoping with those excel files I can attach them to the correct supervisor....*/
/*this likely needs a lot of work*/

%macro email;

%do i = 1 %to %number_deps;

filename msg email ;
data _null_;
file mailbox EMAIL to="&amp;amp;&amp;amp;supervisoremail&amp;amp;i"
 					from="myemail@company.com"
					subject = "Employee List ";
		put "Hello,";
		put "This is the report of your employees";
		put "with an attached file";
		put;
		put 'Regardss';
		put
		put 'My name'

		FILENAME MailBox EMAIL ATTACH="[filepath/]&amp;amp;&amp;amp;SupervisorDepartment1&amp;amp;i";
		run;

%end;

%mend email;&lt;BR /&gt;&lt;BR /&gt;%email&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Wed, 13 Mar 2024 22:32:05 GMT</pubDate>
    <dc:creator>denperp</dc:creator>
    <dc:date>2024-03-13T22:32:05Z</dc:date>
    <item>
      <title>Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920210#M362428</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I’m using SAS 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal is to have a program to create multiple multi-tab worksheets in Excel.&amp;nbsp; The output should have each worksheet as a department and each worksheet/department could have 0-3 additional tabs (units).&amp;nbsp;&amp;nbsp; l then want to email each worksheet to the respective supervisor.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bonus 1:&amp;nbsp; One unit is a bit rogue (SOLO) and doesn’t have a department but a unit with a supervisor.&amp;nbsp; I can manually do that one if overly complicated.&lt;/P&gt;&lt;P&gt;Bonus 2.&amp;nbsp; A few units/departments have an &amp;amp; in the name. I assume I need to use %SUPERQ but used tranwrd function in my program to replace ‘&amp;amp;’ with ‘and’ to avoid that.&lt;/P&gt;&lt;P&gt;Bonus 3: A few departments have subunits.&amp;nbsp; Those subunits would replace the units.&amp;nbsp; Again, I can do this programmatically in the prep part to replace a subunit with unit if need be.&lt;/P&gt;&lt;P&gt;Bonus 4:&amp;nbsp; Plant managers would also like a copy of the units but if I can get the bulk down, I can probably figure out the code for that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*getting departments in a list and the count*/

proc sql; select distinct department, count(distinct department)  into :dept_list1-  , :number_deps from employees where department ^=''; quit;

/*creating an excel sheet for each instance of a department*/
filename target '[filepath]\departments.xlsx';
ods excel file=target options (frozen_headers='on' embedded_titles='on'  autofilter="2" flow = 'table');



%macro helpme;
  ods results off;  
%do i=1 %to &amp;amp;number_deps;
  ods excel file="[filepath]\&amp;amp;&amp;amp;dept_list&amp;amp;i...xlsx";

    ods excel options(sheet_name="Dept - &amp;amp;&amp;amp;dept_list&amp;amp;i" flow = "tables" );
    proc print data=employees noobs; 
	var Employee_Identification department_name department;
	where department = "&amp;amp;&amp;amp;dept_list&amp;amp;i"; run;

  %end;
  ods excel close;

/*Need help here - can I add tab(s)/unit(s) to the existing &amp;amp;&amp;amp;dept_list&amp;amp;i worksheets the units where &lt;BR /&gt;the department = &amp;amp;&amp;amp;deptlist&amp;amp;i?  */
%mend helpme;

%helpme


/*prepping for the email macro*/

/*list of supervisors*/

proc sql; select distinct employee_identification, email, Department into :SupervisorID1-, :SupervisorEmail1-, :SupervisorDepartment1- from employees
where departmental_supervisor = 'x';
quit;



proc options group=email; run;


options 
emailsys=smtp 
emailhost = [myhostname]
emailid = "[my email]"
emailport = 25;

/*then I am hoping with those excel files I can attach them to the correct supervisor....*/
/*this likely needs a lot of work*/

%macro email;

%do i = 1 %to %number_deps;

filename msg email ;
data _null_;
file mailbox EMAIL to="&amp;amp;&amp;amp;supervisoremail&amp;amp;i"
 					from="myemail@company.com"
					subject = "Employee List ";
		put "Hello,";
		put "This is the report of your employees";
		put "with an attached file";
		put;
		put 'Regardss';
		put
		put 'My name'

		FILENAME MailBox EMAIL ATTACH="[filepath/]&amp;amp;&amp;amp;SupervisorDepartment1&amp;amp;i";
		run;

%end;

%mend email;&lt;BR /&gt;&lt;BR /&gt;%email&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 22:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920210#M362428</guid>
      <dc:creator>denperp</dc:creator>
      <dc:date>2024-03-13T22:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920243#M362437</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you clarify something for me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You want one excel workbook named departments, and different tabs for each department inside that workbook.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is that correct?&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 05:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920243#M362437</guid>
      <dc:creator>Mazi</dc:creator>
      <dc:date>2024-03-14T05:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920286#M362453</link>
      <description>&lt;P&gt;Hi, thanks for looking at this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;No, each department will be its own workbook and a tab with the department name.&amp;nbsp; I have this part down for the most part.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In the example data - there are 23 departments.&amp;nbsp; The %macro helpme creates 23 workbooks with the name DEPT (BCN, BRDSTCK, CHZPUFF, CRACK, etc).&amp;nbsp; I added the prefix 'DEPT- ' to each department on the tabs (BCN, DEPT-BRDSTCK, DEPT-CHZPUFF, DEPT-CRACK, etc). &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am hoping to do is put additional tabs in those workbooks based on the units [Unit].&amp;nbsp; In my example file the department CHZPUFF has two units -CHZPFLS and CHZPFRG so those units would be their own tabs in the CHZPUFF workbook.&amp;nbsp; The TST department has three units (TST-Jam, TST-Butter, TST-PLN).&amp;nbsp; Many departments have only one unit, some have none, some have two or three, but I still want those units to be a separate tab because the employees are different.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, each workbook would be emailed to the supervisor of the department.&amp;nbsp; I hope that helps clarify?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm reattaching the workbook.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 13:41:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920286#M362453</guid>
      <dc:creator>denperp</dc:creator>
      <dc:date>2024-03-14T13:41:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920298#M362457</link>
      <description>&lt;P&gt;You are most of the way there.&lt;/P&gt;
&lt;P&gt;In this part:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%do i=1 %to &amp;amp;number_deps;
  ods excel file="[filepath]\&amp;amp;&amp;amp;dept_list&amp;amp;i...xlsx";

    ods excel options(sheet_name="Dept - &amp;amp;&amp;amp;dept_list&amp;amp;i" flow = "tables" );
    proc print data=employees noobs; 
	var Employee_Identification department_name department;
	where department = "&amp;amp;&amp;amp;dept_list&amp;amp;i"; run;

  %end;
  ods excel close;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;you would add additional ODS EXCEL statements with the Sheet_name and the procedure to generate the output before the %end; . If the sub-unit names are in another variable you could use the Sheet_interval="By Group" to get a separate tab for each sub-unit and use By group processing in the procedure(s). Might be a&amp;nbsp; good idea to sort the data by the department and sub-unit variables.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 15:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920298#M362457</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-14T15:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920304#M362461</link>
      <description>&lt;P&gt;You have two separate problems.&lt;/P&gt;
&lt;P&gt;1) How to create multiple XSLX Workbooks that each have multiple worksheets.&amp;nbsp; (A TAB in a WORKBOOK is a WORKSHEET.)&lt;/P&gt;
&lt;P&gt;2) How to send an email with an attachment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use macro code to help with this I would recommend making a macro that takes as an input parameter the name of the department.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro email_report(department);
....
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you mentioned that the department names might not be macro friendly just assume the user has passed in the department value as a string literal.&amp;nbsp; So the calls would look something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%email_report(department='R&amp;amp;D')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And in the body of the macro you would use the department parameter something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where department=&amp;amp;department ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;that way and &amp;amp; or % characters in the name will be ignored by the macro processor since they are inside a string literal bounded by single quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So assuming you have an input dataset named EMPLOYEES then it should be sorted by DEPARTMENT so that you can use it to generate one call to the macro per department with a simple data step like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set employees;
  by department;
  if first.department;
  call execute(cats('%nrstr(%email_report)(department=',quote(trim(department),"'"),')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you just need to answer the first two questions to decide how to build the actual macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first one just use ODS features to generate a separate worksheet for each value of the BY group.&amp;nbsp; Look up&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/odsug/p09n5pw9ol0897n1qe04zeur27rv.htm" target="_self"&gt;Sheet_interval&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the second one just look-up how to attach an existing file to an email.&amp;nbsp; Look at the&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsglobal/n0ig2krarrz6vtn1aw9zzvtez4qo.htm#p0ylwn2vim3g3en1duw1n51u741z" target="_self"&gt;attach= option of EMAIL fileref engine&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to where to send the email, if that information is in the EMPLOYEE dataset then use it to generate the code that sets the TO address of the email.&amp;nbsp; Perhaps by storing the value into a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 15:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920304#M362461</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-14T15:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920518#M362531</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp; I'm getting closer and I think I need to prep my data a bit more.&amp;nbsp; One issue I am having is some departments have units and some don't - so while it may be a bit clunkier it should work to do this in 2 steps - one with just departments and one with the units.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2024 21:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920518#M362531</guid>
      <dc:creator>denperp</dc:creator>
      <dc:date>2024-03-15T21:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920519#M362532</link>
      <description>&lt;P&gt;Thank you and for those references!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Mar 2024 21:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920519#M362532</guid>
      <dc:creator>denperp</dc:creator>
      <dc:date>2024-03-15T21:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple multi-tab Excel worksheets and email as attachments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920521#M362534</link>
      <description>&lt;P&gt;What is the impact of not having units?&lt;/P&gt;
&lt;P&gt;Is UNIT the thing that gets split into different worksheets?&lt;/P&gt;
&lt;P&gt;If so then set UNIT to a constant for the departments that do not have units.&amp;nbsp; Perhaps something like ALL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Department Unit
DEPT_ONE UNIT_ONE
DEPT_ONE UNIT_TWO
DEPT_TWO ALL&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Mar 2024 21:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-multi-tab-Excel-worksheets-and-email-as/m-p/920521#M362534</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-15T21:29:36Z</dc:date>
    </item>
  </channel>
</rss>

