<?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: SAS 'remembers' the last value? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29407#M5529</link>
    <description>Thanks guys. &lt;BR /&gt;
Your replies are very helpful.  It is clear to me now. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
    <pubDate>Fri, 11 Dec 2009 17:31:33 GMT</pubDate>
    <dc:creator>Fisher</dc:creator>
    <dc:date>2009-12-11T17:31:33Z</dc:date>
    <item>
      <title>SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29401#M5523</link>
      <description>I have this simple data step to assign a value to a macro variable:&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt;data _null_;  	&lt;BR /&gt;
 &lt;BR /&gt;
      call symput("para1", 'hello'); &lt;BR /&gt;
&lt;BR /&gt;
      putlog "para1 is &amp;amp;para1";&lt;BR /&gt;
	  &lt;BR /&gt;
run;&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
As you expect, when I run this code, in the log window, 'para1 is hello' is displayed.&lt;BR /&gt;
The weird thing is, if you change the value 'hello' to, for example, 'world' and run the code again, what you get is still 'para1 is hello', not 'para1 is world' as expected. But if you run the codes for the second time, then 'para1 is world' is displayed in the log window, which is what is expected.&lt;BR /&gt;
This is very confusing to me, seems the SAS :remembers" or "caches" the last value, what is the problem with it?&lt;BR /&gt;
Anyone can give me a piece of light on this problem? thanks. &lt;BR /&gt;
&lt;BR /&gt;
Eric&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: EricSK

Message was edited by: EricSK</description>
      <pubDate>Fri, 11 Dec 2009 16:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29401#M5523</guid>
      <dc:creator>Fisher</dc:creator>
      <dc:date>2009-12-11T16:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29402#M5524</link>
      <description>Correction - on the "first" execution you should get a SAS warning because during the DATA step execution, the SAS macro variable PARA1 does not have an assigned value - see code log below:&lt;BR /&gt;
&lt;BR /&gt;
1    data _null_;&lt;BR /&gt;
2&lt;BR /&gt;
3    call symput("para1", 'hello');&lt;BR /&gt;
4&lt;BR /&gt;
5    putlog "para1 is &amp;amp;para1";&lt;BR /&gt;
WARNING: Apparent symbolic reference PARA1 not resolved.&lt;BR /&gt;
6&lt;BR /&gt;
7    run;&lt;BR /&gt;
&lt;BR /&gt;
para1 is &amp;amp;para1&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.06 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
So, what you should consider doing is reading up in the SAS.COM website after searching for the argument keywords "macro variable scope".  Or consider using Google advanced search argument below against the SAS.COM website:&lt;BR /&gt;
&lt;BR /&gt;
macro variable scope site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
Normally, SAS macro variable resolution will occur at SAS compilation time and value assignments will be reflected *AFTER* the DATA or PROC step is completed.  By including a RUN; or QUIT;  statement, you ensure that SAS compiles your program up to that point.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 11 Dec 2009 16:33:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29402#M5524</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-11T16:33:08Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29403#M5525</link>
      <description>Scott is right.&lt;BR /&gt;
&lt;BR /&gt;
For macro assignment/resolution AT runtime you should use the symput and symget family functions/routines.&lt;BR /&gt;
&lt;BR /&gt;
See the only doc at SAS:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000210322.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000210322.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000127861.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000127861.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Fri, 11 Dec 2009 16:41:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29403#M5525</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-12-11T16:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29404#M5526</link>
      <description>Thanks for the responses, guys.&lt;BR /&gt;
Sorry, I didn't make it clear. Copy this codes and run it first:&lt;BR /&gt;
&lt;BR /&gt;
1 data x;&lt;BR /&gt;
2   x=1;&lt;BR /&gt;
3   call symput('var','hello');&lt;BR /&gt;
4  *putlog "para is: &amp;amp;var";&lt;BR /&gt;
5  run;&lt;BR /&gt;
&lt;BR /&gt;
at this step, there should be no problem. Then uncomment out the line 4 and run the codes again, 'para is: hello' is displayed in the log window, which is expected.&lt;BR /&gt;
The weird thing I mean is, if you change the 'hello' to 'world' and run the codes again, you will still get the 'para is: hello', but not 'para is: world' until you run the codes for the second times.</description>
      <pubDate>Fri, 11 Dec 2009 17:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29404#M5526</guid>
      <dc:creator>Fisher</dc:creator>
      <dc:date>2009-12-11T17:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29405#M5527</link>
      <description>It is all about what happens when the step is compiled vs executed.  Look back at macro documention but this example may help you understand.    Once you get it you can't forget it, but it may not be obvious to those new to SAS and SAS/MACRO.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
3095  data x;&lt;BR /&gt;
3096     x=1;&lt;BR /&gt;
3097     call symput('var','hello');&lt;BR /&gt;
3098     varAtExecution=symget('VAR');&lt;BR /&gt;
3099     varAtCompile="&amp;amp;var";&lt;BR /&gt;
WARNING: Apparent symbolic reference VAR not resolved.&lt;BR /&gt;
3100     putlog "para at complile is: &amp;amp;var";&lt;BR /&gt;
WARNING: Apparent symbolic reference VAR not resolved.&lt;BR /&gt;
3101     putlog 'para at execution is' varAtExecution=;&lt;BR /&gt;
3102&lt;BR /&gt;
3103     run;&lt;BR /&gt;
&lt;BR /&gt;
para at complile is: &amp;amp;var&lt;BR /&gt;
para at execution isvarAtExecution=hello&lt;BR /&gt;
NOTE: The data set WORK.X has 1 observations and 3 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.05 seconds&lt;BR /&gt;
      cpu time            0.02 seconds&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
3104  data x;&lt;BR /&gt;
3105     x=1;&lt;BR /&gt;
3106     call symput('var','hello');&lt;BR /&gt;
3107     varAtExecution=symget('VAR');&lt;BR /&gt;
3108     varAtCompile="&amp;amp;var";&lt;BR /&gt;
3109     putlog "para at complile is: &amp;amp;var";&lt;BR /&gt;
3110     putlog 'para at execution is' varAtExecution=;&lt;BR /&gt;
3111&lt;BR /&gt;
3112     run;&lt;BR /&gt;
&lt;BR /&gt;
para at complile is: hello&lt;BR /&gt;
para at execution isvarAtExecution=hello&lt;BR /&gt;
NOTE: The data set WORK.X has 1 observations and 3 variables.&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 11 Dec 2009 17:21:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29405#M5527</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-12-11T17:21:49Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29406#M5528</link>
      <description>OK, I posted at the same time data _null_ did, but the content is similar to it's post.&lt;BR /&gt;
&lt;BR /&gt;
So you've got there you're explanation &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;

Message was edited by: Daniel Santos</description>
      <pubDate>Fri, 11 Dec 2009 17:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29406#M5528</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-12-11T17:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29407#M5529</link>
      <description>Thanks guys. &lt;BR /&gt;
Your replies are very helpful.  It is clear to me now. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 11 Dec 2009 17:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29407#M5529</guid>
      <dc:creator>Fisher</dc:creator>
      <dc:date>2009-12-11T17:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29408#M5530</link>
      <description>The whole problem boils down to where the macro variable is examined.  In the on-line SAS documentation it states: &lt;BR /&gt;
"You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) in which SYMPUT creates that macro variable and assigns it a value."&lt;BR /&gt;
&lt;BR /&gt;
You need to wait until after your RUN: statement to examine the macro variable.</description>
      <pubDate>Fri, 11 Dec 2009 17:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29408#M5530</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-12-11T17:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29409#M5531</link>
      <description>To EricSK - read the suggested documentation and learn about how SAS macro variables get resolved and when.  Also, read clearly the replies -- the exact code you executed is what I executed -- however I had started a new/fresh SAS session and so there were no prior GLOBAL MACRO VARIABLES around to prop-up your DATA step logic.&lt;BR /&gt;
&lt;BR /&gt;
Take the opportunity to read and learn - it will help enhance your SAS experience up-front rather than taking yours and others' time on the forums.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 11 Dec 2009 17:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29409#M5531</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-11T17:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'remembers' the last value?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29410#M5532</link>
      <description>To sbb: thanks for your time and recommendation. I don't expect to waste anyone's time, I will be more careful next time when I think posting a question here.</description>
      <pubDate>Fri, 11 Dec 2009 18:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-remembers-the-last-value/m-p/29410#M5532</guid>
      <dc:creator>Fisher</dc:creator>
      <dc:date>2009-12-11T18:28:07Z</dc:date>
    </item>
  </channel>
</rss>

