<?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: Saving value of evaluated macro variable to a data set variable in a macro definition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788032#M251877</link>
    <description>Hi Tom, thanks a lot. Achieves what I wanted.</description>
    <pubDate>Sun, 02 Jan 2022 17:41:54 GMT</pubDate>
    <dc:creator>bokola</dc:creator>
    <dc:date>2022-01-02T17:41:54Z</dc:date>
    <item>
      <title>Saving value of evaluated macro variable to a data set variable in a macro definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788028#M251873</link>
      <description>&lt;P&gt;I have the following macro from sas macro reference&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test(x);&lt;BR /&gt;%let x=1.5;&lt;BR /&gt;%do %while (&amp;amp;x&amp;lt;=2);&lt;BR /&gt;%put the value of x is &amp;amp;x;&lt;BR /&gt;%let x=%sysevalf(&amp;amp;x+0.25);&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%mend test;&lt;BR /&gt;%test(1);&lt;/P&gt;&lt;P&gt;I wish to save the value of &amp;amp;x to a new variable say y. How can I achieve that?&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jan 2022 15:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788028#M251873</guid>
      <dc:creator>bokola</dc:creator>
      <dc:date>2022-01-02T15:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: Saving value of evaluated macro variable to a data set variable in a macro definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788029#M251874</link>
      <description>&lt;P&gt;It is hard to tell from your example code what your actual question is.&amp;nbsp; There is no data step code shown so no data step variable to save a value into.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's address the dataset variable issue first.&lt;/P&gt;
&lt;P&gt;The easiest way to get a macro variable value into a data step is to just expand the value of the macro variable where you wants its text used to generate SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   myvar = &amp;amp;x ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if macro variable X has values like 1.75 then the macro processor will generate SAS code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   myvar = 1.75 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another possible interpretation of your question is around the use of macro variables and macros.&amp;nbsp; In your example code the macro variable X is defined as LOCAL to the macro TEST.&amp;nbsp; So when TEST ends X will disappear as there is no longer any symbol table to store it in.&amp;nbsp; If you want the calculated value of X to live after the macro finishes then assign its value to a macro variable that will exist.&amp;nbsp; One that is either GLOBAL or lives in the symbol table of a macro that is calling %TEST.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(x);
%do %while (%sysevalf(&amp;amp;x&amp;lt;=2));
  %put the value of x is &amp;amp;x;
  %let x=%sysevalf(&amp;amp;x+0.25);
%end;
%if not %symexist(y) %then %global y;
%let y=&amp;amp;x;
%mend test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another possible interpretation is to return the value of X as the result of calling the macro. Essentially convert TEST into a macro function.&amp;nbsp; This can only work if the macro only generates macro statements.&amp;nbsp; Do do that just expand the value to be returned in the macro definition.&amp;nbsp; And then call the macro as part of another statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test(x);
%do %while (%sysevalf(&amp;amp;x&amp;lt;=2));
  %put the value of x is &amp;amp;x;
  %let x=%sysevalf(&amp;amp;x+0.25);
%end;
&amp;amp;x.
%mend test;
data want;
  myvar=%test(1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Jan 2022 16:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788029#M251874</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-02T16:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Saving value of evaluated macro variable to a data set variable in a macro definition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788032#M251877</link>
      <description>Hi Tom, thanks a lot. Achieves what I wanted.</description>
      <pubDate>Sun, 02 Jan 2022 17:41:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Saving-value-of-evaluated-macro-variable-to-a-data-set-variable/m-p/788032#M251877</guid>
      <dc:creator>bokola</dc:creator>
      <dc:date>2022-01-02T17:41:54Z</dc:date>
    </item>
  </channel>
</rss>

