<?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: Using macros in a do loop to send a Notification EMAIL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565709#M158869</link>
    <description>&lt;P&gt;basically this idea.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems you already have your data in a dataset since you're using that to create macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, change your macro program to send an email provided the body, subject and email parameters.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro emailClients(email=, subject=, body=);

....code ...

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, like in my example linked, call that macro from your data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set all;

str = catt('%emailClient(email=', email, ', subject=', subject, ',body=', body, ');');

if run='Y' then call execute(str);

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I usually keep it in a data set so I can check that the str variable is created correctly and once that's verified, I change it to a data _null_ step so that it's cleaner.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jun 2019 22:53:25 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-12T22:53:25Z</dc:date>
    <item>
      <title>Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565698#M158861</link>
      <description>&lt;P&gt;Hi, I am have a program that is supposed to send out different email messages based on whether a certain condition is met. I have created strings of all my variables to feed them through a do loop to send out each individual email. I have successfully created the string of variables, but can't seem to figure out how to call them in my email code. I am not sure whether I can call macro variables using a put statement in another macro. I have my code below, any help is much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select SUBJECT, EMAIL, BODY1, count(SUBJECT)&lt;BR /&gt;into&lt;BR /&gt;:SUBJECT separated by '|',&lt;BR /&gt;:EMAIL separated by '|',&lt;BR /&gt;:BODY1 separated by '|',&lt;/P&gt;&lt;P&gt;:n&lt;BR /&gt;from all;&lt;BR /&gt;quit;&lt;BR /&gt;%put subject=&amp;amp;subject;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;options emailsys=XX emailhost='XXX.XXXX.com' emailauthprotocol=none emailid="XXXXXXXXXXXXXX.com" ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;options mprint;&lt;BR /&gt;%macro email;&lt;BR /&gt;%do i=1 %to &amp;amp;n;&lt;BR /&gt;%let SUBJECT=%scan(&amp;amp;SUBJECT,&amp;amp;i,'|');&lt;BR /&gt;%let EMAIL=%scan(&amp;amp;EMAIL,&amp;amp;i,'|');&lt;BR /&gt;%let BODY1=%scan(&amp;amp;BODY1,&amp;amp;i,'|');&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro sendemail;&lt;BR /&gt;*write an email;&lt;BR /&gt;filename outbox email;&lt;BR /&gt;data _null_;&lt;BR /&gt;file outbox&lt;BR /&gt;to=("XXXXXX.com")&lt;BR /&gt;&lt;BR /&gt;subject="&amp;amp;EMAIL";&lt;/P&gt;&lt;P&gt;put "&amp;amp;body1";&lt;BR /&gt;put ' ';&lt;BR /&gt;put 'SUBJECT:&amp;amp;SUBJECT';&lt;/P&gt;&lt;P&gt;put ' ';&lt;BR /&gt;put 'Thanks,';&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;filename outbox clear;&lt;/P&gt;&lt;P&gt;%mend;&lt;BR /&gt;%sendemail;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%email;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 21:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565698#M158861</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-12T21:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565699#M158862</link>
      <description>My suggestion would be to redesign your process.&lt;BR /&gt;Create a macro to send the email with the parameters needed, driven from the data in your all data set. Call the macro using CALL EXECUTE instead of looping, which makes conditional logic much easier.</description>
      <pubDate>Wed, 12 Jun 2019 22:05:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565699#M158862</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-12T22:05:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565707#M158867</link>
      <description>&lt;P&gt;While I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;that you are using more macro language than I would, you could keep your current program.&amp;nbsp; However, there is a laundry list of issues to consider.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If you were to hard-code your logic (two sets of FILENAME statements and DATA _null_ steps) using hard-coded values with zero macro language, would the program send out two emails as planned or would it somehow (perhaps the FILENAME statements) fail?&lt;/LI&gt;
&lt;LI&gt;You have a macro variable reference ('SUBJECT:&amp;amp;SUBJECT') within single quotes.&amp;nbsp; Those need to be double quotes for the macro variable reference to resolve.&lt;/LI&gt;
&lt;LI&gt;The %SCAN functions are using the wrong third parameter:&amp;nbsp; '|'&amp;nbsp; Instead, it should be:&amp;nbsp; |&amp;nbsp; &amp;nbsp; Macro language does not need quotes to indicate that it is processing a character string.&amp;nbsp; Using the quotes means that both single quotes and pipes are delimiters.&amp;nbsp; So if any of your SUBJECT strings contain single quotes, the logic will be wrong.&lt;/LI&gt;
&lt;LI&gt;Defining a macro within the definition of another macro is bad practice.&amp;nbsp; You can call one macro from another, but keep the definitions separate.&lt;/LI&gt;
&lt;LI&gt;You replace your macro variables (&amp;amp;SUBJECT, &amp;amp;EMAIL, and &amp;amp;BODY1) with new values inside your %DO loop.&amp;nbsp; So after the first iteration, they are all truncated and contain only one varlue.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Beyond that, you didn't tell us what went wrong.&amp;nbsp; Why do you say it isn't working?&amp;nbsp; What did the log show?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 22:40:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565707#M158867</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-12T22:40:49Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565708#M158868</link>
      <description>&lt;P&gt;Hi Reeza, I'm not exactly sure what you mean, you could write out a sample of what you mean? Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 22:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565708#M158868</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-12T22:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565709#M158869</link>
      <description>&lt;P&gt;basically this idea.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems you already have your data in a dataset since you're using that to create macro variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, change your macro program to send an email provided the body, subject and email parameters.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro emailClients(email=, subject=, body=);

....code ...

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, like in my example linked, call that macro from your data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set all;

str = catt('%emailClient(email=', email, ', subject=', subject, ',body=', body, ');');

if run='Y' then call execute(str);

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I usually keep it in a data set so I can check that the str variable is created correctly and once that's verified, I change it to a data _null_ step so that it's cleaner.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 22:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565709#M158869</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-12T22:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565711#M158870</link>
      <description>&lt;P&gt;So when the macros are not used, I can successfully send an email, but it will only send an email for the last subject in the dataset, which is why I was trying to create a do-loop to get separate emails sent for each subject.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried removing the quotations for '|' but it gave me an error, I have checked that the strings for all those variables were successfully created, so I think this part of the code is working ok.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What ends up happening though, is the multiple emails are sent correctly, but they only include the text I have written out in the email, none of the actual macro values are recognized. Only the very first email is correct and has all the information, so it is something with my do loop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EMAIL EXAMPLE:&lt;/P&gt;&lt;P&gt;subject= (no subject shows up)&lt;/P&gt;&lt;P&gt;''&lt;/P&gt;&lt;P&gt;''&lt;/P&gt;&lt;P&gt;SUBJECT:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 23:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565711#M158870</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-12T23:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565713#M158871</link>
      <description>&lt;P&gt;Hi Reeza, I see what you mean. The only reason I think this might not work for my specific program is because the emails are generated based on certain conditions. So I won't know when one email is generated over another. Which is why I thought it would be easier to just create a string of each variable and just run the do loop through them (because the email body/subject/ etc might be different) I hope that makes sense, thank you again for all the help on this!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 23:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565713#M158871</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-12T23:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565716#M158872</link>
      <description>&lt;P&gt;It seems like my do loop is not running past n=1 for some reason&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 23:26:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565716#M158872</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-12T23:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565724#M158876</link>
      <description>&lt;P&gt;Perhaps this will give you some ideas?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/sendmail.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/sendmail.sas&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 01:42:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565724#M158876</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-06-13T01:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565727#M158879</link>
      <description>That's related to my original point #5.  Before the loop begins, &amp;amp;SUBJECT (for example) contains a set of values.  As the loop begins (&amp;amp;n=1), &amp;amp;SUBJECT gets replaced using the %SCAN function so it now contains only the first item on the original list.  All 5 points are important.  This shows why point #5 makes a difference.</description>
      <pubDate>Thu, 13 Jun 2019 02:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565727#M158879</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-13T02:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565743#M158887</link>
      <description>&lt;P&gt;I see, what would be the alternative though? Because I thought using the scan function was necessary to go through the string of values that I created for each variable.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 04:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565743#M158887</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-13T04:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565750#M158892</link>
      <description>&lt;P&gt;First, use the "little running man" button for posting code, so we don't get smileys and scrambled code formatting.&lt;/P&gt;
&lt;P&gt;Second, NEVER define macros inside each other. This serves NO purpose at all, as all macros are defined in the &lt;EM&gt;global&lt;/EM&gt; symbol table, and only clutters up the definition of the outside macro, making it less readable and maintainable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third, there's no need to intermediately store lists in macro variables from a dataset when you can directly execute your code from that dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sendemail(email=,body1=,subject=);
*write an email;
filename outbox email;
data _null_;
file outbox
  to=("XXXXXX.com")
  subject="&amp;amp;EMAIL"
;
put "&amp;amp;body1";
put ' ';
put "SUBJECT:&amp;amp;SUBJECT";
put ' ';
put 'Thanks,';
run;
filename outbox clear;
%mend;

data _null_;
set all;
call execute(cats('%nrstr(%sendmail(email=',email,',body1=',body1,',subject=',subject,'))'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here, I only used the macro definition to make the call execute shorter. In RL, I usually put the code right into the data step like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set all;
call execute('
filename outbox email;
data _null_;
file outbox
  to=("XXXXXX.com")
  subject="' !! EMAIL !! '"
;
put "' !! strip(body1) !!'";
put " ";
put "SUBJECT:' !! strip(SUBJECT) !! '";
put " ";
put "Thanks,";
run;
filename outbox clear;
');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the subject would not have worked in your original code because of the single quotes.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 06:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565750#M158892</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-13T06:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565751#M158893</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Hi, I am have a program that is supposed to send out different email messages based on whether a certain condition is met.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You don't really go into enough detail on this.&amp;nbsp; What condition?&amp;nbsp; And what's supposed to happen when that condition is met?&amp;nbsp; Does the Subject change?&amp;nbsp; The To: list change?&amp;nbsp; Is the email body the same, just the subject is different?&amp;nbsp; You know you can put more than one address in the To: list, right?&amp;nbsp; You don't have to send separate emails one at a time.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In addition to my previous sendmail link, perhaps these will help.&amp;nbsp; Read the macro header for use cases:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop.sas&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/loop_control.sas&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For example, with loop_control, you could create a metadata dataset with your To, Subject, Body, etc, plus some filtering condition.&amp;nbsp; Then call your child macro, filtering your control table with a where clause.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 06:26:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565751#M158893</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-06-13T06:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565786#M158905</link>
      <description>The alternative?  &lt;BR /&gt;&lt;BR /&gt;Why replace &amp;amp;SUBJECT?&lt;BR /&gt;&lt;BR /&gt;Create a new macro variable:&lt;BR /&gt;&lt;BR /&gt;%let subject2 = %scan(&amp;amp;subject, &amp;amp;n, |) ;&lt;BR /&gt;&lt;BR /&gt;Use the new macro variable within %email.&lt;BR /&gt;&lt;BR /&gt;Same idea for the remaining two macro variables.</description>
      <pubDate>Thu, 13 Jun 2019 09:48:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565786#M158905</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-13T09:48:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565792#M158908</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139341"&gt;@kmardinian&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your problem is caused in the do loop, in this construct:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;		%let SUBJECT=%scan(&amp;amp;SUBJECT,&amp;amp;i,'|');
		%let EMAIL=%scan(&amp;amp;EMAIL,&amp;amp;i,'|');
		%let BODY1=%scan(&amp;amp;BODY1,&amp;amp;i,'|');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are reusing the variable name, so first time the loop is running, you take the first string out of SUBJECT and assign it to SUBJECT again, so there is no second value for the next loop run. Same with EMAIL and BODY1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use another name in the loop, and it will work. I removed the inner macro, because there is no macro code in it, so it is not needed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options emailsys=XX emailhost='XXX.XXXX.com' emailauthprotocol=none emailid="XXXXXXXXXXXXXX.com" ;
options mprint;

proc sql noprint;
	select SUBJECT, EMAIL, BODY1, count(SUBJECT)
	into
		:SUBJECT separated by '|',
		:EMAIL separated by '|',
		:BODY1 separated by '|',
		:n
from all;
quit;
%put subject=&amp;amp;subject;

%macro email;
	%do i=1 %to &amp;amp;n;
		%let &lt;FONT color="#FF0000"&gt;thisSUBJECT&lt;/FONT&gt;=%scan(&amp;amp;SUBJECT,&amp;amp;i,|);
		%let &lt;FONT color="#FF0000"&gt;thisEMAIL&lt;/FONT&gt;=%scan(&amp;amp;EMAIL,&amp;amp;i,|);
		%let &lt;FONT color="#FF0000"&gt;thisBODY1&lt;/FONT&gt;=%scan(&amp;amp;BODY1,&amp;amp;i,|);

		*write an email;
		filename outbox email;
		data _null_;
			file outbox
			to=("XXXXXX.com")

			subject="&lt;FONT color="#FF0000"&gt;&amp;amp;thisEMAIL&lt;/FONT&gt;";

			put "&lt;FONT color="#FF0000"&gt;&amp;amp;thisBODY1&lt;/FONT&gt;";
			put ' ';
			put 'SUBJECT:&lt;FONT color="#FF0000"&gt;&amp;amp;thisSUBJECT&lt;/FONT&gt;';

			put ' ';
			put 'Thanks,';

		run;
		filename outbox clear;
%end;
%mend;
%email;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2019 10:14:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565792#M158908</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-06-13T10:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565802#M158909</link>
      <description>&lt;P&gt;Once you've gotten that far, you might as well remove the new macro variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro email;
	%do i=1 %to &amp;amp;n;
		*write an email;
		filename outbox email;
		data _null_;
			file outbox
			to=("XXXXXX.com")

			subject="%scan(&amp;amp;EMAIL, &amp;amp;i, |)";

			put "%scan(&amp;amp;BODY1, &amp;amp;i, |)";
			put ' ';
			put 'SUBJECT:%scan(&amp;amp;SUBJECT, &amp;amp;i, |)';

			put ' ';
			put 'Thanks,';

		run;
		filename outbox clear;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;An important point:&amp;nbsp; the original post represents both a question on an application as well as a learning curve for macro language.&amp;nbsp; So add to the body of knowledge one step at a time.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 11:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565802#M158909</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-13T11:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using macros in a do loop to send a Notification EMAIL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565909#M158946</link>
      <description>&lt;P&gt;Thank you, that worked perfectly and exactly how I needed!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you to everyone for their help on this. I need to keep working on the proper way of coding macros...still learning.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 16:14:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-macros-in-a-do-loop-to-send-a-Notification-EMAIL/m-p/565909#M158946</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2019-06-13T16:14:54Z</dc:date>
    </item>
  </channel>
</rss>

