<?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: Generate an automatic email after the sql query condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/901016#M356093</link>
    <description>&lt;P&gt;Thanks for your suggestions&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The below one works.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) into :record_cnt
 from table1
 where id not in (select id from table2);
%let count=&amp;amp;record_cnt;
quit;

%macro test;
%if &amp;amp;count. &amp;gt; 0 %then %do;
filename msg email 
 to="leademail@company.com"
 from="myemail@company.com"
 subject = "Alert: New Record Found";

data _null_;
 file msg;
 put "Hi,";
 put "%trim(&amp;amp;count.) new records found in the dataset. Please take action.";
 %end;
run;
%mend;
%test&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Nov 2023 07:48:52 GMT</pubDate>
    <dc:creator>Sathish_jammy</dc:creator>
    <dc:date>2023-11-01T07:48:52Z</dc:date>
    <item>
      <title>Generate an automatic email after the sql query condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/900957#M356067</link>
      <description>&lt;P&gt;I have to run the below query weekly once. If the record_cnt &amp;gt; 0 then it needs to send an automatic email to lead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) into :record_cnt
 from table1 where id not in (select id from table2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have tried the below code, however it not worked well.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null;
%if &amp;amp;record_cnt &amp;gt; 0 %then %do;
filename msg email 
 to="lead@core.com"
 from="replies-disabled@company.com"
 subject = "New record_found";

data _null_;
file msg;
 put "%trim(&amp;amp;record_cnt.) record found.";
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kindly suggest the appropriate solution,&lt;/P&gt;
&lt;P&gt;Thanks, in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 20:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/900957#M356067</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2023-10-31T20:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Generate an automatic email after the sql query condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/900964#M356070</link>
      <description>&lt;P&gt;Make sure SMTP e-mail server is set up!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then look here:&lt;/P&gt;
&lt;UL class="lia-list-style-type-square"&gt;
&lt;LI&gt;Home &amp;gt; SAS Communities Library &amp;gt; How to send email using SAS?&lt;BR /&gt;Started ‎06-10-2021 | Modified ‎08-04-2022&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-send-email-using-SAS/ta-p/746523" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-send-email-using-SAS/ta-p/746523&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;How to use Gmail to send a message from a SAS program?&lt;BR /&gt;By Chris Hemedinger on The SAS Dummy July 31, 2013&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/sasdummy/2013/07/31/gmail-from-sas-program/" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/sasdummy/2013/07/31/gmail-from-sas-program/&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Then do something like this, but write to mail and not to print destination!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET record_cnt=7;

%IF &amp;amp;record_cnt.&amp;gt;0 %THEN %DO;
data _null_;
file print;
 record_cnt=strip(&amp;amp;record_cnt.);
 put record_cnt 'record found.';
run;
%END;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 20:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/900964#M356070</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-10-31T20:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Generate an automatic email after the sql query condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/900985#M356080</link>
      <description>&lt;P&gt;What do you mean by "not worked well"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the code you shared make sure the Proc SQL ends with a QUIT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first "data _null_;" statement doesn't make sense and should get removed.&lt;/P&gt;
&lt;P&gt;I assume the missing macro %end statement just got "cut off" when you copy/pasted your code?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1698791666009.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89289i51A8AF264A5E573C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1698791666009.png" alt="Patrick_0-1698791666009.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 22:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/900985#M356080</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-31T22:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Generate an automatic email after the sql query condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/901016#M356093</link>
      <description>&lt;P&gt;Thanks for your suggestions&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The below one works.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) into :record_cnt
 from table1
 where id not in (select id from table2);
%let count=&amp;amp;record_cnt;
quit;

%macro test;
%if &amp;amp;count. &amp;gt; 0 %then %do;
filename msg email 
 to="leademail@company.com"
 from="myemail@company.com"
 subject = "Alert: New Record Found";

data _null_;
 file msg;
 put "Hi,";
 put "%trim(&amp;amp;count.) new records found in the dataset. Please take action.";
 %end;
run;
%mend;
%test&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 07:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/901016#M356093</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2023-11-01T07:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: Generate an automatic email after the sql query condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/901024#M356095</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215282"&gt;@Sathish_jammy&lt;/a&gt;&amp;nbsp;I strongly recommend to amend your code as per the highlighted lines below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your condition &amp;amp;count&amp;gt;0 is false then your macro will currently still generate a run; statement.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1698826830000.png" style="width: 625px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89301i07A657E20EE2BDFC/image-dimensions/625x367?v=v2" width="625" height="367" role="button" title="Patrick_0-1698826830000.png" alt="Patrick_0-1698826830000.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for the SQL the following code would suffice and populate macro var &amp;amp;record_cnt the same as your code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select count(*) into :record_cnt trimmed
    from table1
      where id not in (select id from table2);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 08:25:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-an-automatic-email-after-the-sql-query-condition/m-p/901024#M356095</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-01T08:25:30Z</dc:date>
    </item>
  </channel>
</rss>

