<?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 Calling a variable list with a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753524#M237509</link>
    <description>&lt;P&gt;Good morning.&amp;nbsp; I have defined variable names in %let statements, but am having trouble trying to resolve part that cycles through a %do loop.&amp;nbsp; Here is the simplified version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let vars=%nrstr(var&amp;amp;n._x var&amp;amp;n._y);&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro datapull;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;keep %do n=1 %to 10; &amp;amp;vars %end;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The macro runs fine if I substitute in all of the variable names by hand - meaning entering each variation 10 times. I've tried using different forms of %nrstr, %superq, etc, but just can't figure this one out.&amp;nbsp; Do I need to define the %let statement in it's own macro?&amp;nbsp; I've also tried putting the %do n1-10 %end statement in the %let statement as well.&amp;nbsp; I feel like I'm missing something easy here.&amp;nbsp; What is it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to keep this format - I know the obvious way is to just type out all the variable names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Jul 2021 15:18:00 GMT</pubDate>
    <dc:creator>JenniferBernard</dc:creator>
    <dc:date>2021-07-12T15:18:00Z</dc:date>
    <item>
      <title>Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753524#M237509</link>
      <description>&lt;P&gt;Good morning.&amp;nbsp; I have defined variable names in %let statements, but am having trouble trying to resolve part that cycles through a %do loop.&amp;nbsp; Here is the simplified version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let vars=%nrstr(var&amp;amp;n._x var&amp;amp;n._y);&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro datapull;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;keep %do n=1 %to 10; &amp;amp;vars %end;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The macro runs fine if I substitute in all of the variable names by hand - meaning entering each variation 10 times. I've tried using different forms of %nrstr, %superq, etc, but just can't figure this one out.&amp;nbsp; Do I need to define the %let statement in it's own macro?&amp;nbsp; I've also tried putting the %do n1-10 %end statement in the %let statement as well.&amp;nbsp; I feel like I'm missing something easy here.&amp;nbsp; What is it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to keep this format - I know the obvious way is to just type out all the variable names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753524#M237509</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2021-07-12T15:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753527#M237511</link>
      <description>&lt;P&gt;Okay, I've seemed to resolve this by just having&lt;/P&gt;&lt;P&gt;%let vars=&lt;SPAN&gt;var&amp;amp;n._x var&amp;amp;n._y;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The macro works and completes, but the log gives the warning that the reference isn't resolved.&amp;nbsp; I think this might be okay?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753527#M237511</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2021-07-12T15:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753528#M237512</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/331398"&gt;@JenniferBernard&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Okay, I've seemed to resolve this by just having&lt;/P&gt;
&lt;P&gt;%let vars=&lt;SPAN&gt;var&amp;amp;n._x var&amp;amp;n._y;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The macro works and completes, but the log gives the warning that the reference isn't resolved.&amp;nbsp; I think this might be okay?&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It doesn't sound okay to me. Can you run this command before you run the macro,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then run the macro again and show us the ENTIRE log for the macro (all of it, do not pick and choose parts of the log to show us).&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753528#M237512</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-12T15:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753530#M237514</link>
      <description>&lt;P&gt;Adding:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;naming variables like var1_x var1_y var2_x var2_y and so on makes your programming more difficult. If, on the other hand you had named your variables varx_1 vary_1 varx_2 vary_2 then your programming is much easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your KEEP statement can then be reduced to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;keep varx_1-varx_10 vary_1-vary_10;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or even simpler (if appropriate)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;keep varx: vary:;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;no macros needed&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753530#M237514</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-12T15:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753535#M237515</link>
      <description>&lt;P&gt;Best solution is to go back to the drawing board and make sure to place the numeric sequence at the END of the variable names.&amp;nbsp; Then you can skip any need for code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you need a macro tool that takes three inputs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlist(base,count,suffix);
%local i j ;
%do i=1 %to &amp;amp;count;
  %do j=1 %to %sysfunc(countw(&amp;amp;suffix,%str( )));
 &amp;amp;base.&amp;amp;i%scan(&amp;amp;suffix,&amp;amp;j,%str( ))
  %end;
%end;
%mend ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;400   %put %varlist(var,10,_x _y);
var1_x  var1_y  var2_x  var2_y  var3_x  var3_y  var4_x  var4_y  var5_x  var5_y  var6_x  var6_y  var7_x  var7_y  var8_x  var8_y
var9_x  var9_y  var10_x  var10_y
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Note if you are using this inside of a macro you might need to add something to prevent the parser from getting confused by the macro function call in the middle of the variable name.&amp;nbsp; So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlist(base,count,suffix);
%local i j ;
%do i=1 %to &amp;amp;count;
  %do j=1 %to %sysfunc(countw(&amp;amp;suffix,%str( )));
 %unquote(&amp;amp;base.&amp;amp;i%scan(&amp;amp;suffix,&amp;amp;j,%str( )))
  %end;
%end;
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753535#M237515</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-12T15:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753536#M237516</link>
      <description>&lt;P&gt;Yes, 100% agree.&amp;nbsp; Can't do anything with this set-up however.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753536#M237516</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2021-07-12T15:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753537#M237517</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/331398"&gt;@JenniferBernard&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The %NRSTR function avoids the warning about &lt;FONT face="courier new,courier"&gt;&amp;amp;n&lt;/FONT&gt; not being resolved by adding invisible macro quoting characters to the macro variable value. To get rid of these added characters in the %DO loop, where they would cause errors, use the %UNQUOTE function:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;keep %do n=1 %to 10; &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;%unquote(&lt;/FONT&gt;&lt;/STRONG&gt;&amp;amp;vars&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;)&lt;/FONT&gt;&lt;/STRONG&gt; %end;;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;If those variables in the KEEP statement are contained in dataset HAVE, you may want to replace the KEEP &lt;EM&gt;statement&lt;/EM&gt; with a KEEP= &lt;EM&gt;dataset option&lt;/EM&gt; to improve performance:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;set have(keep=%do n=1 %to 10; %unquote(&amp;amp;vars) %end;);&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753537#M237517</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-07-12T15:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753538#M237518</link>
      <description>&lt;P&gt;The warning comes from the &amp;amp;n. not being resolved in the let statement (outside the macro).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I checked with mprint and it did indeed resolve to include all of my variables.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 15:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753538#M237518</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2021-07-12T15:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753539#M237519</link>
      <description>&lt;P&gt;Here is a way to see what values you are currently creating:&lt;/P&gt;
&lt;PRE&gt;%let vars=%nrstr(var&amp;amp;n._x var&amp;amp;n._y); 

%macro dummy;
%do n=1 %to 10;
%put resolved variables: &amp;amp;vars ;
%end;

%mend;

%dummy;&lt;/PRE&gt;
&lt;P&gt;And what may be one way to create what you are expecting:&lt;/P&gt;
&lt;PRE&gt;%macro dummy2(vars=var&amp;amp;n._x var&amp;amp;n._y);
%do n=1 %to 10;
%put resolved variables: &amp;amp;vars ;
%end;

%mend;

%dummy2;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note since I am using the %put statement it does need the ; to end the statement. If you want/need a single Keep statement then your current structure would work while passing the parameter this way. Or create multiple Keep statements. Since you are not using a data set option multiple keeps will work:&lt;/P&gt;
&lt;PRE&gt;data junk;
   set sashelp.class;
   keep name;
   keep age;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not a fan of just letting a macro parameter, your Vars variable, just appear in the middle of a macro. It is often hard to tell just when a value is assigned or used and debugging scope problems gets much more difficult.&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>Mon, 12 Jul 2021 15:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753539#M237519</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-12T15:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a variable list with a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753540#M237520</link>
      <description>Great! Thank you so much! And I've moved the keep statement, so thank you for that.</description>
      <pubDate>Mon, 12 Jul 2021 15:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-variable-list-with-a-do-loop/m-p/753540#M237520</guid>
      <dc:creator>JenniferBernard</dc:creator>
      <dc:date>2021-07-12T15:57:33Z</dc:date>
    </item>
  </channel>
</rss>

