<?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: creating a usable macro variable in a do loop.. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70343#M15223</link>
    <description>Linus - that works great in this example - I'll try it in my main code.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Cynthia, in the sample I provided a very basic version of what I am trying to achieve.  Basically there are a lot more variables in my working example e.g. M1-M90 and the number changes depending on when the code is run.&lt;BR /&gt;
&lt;BR /&gt;
I have been asked to design all my code so that the users, who are not technical, can simply open and run without having to input any dates, or amend anything.  I have managed to get everything else automated and just need to create the field M1-Mn where I have created n as a global variable based on date.&lt;BR /&gt;
&lt;BR /&gt;
I think I have everything I need now thanks to these forums.</description>
    <pubDate>Tue, 20 Jan 2009 16:41:27 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-01-20T16:41:27Z</dc:date>
    <item>
      <title>creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70338#M15218</link>
      <description>I'm stumped.&lt;BR /&gt;
&lt;BR /&gt;
In the example below i have created the variables B1, B2, B3, B4 and B5. They have the values 1,5,2,6 and 3 respectively.&lt;BR /&gt;
&lt;BR /&gt;
I have also created the variables M1, M2, M3, M4 and M5, using an array, which I want to take the following values.&lt;BR /&gt;
&lt;BR /&gt;
M1=B1;&lt;BR /&gt;
M2=max(of B1-B2);&lt;BR /&gt;
M3=max(of B1-B3) etc...&lt;BR /&gt;
&lt;BR /&gt;
I need to do this within the do loop for various reasons which I won't go into now. Anyway, this code looks to me like it should work fine. But it doesn't. It looks like the Call Symput doesn't create the global variable &amp;amp;bobno until the datastep has finished running, I need the variable to be created during the do loop to ensure that the correct max(B1-B&amp;amp;bobno) is calculated to give the correct value in M1-M5.&lt;BR /&gt;
&lt;BR /&gt;
Help?&lt;BR /&gt;
&lt;BR /&gt;
Sample Code:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data play;&lt;BR /&gt;
&lt;BR /&gt;
B1=1;&lt;BR /&gt;
B2=5;&lt;BR /&gt;
B3=2;&lt;BR /&gt;
B4=6;&lt;BR /&gt;
B5=3;&lt;BR /&gt;
&lt;BR /&gt;
array maxi(5) M1-M5;&lt;BR /&gt;
&lt;BR /&gt;
do i=1 to 5;&lt;BR /&gt;
call symput ('bobno',compress('B'||i));&lt;BR /&gt;
maxi(i)=max(of B1-&amp;amp;bobno);&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
run;

Amended as I pasted in the wrong code.&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: cstanyer</description>
      <pubDate>Tue, 20 Jan 2009 14:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70338#M15218</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-20T14:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70339#M15219</link>
      <description>I get a SAS error when I execute your "sample" code.  Best to share more about your actual SAS code and a SASLOG error problem excerpt.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 20 Jan 2009 14:55:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70339#M15219</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-01-20T14:55:44Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70340#M15220</link>
      <description>I too get an error, which is why I am here.&lt;BR /&gt;
&lt;BR /&gt;
In theory, this do loop would work if the call symput assigned during the processing. It appears that it does not until the datastep is complete.&lt;BR /&gt;
&lt;BR /&gt;
Using a %let doesn't work either as that doesn't resolve the compress function.</description>
      <pubDate>Tue, 20 Jan 2009 15:17:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70340#M15220</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-20T15:17:27Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70341#M15221</link>
      <description>No, call symput will not be executed until after the data step have finished, so there little you can do about changing the syntax within the data step itself.&lt;BR /&gt;
&lt;BR /&gt;
Try this, does it suit your needs?&lt;BR /&gt;
&lt;BR /&gt;
data play;&lt;BR /&gt;
&lt;BR /&gt;
B1=1;&lt;BR /&gt;
B2=5;&lt;BR /&gt;
B3=2;&lt;BR /&gt;
B4=6;&lt;BR /&gt;
B5=3;&lt;BR /&gt;
array b(5) b1-b5;&lt;BR /&gt;
array maxi(5) M1-M5;&lt;BR /&gt;
&lt;BR /&gt;
m1=b1;&lt;BR /&gt;
do i=2 to 5;&lt;BR /&gt;
maxi(i)=max(maxi(i-1),b(i));&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Tue, 20 Jan 2009 15:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70341#M15221</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-01-20T15:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70342#M15222</link>
      <description>Hi:&lt;BR /&gt;
  Just as you use CALL SYMPUT to write to the Global Symbol table from a "running" Data Step program, you generally use SYMGET to retrieve values from the Global Symbol table inside a "running" Data Step program.&lt;BR /&gt;
 &lt;BR /&gt;
  The reference to &amp;amp;bobno, will only resolve 1 time -- at compile time. So whatever value you are seeing is either left over from the previous DATA step execution or, if there is no value for &amp;amp;bobno at the first compile, then you are probably getting a macro variable not found error. &lt;BR /&gt;
&lt;BR /&gt;
  I am a bit confused over WHY you are trying to generate this using macro variables. Is this really the code you envision:&lt;BR /&gt;
[pre]&lt;BR /&gt;
m1 = max(of B1-B1);&lt;BR /&gt;
m2 = max(of B1-B2);&lt;BR /&gt;
m3 = max(of B1-B3);&lt;BR /&gt;
m4 = max(of B1-B4);&lt;BR /&gt;
m5 = max(of B1-B5);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
I think the statement for M1 will give you problems because B1-B1 is not a proper range of numbers for the OF usage. I just don't see how macro variables fit into the picture.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 20 Jan 2009 16:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70342#M15222</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-01-20T16:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70343#M15223</link>
      <description>Linus - that works great in this example - I'll try it in my main code.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Cynthia, in the sample I provided a very basic version of what I am trying to achieve.  Basically there are a lot more variables in my working example e.g. M1-M90 and the number changes depending on when the code is run.&lt;BR /&gt;
&lt;BR /&gt;
I have been asked to design all my code so that the users, who are not technical, can simply open and run without having to input any dates, or amend anything.  I have managed to get everything else automated and just need to create the field M1-Mn where I have created n as a global variable based on date.&lt;BR /&gt;
&lt;BR /&gt;
I think I have everything I need now thanks to these forums.</description>
      <pubDate>Tue, 20 Jan 2009 16:41:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70343#M15223</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-20T16:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70344#M15224</link>
      <description>I really was trying to overcomplicate everything wasn't I?&lt;BR /&gt;
&lt;BR /&gt;
Thanks again.</description>
      <pubDate>Tue, 20 Jan 2009 16:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70344#M15224</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-01-20T16:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: creating a usable macro variable in a do loop..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70345#M15225</link>
      <description>Great! Glad you have a solution that works for you.&lt;BR /&gt;
 &lt;BR /&gt;
 I'd like to clarify something for future reference. CALL SYMPUT executes on every iteration of the DATA step program to CREATE a macro variable -- at execution time (or in your case, for every iteration of the DO loop). An "&amp;amp;macvar" reference only gets resolved at COMPILE time. That means if you use CALL SYMPUT and an &amp;amp;macvar reference, you have a timing issue because the &amp;amp;macvar will be resolved, if possible BEFORE the CALL SYMPUT executes. &lt;BR /&gt;
&lt;BR /&gt;
Usually, you don't CREATE a macro variable and then REFERENCE that macro variable with an &amp;amp;macvar reference in the same program.&lt;BR /&gt;
&amp;amp;bobno will not be available for compile time resolution until after the creating datastep is over, as described here:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/23/182.html" target="_blank"&gt;http://support.sas.com/kb/23/182.html&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/22/987.html" target="_blank"&gt;http://support.sas.com/kb/22/987.html&lt;/A&gt; &lt;BR /&gt;
          &lt;BR /&gt;
--and when it IS available, it will ALWAYS be whatever value was the LAST value assigned in the CALL SYMPUT -- which in your case would always be B5.&lt;BR /&gt;
                &lt;BR /&gt;
The documentation on CALL SYMPUT and SYMGET are here. The SYMGET doc has a good example of using SYMGET from a previously SYMPUT-created variable:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000210266.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000210266.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000210322.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/59526/HTML/default/a000210322.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
This program shows an interesting use of CALL SYMPUT and SYMGET that proves the macro variable 'bobno' is getting set appropriately for every iteration of the DO loop -- it also proves that at the end of the do loop, 'bobno' is set to B5. Usually, when you have a DO loop and a CALL SYMPUT, you are creating numbered macro variables (also shown):&lt;BR /&gt;
[pre]&lt;BR /&gt;
421  data play;&lt;BR /&gt;
422&lt;BR /&gt;
423     do i=1 to 5;&lt;BR /&gt;
424       call symput ('bobno',compress('B'||i));&lt;BR /&gt;
425       val = symget('bobno');&lt;BR /&gt;
426       call symput ('bobno'||put(i,1.0),compress('B'||i));&lt;BR /&gt;
427       put _all_;&lt;BR /&gt;
428&lt;BR /&gt;
429     end;&lt;BR /&gt;
430&lt;BR /&gt;
431  run;&lt;BR /&gt;
                                      &lt;BR /&gt;
NOTE: Numeric values have been converted to character values at the places given by:&lt;BR /&gt;
      (Line):(Column).&lt;BR /&gt;
      424:41   426:53&lt;BR /&gt;
i=1 val=B1 _ERROR_=0 _N_=1&lt;BR /&gt;
i=2 val=B2 _ERROR_=0 _N_=1&lt;BR /&gt;
i=3 val=B3 _ERROR_=0 _N_=1&lt;BR /&gt;
i=4 val=B4 _ERROR_=0 _N_=1&lt;BR /&gt;
i=5 val=B5 _ERROR_=0 _N_=1&lt;BR /&gt;
NOTE: The data set WORK.PLAY has 1 observations and 2 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
                                    &lt;BR /&gt;
                                  &lt;BR /&gt;
432&lt;BR /&gt;
433  %put after data step: bobno = &amp;amp;bobno;&lt;BR /&gt;
after data step: bobno = B5&lt;BR /&gt;
434  %put bobno1 = &amp;amp;bobno1;&lt;BR /&gt;
bobno1 = B1&lt;BR /&gt;
435  %put bobno2 = &amp;amp;bobno2;&lt;BR /&gt;
bobno2 = B2&lt;BR /&gt;
436  %put bobno3 = &amp;amp;bobno3;&lt;BR /&gt;
bobno3 = B3&lt;BR /&gt;
437  %put bobno4 = &amp;amp;bobno4;&lt;BR /&gt;
bobno4 = B4&lt;BR /&gt;
438  %put bobno5 = &amp;amp;bobno5;&lt;BR /&gt;
bobno5 = B5&lt;BR /&gt;
                                                                                                  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                                                                        &lt;BR /&gt;
Note that when using SYMGET to create the variable VAL, VAL changes for every iteration of the DO loop; in a similar fashion VAL &lt;BR /&gt;
would change on every iteration of a DATA step program. The usual way that macro variables are set in a DO loop are to use i as part of &lt;BR /&gt;
the macro variable value (as shown). The FINAL value of bobno is B5...because at every iteration of the do loop, you are writing over the &lt;BR /&gt;
previously set value.&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 20 Jan 2009 17:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-a-usable-macro-variable-in-a-do-loop/m-p/70345#M15225</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-01-20T17:24:08Z</dc:date>
    </item>
  </channel>
</rss>

