<?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: Assigning a macro a macro value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396253#M278115</link>
    <description>&lt;P&gt;When you do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET x = %RandBetween(1,50);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro call is resolved to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET x = (1 + floor((1+50-1)*rand("uniform")));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that x will now contain the &lt;EM&gt;text string&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;(1 + floor((1+50-1)*rand("uniform")))&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Seed = %EVAL(&amp;amp;x+&amp;amp;j);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will then resolve to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Seed = %EVAL((1 + floor((1+50-1)*rand("uniform")))+1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(for &amp;amp;j=1) where the macro processor can't resolve floor and rand (as they are not macro functions), and will throw an error because of non-numeric data.&lt;/P&gt;
&lt;P&gt;If you want to create a value, I suggest to use a data _null_ step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RandBetween(min, max);
(&amp;amp;min + floor((1+&amp;amp;max-&amp;amp;min)*rand("uniform")))
%mend;

%macro DoLoop;
%DO j = 1 %TO 2 ;
data _null_;
x = %RandBetween(1,50);
call symput('Seed',put(x+&amp;amp;j,best.));
run;

proc ... data=... ;

...

seed &amp;amp;seed;

run;

%END;
%MEND DoLoop;
%DoLoop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also have to question if macro randbetween is necessary at all.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2017 10:53:55 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-09-15T10:53:55Z</dc:date>
    <item>
      <title>Assigning a macro a macro value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396226#M278114</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to write a macro to run a model multiple times, each time setting the seed to a randomly selected value greater than 1. Right now my code looks something like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro RandBetween(min, max);&lt;BR /&gt;(&amp;amp;min + floor((1+&amp;amp;max-&amp;amp;min)*rand("uniform")))&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;%macro DoLoop;&lt;BR /&gt;%DO j = 1 %TO 2 ;&lt;BR /&gt;%LET x = %RandBetween(1,50);&lt;BR /&gt;%LET Seed = %EVAL(&amp;amp;x+&amp;amp;j);&lt;/P&gt;&lt;P&gt;proc ...&amp;nbsp;data=... ;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;seed &amp;amp;seed;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%END;&lt;BR /&gt;%MEND DoLoop;&lt;BR /&gt;%DoLoop;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However there is an issue assigning x a macro value. Any ideas how to assign a macro a random value?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 09:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396226#M278114</guid>
      <dc:creator>aoifeoneill</dc:creator>
      <dc:date>2017-09-15T09:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro a macro value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396253#M278115</link>
      <description>&lt;P&gt;When you do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET x = %RandBetween(1,50);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro call is resolved to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET x = (1 + floor((1+50-1)*rand("uniform")));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that x will now contain the &lt;EM&gt;text string&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;(1 + floor((1+50-1)*rand("uniform")))&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Seed = %EVAL(&amp;amp;x+&amp;amp;j);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will then resolve to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Seed = %EVAL((1 + floor((1+50-1)*rand("uniform")))+1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(for &amp;amp;j=1) where the macro processor can't resolve floor and rand (as they are not macro functions), and will throw an error because of non-numeric data.&lt;/P&gt;
&lt;P&gt;If you want to create a value, I suggest to use a data _null_ step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro RandBetween(min, max);
(&amp;amp;min + floor((1+&amp;amp;max-&amp;amp;min)*rand("uniform")))
%mend;

%macro DoLoop;
%DO j = 1 %TO 2 ;
data _null_;
x = %RandBetween(1,50);
call symput('Seed',put(x+&amp;amp;j,best.));
run;

proc ... data=... ;

...

seed &amp;amp;seed;

run;

%END;
%MEND DoLoop;
%DoLoop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also have to question if macro randbetween is necessary at all.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 10:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396253#M278115</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-15T10:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro a macro value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396257#M278116</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;that's running now. I just have one question to help my understanding of the code - what does the code 'best.' do?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 11:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396257#M278116</guid>
      <dc:creator>aoifeoneill</dc:creator>
      <dc:date>2017-09-15T11:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro a macro value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396261#M278117</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put(x+&amp;amp;j,best.)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;converts the numeric values derived from the summation to a character value, as call symput expects two strings as arguments; otherwise you'd get a NOTE about the type conversion, and I don't like them in my codes (I even have measures to search for such NOTEs in log files and throw errors in batch jobs, as undetected type mismatches can cause logical havoc).&lt;/P&gt;
&lt;P&gt;best. is just a default format that will work basically all the time and create a string that SAS will always recognize as a number when the macro variable is resolved.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 11:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396261#M278117</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-15T11:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro a macro value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396266#M278118</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;for all your help!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 11:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396266#M278118</guid>
      <dc:creator>aoifeoneill</dc:creator>
      <dc:date>2017-09-15T11:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro a macro value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396378#M278119</link>
      <description>&lt;P&gt;To resolve data step functions such as Floor or Rand in macros you must use the macro function %sysfunc for &lt;STRONG&gt;each&lt;/STRONG&gt; function called. Also the macro language doesn't use the the quoted literal strings such as the parameter to Rand quite the same way since it expects text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This does what you were attempting:&lt;/P&gt;
&lt;PRE&gt;%macro RandBetween(min, max);
%let x =  %eval(&amp;amp;min + %sysfunc ( floor((1+&amp;amp;max-&amp;amp;min)* %sysfunc(rand(uniform)) )) );
%put &amp;amp;x;
%mend;

%RandBetween(1,50);&lt;/PRE&gt;
&lt;P&gt;You can see that complex operations involving multiple functions can get quite ugly quickly. I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;that datasteps often are much easier to understand and debug than doing things directly in the macro language for many operations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 15:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-a-macro-value/m-p/396378#M278119</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-09-15T15:02:05Z</dc:date>
    </item>
  </channel>
</rss>

