<?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: How to create global variables in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789043#M252428</link>
    <description>&lt;P&gt;I don't think global MACRO variables are needed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How are you planning to create the email?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are using a data step to create the email then just leave the data in the dataset variables and use the dataset directly in the data step that writes the email. without needing to convert your dataset variables into text strings to store into macro variables.&amp;nbsp; With email directives you should be able to write all of the emails in one data step.&amp;nbsp;&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsglobal/n0ig2krarrz6vtn1aw9zzvtez4qo.htm#p048cb9obhxem4n1fug5qu9pze1n" target="_blank"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsglobal/n0ig2krarrz6vtn1aw9zzvtez4qo.htm#p048cb9obhxem4n1fug5qu9pze1n&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise create a macro with input parameters that match the variables in your dataset.&amp;nbsp; Then use a data step to generate one call to the macro for each observation in the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro email
(bank_id
,branch_id
,account_number
);
.... details of how to send an email for this account ...
%mend;

filename code temp;
data _null_;
   set have;
  file code;
  put '%email(' bank_id= ',' branch_id= ',' account_number= ')';
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;So the text file generated by the data _null_ step and executed by the %INCLUDE statement will look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%email(Bank_ID=20 ,Branch_ID=1 ,Account_Number=1234567)
%email(Bank_ID=20 ,Branch_ID=112 ,Account_Number=3224334)&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 08 Jan 2022 15:50:49 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-01-08T15:50:49Z</dc:date>
    <item>
      <title>How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789042#M252427</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I have the following final table (every day the table contents change):&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Bank_ID&lt;/TD&gt;&lt;TD&gt;Branch_ID&lt;/TD&gt;&lt;TD&gt;Account_Number&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1234567&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;112&lt;/TD&gt;&lt;TD&gt;3224334&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;1234567&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;2344523&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;1234567&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to make a loop that will run as the number of records in the table.&lt;/P&gt;&lt;P&gt;Each run will define all the variables in the table as global variables with the values ​​of the relevant record.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;According to the example,&amp;nbsp;the loop will have 5 runs.&lt;/P&gt;&lt;P&gt;Example of a first run:&lt;/P&gt;&lt;P&gt;Bank_ID=20&lt;/P&gt;&lt;P&gt;Branch_ID=1&lt;/P&gt;&lt;P&gt;Account_Number=1234567&lt;/P&gt;&lt;P&gt;Example of a second run:&lt;/P&gt;&lt;P&gt;Bank_ID=20&lt;/P&gt;&lt;P&gt;Branch_ID=112&lt;/P&gt;&lt;P&gt;Account_Number=3224334&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At each run, I will take the value of the global variables for sending an email.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would appreciate your help.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 15:17:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789042#M252427</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2022-01-08T15:17:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789043#M252428</link>
      <description>&lt;P&gt;I don't think global MACRO variables are needed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How are you planning to create the email?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are using a data step to create the email then just leave the data in the dataset variables and use the dataset directly in the data step that writes the email. without needing to convert your dataset variables into text strings to store into macro variables.&amp;nbsp; With email directives you should be able to write all of the emails in one data step.&amp;nbsp;&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsglobal/n0ig2krarrz6vtn1aw9zzvtez4qo.htm#p048cb9obhxem4n1fug5qu9pze1n" target="_blank"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsglobal/n0ig2krarrz6vtn1aw9zzvtez4qo.htm#p048cb9obhxem4n1fug5qu9pze1n&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise create a macro with input parameters that match the variables in your dataset.&amp;nbsp; Then use a data step to generate one call to the macro for each observation in the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro email
(bank_id
,branch_id
,account_number
);
.... details of how to send an email for this account ...
%mend;

filename code temp;
data _null_;
   set have;
  file code;
  put '%email(' bank_id= ',' branch_id= ',' account_number= ')';
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;So the text file generated by the data _null_ step and executed by the %INCLUDE statement will look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%email(Bank_ID=20 ,Branch_ID=1 ,Account_Number=1234567)
%email(Bank_ID=20 ,Branch_ID=112 ,Account_Number=3224334)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Jan 2022 15:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789043#M252428</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-08T15:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789065#M252445</link>
      <description>&lt;P&gt;Look into CALL EXECUTE instead IMO. Creating 3*# of loop of macro variables + 1 seems cumbersome and it's a PITA to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: here are some references that may be helpful for you.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Tutorial on converting a working program to a macro&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&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;&lt;STRONG&gt;Examples of common macro usage&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 18:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789065#M252445</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-08T18:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789084#M252459</link>
      <description>&lt;P&gt;I'm sorry but I still do not know how to start.&lt;BR /&gt;I will simplify my request, I have a SAS table that I want to define 3 global variables whose value varies in each record.&lt;BR /&gt;The amount of records and their value changes daily.&lt;BR /&gt;In the case of the example I would like to use the 3 variable values ​​in each round to sending an email according to the following example:&lt;/P&gt;&lt;P&gt;From:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="mailto:freetalk@gmail.com" target="_blank" rel="noopener nofollow noreferrer"&gt;freetalk@gmail.com&lt;/A&gt;&lt;/P&gt;&lt;P&gt;To: abc@gmail.com&lt;/P&gt;&lt;P&gt;Subject:&amp;nbsp;Closing &amp;lt;Account_Number&amp;gt; - &amp;lt;Branch_ID&amp;gt;&lt;/P&gt;&lt;P&gt;Email content:&lt;/P&gt;&lt;P&gt;Cancel any instructions in the account &amp;lt;Account_Number&amp;gt; - &amp;lt;Branch_ID&amp;gt; -&amp;lt;Bank_ID&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Will send 5 emails.&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 21:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789084#M252459</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2022-01-08T21:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789085#M252460</link>
      <description>&lt;P&gt;Please show to code you want to run to send the first email.&lt;/P&gt;
&lt;P&gt;Then you can think about how to generate that code from your data.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 22:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789085#M252460</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-08T22:45:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789094#M252462</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/288727"&gt;@shlomiohana&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Repeating what others already proposed and then some sample steps to demonstrate how to get there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you need is a SAS macro which you then can call out of a SAS data step where you pass in your SAS variables as parameters to the SAS macro for every row in your table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Step 1: Create static and fully tested code for sending an email. Below just some sample code that will print something.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  file print;
  put
    "From: freetalk@gmail.com" /
    "To: abc@gmail.com" /
    "Subject: Closing &amp;lt;Account_Number&amp;gt; - &amp;lt;Branch_ID&amp;gt;" /
    "Email content:"
    "Cancel any instructions in the account &amp;lt;Account_Number&amp;gt; - &amp;lt;Branch_ID&amp;gt; -&amp;lt;Bank_ID&amp;gt;" /
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Step 2: Once the static code is working make it dynamic via a SAS macro. You basically need to create a parameter for all the bits in your code that will need to take on different values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymail(
  from=freetalk@gmail.com,
  to=abc@gmail.com,
  acct_no=,
  branch_id=,
  bank_id=
  );

  data _null_;
    file print;
    put
      "From: &amp;amp;from" /
      "To: &amp;amp;to" /
      "Subject: Closing &amp;amp;acct_no - &amp;amp;branch_id" /
      "Email content:"
      "Cancel any instructions in the account &amp;amp;acct_no - &amp;amp;branch_id - &amp;amp;bank_id" /
      ;
  run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then test the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymail(
  to=abc@gmail.com,
  acct_no=ACCT0001,
  branch_id=BR025,
  bank_id=BANK001
  );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Step 3: If above is working then you're ready to also generate the macro calls.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data */
data sample;
  infile datalines truncover dlm='|' dsd;
  input to_addr :$40. Account_Number :$20. Branch_ID :$20. Bank_ID :$20.;
  datalines;
email_1@test.com|ACCT0001|BR001|BANK001
email_99@test.com|ACCT0099|BR099|BANK099
;

/* call macro once per observation in sample table */
data _null_;
  set sample;
  length cmd $500;
  cmd=cats( '%mymail(to=',to_addr,',acct_no=',Account_Number,',branch_id=',Branch_ID,',bank_id=',Bank_ID,')' );
  call execute(cmd);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And here you go:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1641691041463.png" style="width: 587px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67230i07F93507588656EA/image-dimensions/587x232?v=v2" width="587" height="232" role="button" title="Patrick_0-1641691041463.png" alt="Patrick_0-1641691041463.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jan 2022 01:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/789094#M252462</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-01-09T01:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to create global variables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/795368#M255121</link>
      <description>&lt;P&gt;Thank you very much, your solution is excellent.&lt;BR /&gt;I will need your help again , if the fields:&amp;nbsp;&lt;SPAN&gt;Branch_ID&lt;/SPAN&gt; and &lt;SPAN&gt;Account_Number, and to_addr&lt;/SPAN&gt;&amp;nbsp;are the same in the table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data sample;
  infile datalines truncover dlm='|' dsd;
  input to_addr :$40. Account_Number :$20. Branch_ID :$20. Bank_ID :$20.;
  datalines;
email_99@test.com|ACCT0001|BR001|BANK001
email_99@test.com|ACCT0001|BR001|BANK231
email_1@test.com|ACCT0099|BR099|BANK099
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Will send 2 emails, the first will look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="mailto:freetalk@gmail.com" target="_blank" rel="noopener nofollow noreferrer"&gt;freetalk@gmail.com&lt;/A&gt;&lt;/P&gt;&lt;P&gt;To: email_99@test.com&lt;/P&gt;&lt;P&gt;Subject:&amp;nbsp;Closing ACCT0001 - BR001&lt;/P&gt;&lt;P&gt;Email content:&lt;/P&gt;&lt;P&gt;Cancel any instructions in the account:&lt;/P&gt;&lt;P&gt;ACCT0001 - BR001 -BANK001&lt;/P&gt;&lt;P&gt;ACCT0001 - BR001 -BANK231&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 records will enter the same email only with a different Bank_ID field.&lt;/P&gt;&lt;P&gt;It is possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Feb 2022 22:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-global-variables-in-a-loop/m-p/795368#M255121</guid>
      <dc:creator>shlomiohana</dc:creator>
      <dc:date>2022-02-09T22:09:39Z</dc:date>
    </item>
  </channel>
</rss>

