<?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: Conditional Export to Excel in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738979#M230587</link>
    <description>&lt;P&gt;Let's highlight some of the trees for you.&lt;/P&gt;
&lt;P&gt;The place where you are specifying that you want to attach a file is here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
  FROM=( "me@xxxx.com" )
  TO=( "manager@xxxx.com" )
  CC=( "me@xxxx.com" ) 
  TYPE='TEXT/HTML'
  SUBJECT="Item Step Back Reporting for &amp;amp;repdate3"
  LRECL=32767
  ATTACH=
   ("FILEPATH_&amp;amp;repdate..xlsx"
    content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first figure whether or not you want to attach the file and set a macro variable to either 1 (true) or 0 (false).&amp;nbsp; Then modify that statement with macro code so that it only includes the ATTACH= part when you want to attach a file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
  FROM=( "me@xxxx.com" )
  TO=( "manager@xxxx.com" )
  CC=( "me@xxxx.com" ) 
  TYPE='TEXT/HTML'
  SUBJECT="Item Step Back Reporting for &amp;amp;repdate3"
  LRECL=32767
%if &amp;amp;attach %then %do;
  ATTACH=
   ("FILEPATH_&amp;amp;repdate..xlsx"
    content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )
%end;
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 May 2021 16:10:28 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-04T16:10:28Z</dc:date>
    <item>
      <title>Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738276#M230260</link>
      <description>&lt;P&gt;Happy Friday, Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a scenario wherein I need to send an email every day but only attach a file if there are results.&amp;nbsp; Basically the code monitors a scenario wherein an item that was placed into the 'Ready for report' status for batch upload and then pulled back to an 'In progress' status (where it will not be picked up by the evening batch run).&amp;nbsp; What I'm trying to do is to&amp;nbsp;&lt;EM&gt;always&amp;nbsp;&lt;/EM&gt;send an email but only include an attached Excel file&amp;nbsp;&lt;EM&gt;if there are results.&amp;nbsp;&amp;nbsp;&lt;/EM&gt;This is required to satisfy internal controls/audits showing the report is running but only sending a file when there are items requiring attention.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table ITEM_STEP_BACK as
select 	t1.alert_id
		,t2.status_name as CASE_STATUS
		,t3.form_identifier
		,t3.e_status
		,t7.cd12 as CASE_ITEM_CURRENT_DUE_DATE
		,intck('days',today(),(datepart(t7.cd12))) as DAYS_TO_DUE_DATE
		,scan(scan(t5.note,3,']'),2,'[') length=45 as STEPPED_FROM
		,scan(scan(t5.note,4,']'),2,'[') length=45 as STEPPED_TO
		,datepart(t5.create_date) format=date9. as NOTE_DATE
		,t6.full_name as STEPPED_BACK_BY

from 	rcmr.alerts t1
		,rcmr.acm_md_alert_statuses t2
		,rcmr.acm_forms t3
		,rcmr.acm_alert_audits t4
		,rcmr.acm_audits t5
		,rcmr.acm_users t6
		,rcmr.acm_alert_custom_attributes t7

where 	t1.status_internal_id = t2.status_internal_id
and 	t1.alert_internal_id = t3.alert_internal_id
and 	t1.alert_internal_id = t4.alert_internal_id
and 	t4.audit_internal_id = t5.audit_internal_id
and 	t5.user_internal_id = t6.user_internal_id
and 	t1.alert_custom_attributes_id = t7.alert_custom_attributes_id
and 	t1.alert_type_internal_id in (324, 325, 327, 328)
and 	(t5.create_date &amp;gt;= intnx('days',today(),-45,'b')
and 	t5.create_date &amp;lt; intnx('days',today(),0,'b'))
and 	(t5.note like 'Type: [ITEM], Identifier: [%] - Step changed from [Ready for report] to [In process]'
or	 	t5.note like 'Type: [ITEM], Identifier: [%] - Step changed from [Ready for report] to [Reported externally]'
or 		t5.note like 'Type: [ITEM], Identifier: [%] - Step changed from [In process] to [Reported externally]'
or 		t5.note like 'Type: [ITEM], Identifier: [%] - Step changed from [%] to [Closed as non issue]')
and 	t3.e_status not in ('Ready for report', 'Reported - pending', 'Reported - success')
/*
and 	intck('days',today(),(datepart(t7.cd12))) &amp;lt; 7
*/

order by t3.create_date;

quit;


title "Item Stepped Back"
;

proc print data=item_step_back;
run;


*******************************************;
*  EXPORT ITEM STEP BACK RESULTS TO EXCEL.  ;
*******************************************;

data _null_;
call symput('repdate',put(today(),date9. -L));
call symput('repdate2',put(intnx('days',today(),-1),date9. -L));
call symput('repdate3',put(today(),weekdate29. -L));
call symput('repdate4',put(intnx('days',today(),-1),weekdate29. -L));
run;

%put repdate : &amp;amp;repdate;  *  TODAY'S DATE in DATE9. ALIGNED LEFT  ;
%put repdate2 : &amp;amp;repdate2;  *  YESTERDAY'S DATE in DATE9. ALIGNED LEFT  ;
%put repdate3 : &amp;amp;repdate3;  *  TODAY'S DATE IN WEEKDATE29. ALIGNED LEFT  ;
%put repdate4 : &amp;amp;repdate4;  *  YESTERDAY'S DATE in WEEKDATE29. ALIGNED LEFT  ;


proc export
data=ITEM_STEP_BACK
dbms=xlsx
outfile="FILEPATH_&amp;amp;repdate..xlsx"
replace;
sheet="ITEM_STEP_BACK_&amp;amp;repdate";
run;


*********************************;
*EMAIL RESULTS - ITEM STEP BACK.  ;
*********************************;

filename mail email 
FROM=( "me@xxxx.com" )
TO=( "manager@xxxx.com" )
CC=( "me@xxxx.com" ) 
TYPE='TEXT/HTML'
SUBJECT="Item Step Back Reporting for &amp;amp;repdate3"
LRECL=32767
ATTACH=("FILEPATH_&amp;amp;repdate..xlsx"
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

data _null_;
file mail;
put "&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;";
put "Good Morning,&amp;lt;br&amp;gt;";
put "&amp;lt;br&amp;gt;";
put "See attached files for Items that were stepped back from 'Ready for report' and into another, possible problematic, status.&amp;lt;br&amp;gt;";
put "&amp;lt;br&amp;gt;";
put "THANKS!&amp;lt;br&amp;gt;";
put "&amp;lt;br&amp;gt;";
put "Matt D&amp;lt;br&amp;gt;";
put "&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;";

run;


title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Again, this code &lt;EM&gt;works just fine,&amp;nbsp;&lt;/EM&gt;but I'd like to get to where it is only exporting a file should results exist and attaching it to the email.&amp;nbsp; The email does need to go out&amp;nbsp;&lt;EM&gt;every day, &lt;/EM&gt;but only include the Excel attachment when there are results.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;THANKS!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Apr 2021 20:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738276#M230260</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-04-30T20:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738277#M230261</link>
      <description>&lt;P&gt;Plenty of possibilities — Google is your friend&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See &lt;A href="https://communities.sas.com/t5/SAS-Procedures/Run-macro-only-if-obs-gt-0/td-p/389030" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Run-macro-only-if-obs-gt-0/td-p/389030&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Apr 2021 20:37:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738277#M230261</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-30T20:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738973#M230583</link>
      <description>&lt;P&gt;Thank you for the quick reply,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;.&amp;nbsp;&amp;nbsp;I have Googled this to no end and posting here was something of a last resort.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've reviewed the link you provided, and I'm certain I'm not seeing the forest for the trees, but I cannot find a solution there.&amp;nbsp; Sincerest apologies.&amp;nbsp; I've waited a couple days as sometimes that permits the fog to clear a bit, but to no avail.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any additional suggestions?&amp;nbsp; If it matters, I am working on SAS EG v7.15 HF7.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks so much.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Matt D&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 15:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738973#M230583</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-04T15:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738979#M230587</link>
      <description>&lt;P&gt;Let's highlight some of the trees for you.&lt;/P&gt;
&lt;P&gt;The place where you are specifying that you want to attach a file is here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
  FROM=( "me@xxxx.com" )
  TO=( "manager@xxxx.com" )
  CC=( "me@xxxx.com" ) 
  TYPE='TEXT/HTML'
  SUBJECT="Item Step Back Reporting for &amp;amp;repdate3"
  LRECL=32767
  ATTACH=
   ("FILEPATH_&amp;amp;repdate..xlsx"
    content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So first figure whether or not you want to attach the file and set a macro variable to either 1 (true) or 0 (false).&amp;nbsp; Then modify that statement with macro code so that it only includes the ATTACH= part when you want to attach a file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
  FROM=( "me@xxxx.com" )
  TO=( "manager@xxxx.com" )
  CC=( "me@xxxx.com" ) 
  TYPE='TEXT/HTML'
  SUBJECT="Item Step Back Reporting for &amp;amp;repdate3"
  LRECL=32767
%if &amp;amp;attach %then %do;
  ATTACH=
   ("FILEPATH_&amp;amp;repdate..xlsx"
    content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )
%end;
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 16:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738979#M230587</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-04T16:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738980#M230588</link>
      <description>&lt;P&gt;In that link, it shows how to determine how many observations are in a data set, and then take action if there are 1 or more observations, and do nothing if there are 0 observations in a data set.&lt;/P&gt;</description>
      <pubDate>Tue, 04 May 2021 16:29:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/738980#M230588</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-05-04T16:29:03Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742709#M232374</link>
      <description>&lt;P&gt;Ok.&amp;nbsp; I'm grasping that the determination to attach the file, or not, needs to be done in the statement creating the email.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I do that?&amp;nbsp; That's the entire point of this.&amp;nbsp; I don't get how I need to do that.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 16:52:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742709#M232374</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T16:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742718#M232378</link>
      <description>&lt;P&gt;This piece seems to be better than the select count(t1.alert_id) I was using.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symput('num_recs',put(numrecs,best.));
set my_data nobs=numrecs;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It seems to be providing a '0' when there are no results and a '1' when there are one or more results.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 17:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742718#M232378</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T17:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742720#M232380</link>
      <description>&lt;P&gt;So put the two together:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputX('num_recs',numrecs);
  set my_data nobs=numrecs;
  stop;
run;

....

filename mail email 
  FROM=( "me@xxxx.com" )
  TO=( "manager@xxxx.com" )
  CC=( "me@xxxx.com" ) 
  TYPE='TEXT/HTML'
  SUBJECT="Item Step Back Reporting for &amp;amp;repdate3"
  LRECL=32767
%if (&amp;amp;num_recs &amp;gt; 0) %then %do;
  ATTACH=
   ("FILEPATH_&amp;amp;repdate..xlsx"
    content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
   )
%end;
;

...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 17:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742720#M232380</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-20T17:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742734#M232388</link>
      <description>&lt;P&gt;It's still attaching an Excel file with no records...&amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-05-20 13_53_40-Window.png" style="width: 944px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59624i8D17C4ECBC78E83C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-05-20 13_53_40-Window.png" alt="2021-05-20 13_53_40-Window.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 17:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742734#M232388</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T17:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742737#M232391</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*****************;
*EMAIL RESULTS.  ;
*****************;

filename mail email 
FROM=( "me@XXXX.com" )
TO=( "manager@XXXX.com" )
CC=( "me@XXXX.com" ) 
TYPE='TEXT/HTML'
SUBJECT="EMAIL TITLE for &amp;amp;repdate"
LRECL=32767
%if (&amp;amp;num_recs &amp;gt;0) %then %do;
ATTACH=("FILEPATH_&amp;amp;repdate..xlsx"
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");


data _null_;
file mail;
put "&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;";
put "EMAIL BODY TEXT.&amp;lt;br&amp;gt;";
put "&amp;lt;br&amp;gt;";
put "THANKS!&amp;lt;br&amp;gt;";
put "&amp;lt;br&amp;gt;";
put "Matt D&amp;lt;br&amp;gt;";
put "&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;";

run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 17:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742737#M232391</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T17:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742740#M232394</link>
      <description>&lt;P&gt;You have a %DO without any corresponding %END.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if (&amp;amp;num_recs &amp;gt;0) %then %do;
  ATTACH=("FILEPATH_&amp;amp;repdate..xlsx"
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You need to have defined NUM_RECS before running the FILENAME statement.&lt;/P&gt;
&lt;P&gt;If the code is not inside a macro definition then you must be using a reasonably recent release of SAS for the %IF/%THEN/%DO/%END to work in "open" code.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 18:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742740#M232394</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-20T18:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742741#M232395</link>
      <description>&lt;P&gt;Your code misses a %END.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 18:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742741#M232395</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T18:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742742#M232396</link>
      <description>&lt;P&gt;The %end gives innumerable errors and doesn't run.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 18:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742742#M232396</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T18:09:26Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742744#M232398</link>
      <description>&lt;P&gt;That's because running code with an open %DO often causes your SAS session to become unusable.&lt;/P&gt;
&lt;P&gt;Start a new SAS session.&lt;/P&gt;
&lt;P&gt;You&amp;nbsp;&lt;STRONG&gt;MUST&lt;/STRONG&gt; have a closing %END for each %DO, otherwise the macro processor will (or will not) execute anything you feed it.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 18:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742744#M232398</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T18:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742745#M232399</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: The %IF statement is not valid in open code.
ERROR: Error in the FILENAME statement.
ERROR 23-2: Invalid option name end.
ERROR: Insufficient authorization to access /var/spool/mail/srvsasadm.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 18:13:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742745#M232399</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T18:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742746#M232400</link>
      <description>&lt;P&gt;Above errors are from running this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
FROM=( ".com" )
TO=( ".com"  )
CC=(  ) 
TYPE='TEXT/HTML'
SUBJECT="SUBJECT &amp;amp;repdate"
LRECL=32767
%if (&amp;amp;num_recs &amp;gt;0) %then %do;
ATTACH=("FILEPATH_&amp;amp;repdate..xlsx"
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 18:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742746#M232400</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T18:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742748#M232402</link>
      <description>&lt;P&gt;Closer, but still a NO.&amp;nbsp; It's telling me the %IF and %END statements aren't valid in open code.&amp;nbsp; It's also STILL sending an empty Excel file.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
FROM=( "" )
TO=( "" )
CC=( / ) 
TYPE='TEXT/HTML'
SUBJECT="SUBJECT for &amp;amp;repdate"
LRECL=32767
%if (&amp;amp;num_recs &amp;gt;0) %then %do;
ATTACH=("FILEPATH_&amp;amp;repdate..xlsx"
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
%end;
;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 18:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742748#M232402</guid>
      <dc:creator>hnb_matt_d</dc:creator>
      <dc:date>2021-05-20T18:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742754#M232406</link>
      <description>&lt;P&gt;This means that you have an older SAS version where the "open code" %IF was not possible.&lt;/P&gt;
&lt;P&gt;You need to wrap your code into a macro definition and call the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename mail email 
FROM=( ".com" )
TO=( ".com"  )
CC=(  ) 
TYPE='TEXT/HTML'
SUBJECT="SUBJECT &amp;amp;repdate"
LRECL=32767
%macro cond_attach;
%if (&amp;amp;num_recs &amp;gt;0) %then %do;
ATTACH=("FILEPATH_&amp;amp;repdate..xlsx"
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
%end;
%mend cond_attach;
%cond_attach /* no semicolon here! */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 18:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742754#M232406</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T18:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Export to Excel</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742755#M232407</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/306354"&gt;@hnb_matt_d&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: The %IF statement is not valid in open code.
ERROR: Error in the FILENAME statement.
ERROR 23-2: Invalid option name end.
ERROR: Insufficient authorization to access /var/spool/mail/srvsasadm.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You have a number of options to fix this.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Talk to your computer support staff to understand why you are running such an old version of SAS.&lt;/LI&gt;
&lt;LI&gt;Start using a macro.&lt;/LI&gt;
&lt;LI&gt;Assuming your SAS version is not even older you could use %SYSFUNC(IFC()) to generate the conditional code.
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename email
....
%sysfunc(ifc(&amp;amp;num_recs &amp;gt;0,ATTACH=("FILEPATH_&amp;amp;repdate..xlsx" content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),))
...
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;Conditionally generate a macro variable that contains the text you want to include in the FILENAME statement.
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let attach=;
data _null_;
  set my_data ;
  call symputx('attach'
     ,'ATTACH=("FILEPATH_&amp;amp;repdate..xlsx" content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")'
     );
  stop;
run;

...
filename email
....
&amp;amp;attach 
...
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I leave it as an exercise for you to figure out which you want to use and how to update your code to use.&amp;nbsp; Make sure that the code you create is valid SAS code and it is still valid SAS code once the conditional part is generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 18:30:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-Export-to-Excel/m-p/742755#M232407</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-20T18:30:53Z</dc:date>
    </item>
  </channel>
</rss>

