<?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: Macro code to iterate through list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799297#M314290</link>
    <description>&lt;P&gt;Since I was talking about storing &lt;EM&gt;values&lt;/EM&gt; in my earlier post, I also named the variable&amp;nbsp;&lt;EM&gt;value&lt;/EM&gt;. So you need to do the same.&lt;/P&gt;</description>
    <pubDate>Tue, 01 Mar 2022 12:32:41 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-03-01T12:32:41Z</dc:date>
    <item>
      <title>Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798479#M313902</link>
      <description>&lt;P&gt;I accepted an answer a bit too hastily when I tried to ask a question earlier.&amp;nbsp; I wasn't clear enough with my question then.&amp;nbsp; And didn't understand the answers given.&amp;nbsp;&amp;nbsp;So, need to try one more time....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;, and helpful community.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've set up a complete document/program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Logistic, All.txt"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the top of the document is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let etf = xyz ;
&lt;/PRE&gt;
&lt;P&gt;Then all throughout the document is the following, as appropriate, everywhere "xyz" needs to appear:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;amp;etf.&lt;/PRE&gt;
&lt;P&gt;SAS calls this trick creating and using a macro variable.&amp;nbsp; Pretty straight forward, easy enough, and incredibly useful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Running the entire long program set in SAS works perfectly.&amp;nbsp; All is good with the entire amalgam of procedure steps.&amp;nbsp; Completion is as desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What's next is to act on a whole&amp;nbsp;&lt;STRONG&gt;list&lt;/STRONG&gt;, as "xyz" is only ONE of a hundred such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The complete list, say, is:&lt;/P&gt;
&lt;P&gt;xyz&lt;/P&gt;
&lt;P&gt;abc&lt;/P&gt;
&lt;P&gt;def&lt;/P&gt;
&lt;P&gt;lmn&lt;/P&gt;
&lt;P&gt;qrs&lt;/P&gt;
&lt;P&gt;(etc.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, as of this second, to move forward, I would need to replicate the above-mentioned document 100x, manually changing only the "xyz" at the very top of each document, to the appropriate characters, and then running each of the 100 documents through SAS.&amp;nbsp; This can fairly efficiently be done in a single go (not 100 separate runs) -- using the %include approach, in an additionally created document:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Logistic, All, Complete List, Using 'Include'.txt":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%include "c:\1\Logistic, All -- xyz.txt" ;&lt;BR /&gt;%include "c:\1\Logistic, All -- abc.txt" ;&lt;BR /&gt;%include "c:\1\Logistic, All -- def.txt" ;&lt;BR /&gt;%include "c:\1\Logistic, All -- lmn.txt" ;&lt;BR /&gt;%include "c:\1\Logistic, All -- qrs.txt" ;&lt;BR /&gt;(etc.)
&lt;/PRE&gt;
&lt;P&gt;Instead of going about it in the above manner, I'm pretty sure there is a macro approach.&amp;nbsp; Automatically get each of the list items over into the "xyz" position (at the very top), one at a time, through the entire list, running the entire procedure set at each iteration of each change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope the task is clear enough this time around, and look forward to your responses.&amp;nbsp; Please try to answer as clear as day, as this all seems rather cloudy at present.&amp;nbsp; And hopefully others will see this post and learn something as well.&amp;nbsp; There may be more than one way of accomplishing the task.&amp;nbsp; Please share your thoughts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Humorously, I don't even know what to call the above challenge, so as to conduct a successful Google search.&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;
&lt;P&gt;Nicholas Kormanik&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798479#M313902</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2022-02-25T00:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798482#M313905</link>
      <description>&lt;P&gt;One typical way with macros:&lt;/P&gt;
&lt;PRE&gt;%macro dummy (listvar= );

%do i = 1 %to %sysfunc(countw(&amp;amp;listvar.));
   %let onevalue = %scan(&amp;amp;listvar.,&amp;amp;i);
   %put Replace this Put with the code that uses &amp;amp;onevalue. ;
%end;
%mend;


%dummy(listvar= thisword thatword anotherword);&lt;/PRE&gt;
&lt;P&gt;Caveats, if your "word" is going to contain spaces then you need to provide a delimiter (NOT COMMA for simplicity sake) between values and modify the countw and %scan function calls to use that delimiter as third argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the values in a data set you can use Proc sql with INTO to place the values into a macro list that will work.&lt;/P&gt;
&lt;PRE&gt;Proc sql noprint;
   select value into : varlist separated by ' '
   from yourdataset
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;Another approach is a data step creating statements and submitting them using the CALL EXECUTE statement. That you can search for on the forum for examples.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure that the generated statements are valid before turning them loose with action, such as my example with %put.&lt;/P&gt;
&lt;P&gt;Your %include statements for example look a bit likely to cause all sorts of problems.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798482#M313905</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-25T00:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798484#M313907</link>
      <description>&lt;P&gt;The start of your question makes it sound like you have ONE program file.&amp;nbsp; If that is the case&amp;nbsp;you have done 99% of the work of defining a macro.&amp;nbsp; &amp;nbsp;So just&amp;nbsp;do the last couple of steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take your existing file.&lt;/P&gt;
&lt;P&gt;Remove the %LET statement.&lt;/P&gt;
&lt;P&gt;Add a %MACRO statement at the top. You will need a name for the macro and you will want to define ETF as the parameter to the macro.&lt;/P&gt;
&lt;P&gt;Add a %MEND statement at the bottom.&lt;/P&gt;
&lt;P&gt;You now have a macro definition.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro logistic(etf);
.... rest of the file ....
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might want to save that into its own file. (I recommend using a normal name for the file. One without spaces or punctuation in it and one that ends with .sas. ) So if you named the macro LOGISTIC in the %MACRO statement then call the file logistic.sas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now make another program that will loop over your list.&lt;/P&gt;
&lt;P&gt;First step is to get the list into the program.&amp;nbsp; Do you have source already for the list?&amp;nbsp; Perhaps a dataset? Or a text file you can read into a dataset?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For something with 100 values I recommend putting it into an actual dataset.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you could do something as simple as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data eft_list;
  input eft $80.;
cards;
xyz
abc
def
lmn
qrs
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that you have the data use it to generate one call to the macro for each observation in the dataset.&amp;nbsp; A simple way might be to use CALL EXECUTE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set eft_list;
  call execute(catx(' '
   ,'%nrstr(%logistic)('
   ,eft
   ,')'
   ));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will first need to actually define the macro by running the code that defines it (the part from %MACRO to %MEND).&amp;nbsp; You could either add the body of the macro into that program before the data step that generates the calls.&lt;/P&gt;
&lt;P&gt;Or leave it in its own file and use %INCLUDE to define the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your final run all program might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%include 'logistic.sas';

data eft_list;
  input eft $80.;
cards;
xyz
abc
def
lmn
qrs
;

data _null_;
  set eft_list;
  call execute(catx(' '
   ,'%nrstr(%logistic)('
   ,eft
   ,')'
   ));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798484#M313907</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-25T00:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798487#M313909</link>
      <description>&lt;P&gt;one way to do is to create table of your list and then looping through the list. those.&amp;nbsp;Below I am pasting an example, how to do it. create a permanent dataset unlike work dataset i am showing in below example. Creation of the dataset is needed only once, next time onwards you can simply run below macro.&lt;/P&gt;
&lt;PRE&gt;data test;&lt;BR /&gt;	myfilename='/home/kiranvenna1/test/fileone.sas';&lt;BR /&gt;	output;&lt;BR /&gt;	myfilename='/home/kiranvenna1/test/filetwo.sas';&lt;BR /&gt;	output;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%macro one;&lt;BR /&gt;	proc sql noprint;&lt;BR /&gt;		select myfilename into :fname1- from test;&lt;BR /&gt;		select count(*) into :cnt from test;&lt;BR /&gt;	quit;&lt;BR /&gt;&lt;BR /&gt;	%do i=1 %to &amp;amp;cnt;&lt;BR /&gt;		%include "&amp;amp;&amp;amp;fname&amp;amp;i";&lt;BR /&gt;	%end;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;%one&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 00:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798487#M313909</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2022-02-25T00:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798489#M313911</link>
      <description>&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Step by Step 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;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage - includes examples to loop through a list&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;</description>
      <pubDate>Fri, 25 Feb 2022 01:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798489#M313911</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-02-25T01:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798515#M313929</link>
      <description>&lt;P&gt;I've read that SAS macros do nothing more than create SAS base code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is true, would it be at all helpful to jump straight to base code output to solve the task outlined at top?&amp;nbsp; What would that look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm finding it really hard to follow exactly what you folks are suggesting.&amp;nbsp; It all looks like a giant jumble.&amp;nbsp; Maybe some sleep will help....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll do a Google search on "&lt;SPAN&gt;CALL EXECUTE", as that might create some light.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Seems strange that I've yet to encounter a single example of what I'm attempting to accomplish, though it appears that what I'm pursuing here is basic and reasonable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 06:37:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798515#M313929</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2022-02-25T06:37:38Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798518#M313932</link>
      <description>&lt;P&gt;By the way, what I'm attempting to do reminds me of a 'mail merge.'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Given a base document, designate a place for a name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Have list of names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; Press the merge button.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe if thought about this way, other solutions within SAS will become apparent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's highly likely SAS has such functionality built in.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 07:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798518#M313932</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2022-02-25T07:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798520#M313934</link>
      <description>&lt;P&gt;Remove the %LET from your code, and save it as&lt;/P&gt;
&lt;PRE&gt;logistic_all.sas&lt;/PRE&gt;
&lt;P&gt;(SAS codes should always be saved with that extension, and don't use special characters like blanks or commas)&lt;/P&gt;
&lt;P&gt;Next, save your values in a dataset, and set up a master code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set dataset;
call execute('%nrstr(
  %let etf = ' !! value !! ';
  %include "logistic_all.sas";
)');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After the data step has run, a complete sequence of %LET and %INCLUDE statements will be in the execution queue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: corrected typo in CALL EXECUTE code (there was a comma where a semicolon is needed).&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 12:30:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798520#M313934</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-01T12:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798538#M313943</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This a really good question for someone beginning with the macro language.&amp;nbsp; And your analogy to a mail merge is a great analogy for the purpose of a SAS macro.&amp;nbsp; So let's use that analogy.&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22691"&gt;@NKormanik&lt;/a&gt;&amp;nbsp;wrote:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;By the way, what I'm attempting to do reminds me of a 'mail merge.'&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Given a base document, designate a place for a name.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Have list of names.&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; Press the merge button.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In a simple mail merge, you create a template (base document) which has static text, and a dynamic field, say NAME.&amp;nbsp; And then to execute the mail merge, you type in a list of 5 names (typically into a table of some sort), and press "RUN", and the mail merge creates 5 documents, each with the value of name_i inserted into the name field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example of a SAS macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro print(name=) ;
  proc print data=sashelp.class ;
    where name="&amp;amp;NAME" ;
  run ;
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This macro defines a block of code that is 3 lines long.&amp;nbsp; That code is mostly static text, like the text in the mail merge template.&amp;nbsp; There is one macro variable, referenced by &amp;amp;NAME, which is analogous to the field in a mail merge.&amp;nbsp; Because the macro variable is defined as an input to the macro, it's also referred to as a macro parameter.&amp;nbsp; When you run the above code, SAS creates a macro (i.e. mail merge template, containing some fixed text and one dynamic field).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use that template, you can call the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint ;
%print(name=Mary)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With that call, SAS will open the template, and execute all the code, and when it sees the field NAME, it will replace that field with the value Mary.&amp;nbsp; And because we turned on the MPRINT option (which is good to always have on), you can see the code that the macro generated (and SAS executed) in the log:&lt;/P&gt;
&lt;PRE&gt;8    %print(name=Mary)
MPRINT(PRINT):   proc print data=sashelp.class ;
MPRINT(PRINT):   where name="Mary" ;
MPRINT(PRINT):   run ;
&lt;/PRE&gt;
&lt;P&gt;So a macro really is like a&amp;nbsp; mail merge.&amp;nbsp; If you want to use the template to generate three PROC PRINT steps for three different students, you could call it three times:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%print(name=Mary)
%print(name=James)
%print(name=William)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and that will execute three PROC PRINT steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now sensibly, if you have 100 names, you don't want to have to type in 100 macro calls.&amp;nbsp; As you know, the whole point of a mail merge is that it is data-driven.&amp;nbsp; You have a data table that lists 100 names, and just tell the mail merge to create one document for each name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are different options for using data to drive the execution of a macro, many of which have been shown in this thread already.&amp;nbsp; I think for the mail merge analogy, call execute is the easiest to understand.&amp;nbsp; I can store a list of names in a SAS dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MyNames ;
  input FirstName $10. ;
  cards ;
Mary 
James 
William
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is the data I will feed into my macro (mail-merge template).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I can use CALL EXECUTE to execute the macro.&amp;nbsp; The below DATA step reads each record from MyNames.&amp;nbsp; The job of CALL EXECUTE, as used below, is to generate a macro call from each record.&amp;nbsp; And when it creates the macro call, it takes the value from the dataset variable FirstName and places it into the macro call. The code is a little ugly, due to some timing complexity of CALL EXECUTE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set MyNames;
  call execute(
    '%nrstr(%%)print(name='||FirstName||')'
   );
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The argument to CALL EXECUTE is a text value, which is created by concatenating fixed text with the value of FirstName read from the dataset.&amp;nbsp; When that code runs, the DATA step loops 3 times, which means CALL EXECUTE executes three times, and it generates 3 macro calls, just as if you had typed them in yourself.&amp;nbsp; You can see the macro calls in the log:&lt;/P&gt;
&lt;PRE&gt;NOTE: CALL EXECUTE generated line.
1   + %print(name=Mary      )
2   + %print(name=James     )
3   + %print(name=William   )
&lt;/PRE&gt;
&lt;P&gt;Using CALL EXECUTE in this was is an advanced way to use the macro language.&amp;nbsp; Before worrying about CALL EXECUTE, work on understanding a macro.&amp;nbsp; A macro (mail merge template) is a unit of code that includes one or more input parameters (fields).&amp;nbsp; When you run a macro, you manually input a value for each parameter.&amp;nbsp; If you want to use DATA to drive a macro call, one way to do that is to use CALL EXECUTE to run a macro and automatically input a value that is read from a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So one approach for your need to is take the code from your %include program and turn it into a macro with a parameter.&amp;nbsp; Then you can try running the macro manually to get it working, then you can try using CALL EXECUTE to drive the macro calls.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Feb 2022 13:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798538#M313943</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-02-25T13:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798778#M314013</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37783"&gt;@kiranv_&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wow, such amazing clarity.&amp;nbsp; I'm finally starting to see the light.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Feb 2022 00:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/798778#M314013</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2022-02-26T00:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799285#M314283</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've called the ETF variable in the dataset "ETF_List".&amp;nbsp; Maybe that's the problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1
2    data _null_;
3    set sas_1.ETF_dataset;
4    call execute('%nrstr(
5    %let etf = ' !! value !! ';
6    %include "c:\0_sas_1\logistic_all.sas",
7    )');
8    run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      5:17
NOTE: Variable value is uninitialized.
NOTE: There were 59 observations read from the data set SAS_1.ETF_DATASET.

NOTE: CALL EXECUTE generated line.
1   + %let etf =            .;%include "c:\0_sas_1\logistic_all.sas",
2   + %let etf =            .;%include "c:\0_sas_1\logistic_all.sas",
ERROR: Open code statement recursion detected.
3   + %let etf =            .;%include "c:\0_sas_1\logistic_all.sas",
ERROR: Incorrect %INCLUDE statement will not be executed. There is a syntax error.
4   + %let etf =            .;%include "c:\0_sas_1\logistic_all.sas",
ERROR: Open code statement recursion detected.
5   + %let etf =            .;%include "c:\0_sas_1\logistic_all.sas",
ERROR: Incorrect %INCLUDE statement will not be executed. There is a syntax error.
&lt;/PRE&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 11:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799285#M314283</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2022-03-01T11:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799290#M314287</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you are generating code (either with a macro or in this case with CALL EXECUTE), it's always important to review the code that was generated (as shown in the log).&amp;nbsp; In this case, the log shows that the following code was generated:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let etf =            .;%include "c:\0_sas_1\logistic_all.sas",
%let etf =            .;%include "c:\0_sas_1\logistic_all.sas",&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you highlight that code and submitted it, would it work?&amp;nbsp; No.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would get a syntax error, because the %include statements ends with a comma instead of a semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would have a logical error, because your macro variable ETF&amp;nbsp; is being assigned the value dot (.) not the value ABC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With that information, you need to then go back and look at your code that generates the bad code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;2    data _null_;
3    set sas_1.ETF_dataset;
4    call execute('%nrstr(
5    %let etf = ' !! value !! ';
6    %include "c:\0_sas_1\logistic_all.sas",
7    )');
8    run;&lt;/PRE&gt;
&lt;P&gt;For the first issue, it looks like there is a typo.&amp;nbsp; You can see on line 6 that there is a comma at the end instead of a semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the second issue, look on line 5.&amp;nbsp; You can see that the helpful example code Kurt provided used a generic variable name VALUE, because he didn't know the name of the variable in your real dataset.&amp;nbsp; So yes, if your variable is named&amp;nbsp;&lt;SPAN&gt;ETF_List, you should replace VALUE with&amp;nbsp;ETF_List.&amp;nbsp; The other clue to this problem was the note "NOTE: Variable value is uninitialized."&amp;nbsp; This is really an error message.&amp;nbsp; It means your code referenced a variable that doesn't exist.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As you work on resolving these issues, you want to get to a point where your log from CALL EXECUTE looks like:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1   + %let etf = abc;%include "c:\0_sas_1\logistic_all.sas";
2   + %let etf = def;%include "c:\0_sas_1\logistic_all.sas";&lt;/PRE&gt;
&lt;P&gt;If fact, you might want to try running the above two lines manually, to make sure they work, and give you the desired results.&amp;nbsp; This would confirm that everything in logistic_all.sas is working.&amp;nbsp; After that, you can go back to using CALL EXECUTE to generate the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 11:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799290#M314287</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-03-01T11:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799294#M314289</link>
      <description>&lt;P&gt;I have corrected the typo in my code. Kudos for finding it.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 12:31:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799294#M314289</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-01T12:31:09Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799297#M314290</link>
      <description>&lt;P&gt;Since I was talking about storing &lt;EM&gt;values&lt;/EM&gt; in my earlier post, I also named the variable&amp;nbsp;&lt;EM&gt;value&lt;/EM&gt;. So you need to do the same.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 12:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799297#M314290</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-01T12:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799322#M314299</link>
      <description>&lt;P&gt;SAS is clearly showing you the mistake in your code.&lt;/P&gt;
&lt;PRE&gt;2    data _null_;
3    set sas_1.ETF_dataset;
4    call execute('%nrstr(
5    %let etf = ' !! value !! ';
6    %include "c:\0_sas_1\logistic_all.sas",
7    )');
8    run;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      5:17
NOTE: Variable value is uninitialized.&lt;/PRE&gt;
&lt;P&gt;The first one says you are treating VALUE as if it was a character variable, but it is numeric.&lt;/P&gt;
&lt;P&gt;The second one says that the variable VALUE has never been given any values.&amp;nbsp; So it did not exist in the source data read from ETF_DATASET.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the name of the variable in the ETF_DATASET that you want to use to assign to the macro variable ETF?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 14:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799322#M314299</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-01T14:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799468#M314374</link>
      <description>&lt;P&gt;Goodness.&amp;nbsp; "Press ANY key..."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where's the "ANY" key??!#&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will retry.&amp;nbsp; Thanks all for the help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2022 00:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799468#M314374</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2022-03-02T00:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro code to iterate through list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799632#M314456</link>
      <description>&lt;P&gt;Writing code that writes code is inherently trickier than writing code. : )&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2022 17:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-code-to-iterate-through-list/m-p/799632#M314456</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-03-02T17:51:22Z</dc:date>
    </item>
  </channel>
</rss>

