<?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 Read a Variable Value into a Macro Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549770#M152595</link>
    <description>&lt;P&gt;I need to read the value of a variable into a macro variable and then in the same data step use the macro variable as the upper limit in a do loop. My code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.test;&lt;BR /&gt;set in.test_data;&lt;BR /&gt;call symput('mqty',trim(Order_Quantity));&lt;BR /&gt;put "Quantity = &amp;amp;mqty.";&lt;BR /&gt;do i=1 to "&amp;amp;mqty.";&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, the symput only processes the last row in the data set, so it uses that value in each iteration of the do loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
    <pubDate>Tue, 09 Apr 2019 18:52:32 GMT</pubDate>
    <dc:creator>CurtisSmithDCAA</dc:creator>
    <dc:date>2019-04-09T18:52:32Z</dc:date>
    <item>
      <title>Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549770#M152595</link>
      <description>&lt;P&gt;I need to read the value of a variable into a macro variable and then in the same data step use the macro variable as the upper limit in a do loop. My code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.test;&lt;BR /&gt;set in.test_data;&lt;BR /&gt;call symput('mqty',trim(Order_Quantity));&lt;BR /&gt;put "Quantity = &amp;amp;mqty.";&lt;BR /&gt;do i=1 to "&amp;amp;mqty.";&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, the symput only processes the last row in the data set, so it uses that value in each iteration of the do loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2019 18:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549770#M152595</guid>
      <dc:creator>CurtisSmithDCAA</dc:creator>
      <dc:date>2019-04-09T18:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549773#M152598</link>
      <description>&lt;P&gt;Why not do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test;
set in.test_data;
do i=1 to order_quantity;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2019 18:59:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549773#M152598</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-09T18:59:10Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549831#M152619</link>
      <description>What @KirkBresmer said. You can still do the macro var, but it is the same value as order_quantity</description>
      <pubDate>Wed, 10 Apr 2019 03:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549831#M152619</guid>
      <dc:creator>CJac73</dc:creator>
      <dc:date>2019-04-10T03:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549837#M152622</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;I need to read the value of a variable into a macro variable and then &lt;U&gt;&lt;STRONG&gt;in the same data step&lt;/STRONG&gt;&lt;/U&gt; use the macro variable as the upper limit in a do loop.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will never work.&amp;nbsp; Read the macro doc to understand the difference between compile time vs. run time execution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test;
set in.test_data;
call symput('mqty',trim(Order_Quantity));
put "Quantity = &amp;amp;mqty.";
do i=1 to "&amp;amp;mqty.";
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;amp;mqty (lines 4 &amp;amp; 5) is evaluated &lt;U&gt;&lt;STRONG&gt;before the data step starts execution&lt;/STRONG&gt;&lt;/U&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I recommend call symputx over call symput.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Apr 2019 04:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549837#M152622</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-04-10T04:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549842#M152624</link>
      <description>&lt;P&gt;There is no need in your example since you already have the value in the dataset.&lt;/P&gt;
&lt;P&gt;If you did need to read the value of a macro variable dynamically then use SYMGET() or SYMGETN() function.&lt;/P&gt;
&lt;P&gt;Try this program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let x1=5;
%let x2=3;
data test;
do row=1 to 2;
  do obs=1 to symgetn(cats('x',row));
    output;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Apr 2019 05:30:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549842#M152624</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-10T05:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549845#M152626</link>
      <description>do i=1 to symgetn('mqty');</description>
      <pubDate>Wed, 10 Apr 2019 05:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549845#M152626</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-10T05:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549849#M152627</link>
      <description>&lt;P&gt;@Tom and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;are very smart SAS professionals, and I bow to their expertise.&amp;nbsp; They've both helped me more times than I can count (although that presumes I can count very high).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, in this instance, I'll point out that I feel their examples are illustrative only, just so you understand the concept of symget and symgetn.&amp;nbsp; I don't think using symget is the approach you should be using in your example.&amp;nbsp; (Actually, Tom made it clear that it was just an example).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you find yourself loading a data step variable into a macro variable (call symput), then wanting to reference that macro variable in the same data step (symget), 99.99% of the time you should just use the data step variable instead.&amp;nbsp; Moving data to/from the macro symbol table is much slower than using data in the PDV/data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In summary, use&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s approach.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Apr 2019 06:02:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549849#M152627</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-04-10T06:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: Read a Variable Value into a Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549851#M152628</link>
      <description>&lt;P&gt;And if you need to keep values &lt;EM&gt;across&lt;/EM&gt; observations, the data step provides multiple tools for this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;lag() functions&lt;/LI&gt;
&lt;LI&gt;retained variables&lt;/LI&gt;
&lt;LI&gt;temporary arrays&lt;/LI&gt;
&lt;LI&gt;hash objects&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;which will all perform better than invoking the macro processor repeatedly while the step is running.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Apr 2019 06:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-a-Variable-Value-into-a-Macro-Variable/m-p/549851#M152628</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-04-10T06:24:49Z</dc:date>
    </item>
  </channel>
</rss>

