<?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: Scope of global variable assignment in DS2 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556506#M155017</link>
    <description>Many thanks.&lt;BR /&gt;</description>
    <pubDate>Mon, 06 May 2019 17:39:02 GMT</pubDate>
    <dc:creator>tomcookbdp</dc:creator>
    <dc:date>2019-05-06T17:39:02Z</dc:date>
    <item>
      <title>Scope of global variable assignment in DS2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556161#M154902</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am relatively new to DS2 and have a question about the scope of global variable assignments. See the following program.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc ds2; 
    data _null_; 
        declare int x;  /* global x in global scope */ 
        declare int y;  /* global y in global scope */ 
/*         retain x y; */
        
    method init();   
        declare int x;  /* local x in local scope */   
        x = 5;          /* local x assigned 5 */   
        y = 6;          /* global y assigned 6 */   
        put '0. in init() ' x= y=; 
    end; 

    method run();    
        put '1. in run()  ' x= y=;         
        x = 1;    
        put '2. in run()  ' x= y=; 
    end; 

    method term(); 
        put '3. in term()  ' x= y=;  
    end; 
    enddata; 
run; 
quit; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is the log:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;PRE&gt;&lt;SPAN class="s"&gt;0. in init()  x=5 y=6&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="s"&gt;1. in run()   x= y=6&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="s"&gt;2. in run()   x=1 y=6&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="s"&gt;3. in term()   x= y=&lt;BR /&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Both x and y are global variables. Variable y is assigned a value of 6 in the init() method and this value is still present in the run() method, as I would expect. However, y no longer has the assigned value in the term() method. Why is this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By the way, explicitly retaining y seems to solve the problem, but I don't understand why this is necessary. Can anyone explain?&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Sat, 04 May 2019 09:19:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556161#M154902</guid>
      <dc:creator>tomcookbdp</dc:creator>
      <dc:date>2019-05-04T09:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: Scope of global variable assignment in DS2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556215#M154938</link>
      <description>&lt;P&gt;The TERM() method automatically resets global variables to uninitialized values.&lt;/P&gt;
&lt;P&gt;There are a few situations where this will not occur, pre-defined variables, such as _N_, accumulator variables used in sum statements, variables specified in a RETAIN statement and package variables will not automatically be reset to uninitialized values in the TERM() method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 20:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556215#M154938</guid>
      <dc:creator>ChrisLysholm</dc:creator>
      <dc:date>2019-05-04T20:16:19Z</dc:date>
    </item>
    <item>
      <title>Re: Scope of global variable assignment in DS2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556506#M155017</link>
      <description>Many thanks.&lt;BR /&gt;</description>
      <pubDate>Mon, 06 May 2019 17:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Scope-of-global-variable-assignment-in-DS2/m-p/556506#M155017</guid>
      <dc:creator>tomcookbdp</dc:creator>
      <dc:date>2019-05-06T17:39:02Z</dc:date>
    </item>
  </channel>
</rss>

