<?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: Nested loop not working with symput macro variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918491#M361803</link>
    <description>Thank you all for your suggestions, I was able to use Paige's suggestion successfully.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;set control_var;&lt;BR /&gt;call execute('%loop(qtr='||strip(qtr)||' ,year_qtr='||strip(year_qtr)||');');&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 29 Feb 2024 20:25:31 GMT</pubDate>
    <dc:creator>bzimmermann</dc:creator>
    <dc:date>2024-02-29T20:25:31Z</dc:date>
    <item>
      <title>Nested loop not working with symput macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918457#M361791</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;I am having an issue with this bit of code. I have created a null dataset containing the variables required in the loop macro and it also counts the number of observations. I want to run through each of the lines in the control_var dataset through the loop macro. But when I run the code it runs only the last row in the control_var table, it doesn't start at the first row. If I remove the macro and have the do statement run using a title i variable in the log it works starting at 1 and going through the 19 observations. So I believe the %do clause is accurate. There is something wrong with calling the variables from the control_var table&lt;/P&gt;&lt;P&gt;Any help you can give me would be greatly appreciated-thanks&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%MACRO run_loops();&lt;/DIV&gt;&lt;DIV&gt;data _null_;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set control_var;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;call symput ("year_qtr", year_qtr);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;call symput ("qtr", qtr);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;call symput ("nobs",_n_);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%DO i = 1 %TO &amp;amp;nobs;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%loop(qtr=&amp;amp;&amp;amp;qtr&amp;amp;i,year_qtr=&amp;amp;&amp;amp;year_qtr&amp;amp;i);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%END;&lt;/DIV&gt;&lt;DIV&gt;%MEND run_loops;&lt;/DIV&gt;&lt;DIV&gt;%run_loops();&lt;/DIV&gt;</description>
      <pubDate>Thu, 29 Feb 2024 18:15:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918457#M361791</guid>
      <dc:creator>bzimmermann</dc:creator>
      <dc:date>2024-02-29T18:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loop not working with symput macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918464#M361794</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49425"&gt;@bzimmermann&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;I am having an issue with this bit of code. I have created a null dataset containing the variables required in the loop macro and it also counts the number of observations. I want to run through each of the lines in the control_var dataset through the loop macro. But when I run the code it runs only the last row in the control_var table, it doesn't start at the first row.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, that is how the data step works. For the first observation in data set CONTROL_VAR, it assigns &amp;amp;year_qtr the proper value based upon the first observation, and then on the second observation of data set CONTROL_VAR, it overwrites the value of &amp;amp;year_qtr to be correct for the second observation. And so on, until you get to the last observation of CONTROL_VAR, and it overwrites the value &amp;amp;year_qtr with the value on the last observation.&amp;nbsp; So then your DO loop only has that available, it doesn't have the value of the other observations available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might want to consider &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/mcrolref/n1q1527d51eivsn1ob5hnz0yd1hx.htm" target="_self"&gt;CALL EXECUTE&lt;/A&gt; as a way of calling a macro with parameters based upon the data set value on each record. That link has an example of calling a macro based upon the values of a data set variable on each record. Another option is to put the DATA _NULL_ into the loop, and execute it to get the macro variables for just one observation (the &amp;amp;i-th observation).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on what %LOOP is doing (we don't know, you didn't show us the code), you might be able to simplify the whole thing to use a BY statement and get rid of the macros entirely.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 19:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918464#M361794</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-29T19:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loop not working with symput macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918471#M361796</link>
      <description>&lt;P&gt;I don't see where you made any attempt to create a macro variable named QTR1 or QTR2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you see that anywhere?&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set control_var;
call symputX(cats("year_qtr",_n_), year_qtr);
call symputX(cats("qtr",_n_), qtr);
call symputX("nobs",_n_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;PS Do not use the ancient CALL SYMPUT() function unless there is a reason why you need to store leading and/or trailing spaces into the macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 19:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918471#M361796</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-29T19:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loop not working with symput macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918473#M361797</link>
      <description>&lt;P&gt;Why bother making the macro variables at all?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just generate the calls to %LOOP directly with SAS code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example by writing a program file and using %INCLUDE to run it.&lt;/P&gt;
&lt;P&gt;Then you take advantage of the features of the PUT statement to make creating the code easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set control_var ;
  file code;
  put '%loop(' qtr= ',' year_qtr= ')' ;
run;
%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Feb 2024 19:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918473#M361797</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-29T19:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loop not working with symput macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918491#M361803</link>
      <description>Thank you all for your suggestions, I was able to use Paige's suggestion successfully.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;set control_var;&lt;BR /&gt;call execute('%loop(qtr='||strip(qtr)||' ,year_qtr='||strip(year_qtr)||');');&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 29 Feb 2024 20:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918491#M361803</guid>
      <dc:creator>bzimmermann</dc:creator>
      <dc:date>2024-02-29T20:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Nested loop not working with symput macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918512#M361806</link>
      <description>&lt;P&gt;Glad you got it working.&amp;nbsp; Please mark Paige's answer as correct / accepted, to close out this question.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Feb 2024 22:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-loop-not-working-with-symput-macro-variables/m-p/918512#M361806</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-29T22:30:42Z</dc:date>
    </item>
  </channel>
</rss>

