<?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: Retain vs. Retain 0 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917214#M361303</link>
    <description>The documentation has this information. Read: &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;From the doc: This RETAIN statement retains the values of nine variables and sets their initial values:&lt;BR /&gt;retain month1-month5 1 year 0 a b c 'XYZ';&lt;BR /&gt;The values of MONTH1 through MONTH5 are set initially to 1; YEAR is set to 0; variables A, B, and C are each set to the character value XYZ.&lt;BR /&gt;If you omit initial-value, the initial value is missing.</description>
    <pubDate>Wed, 21 Feb 2024 18:45:22 GMT</pubDate>
    <dc:creator>JosvanderVelden</dc:creator>
    <dc:date>2024-02-21T18:45:22Z</dc:date>
    <item>
      <title>Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917209#M361300</link>
      <description>&lt;P&gt;Can someone please explain nicely and with examples the difference between RETAIN and RETAIN 0? For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#0000FF"&gt;RETAIN var1;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;EM&gt; vs.&lt;/EM&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#0000FF"&gt;RETAIN var1 0;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 18:36:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917209#M361300</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2024-02-21T18:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917212#M361301</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/235176"&gt;@pink_poodle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can someone please explain nicely and with examples the difference between RETAIN and RETAIN 0? For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#0000FF"&gt;RETAIN var1;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;EM&gt; vs.&lt;/EM&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#0000FF"&gt;RETAIN var1 0;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your second version provides an initial value of 0.&amp;nbsp; Otherwise it is initialized to a missing value (blanks for character vars, . for numerics):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length x $5;
  retain x;
  retain y 0;
  retain z;
  put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Feb 2024 18:41:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917212#M361301</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-21T18:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917214#M361303</link>
      <description>The documentation has this information. Read: &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0t2ac0tfzcgbjn112mu96hkgg9o.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;From the doc: This RETAIN statement retains the values of nine variables and sets their initial values:&lt;BR /&gt;retain month1-month5 1 year 0 a b c 'XYZ';&lt;BR /&gt;The values of MONTH1 through MONTH5 are set initially to 1; YEAR is set to 0; variables A, B, and C are each set to the character value XYZ.&lt;BR /&gt;If you omit initial-value, the initial value is missing.</description>
      <pubDate>Wed, 21 Feb 2024 18:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917214#M361303</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2024-02-21T18:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917216#M361304</link>
      <description>&lt;P&gt;When you use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain var1 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It will retain var1, and set the initial value to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to set the initial value to 0 you would use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain var1 0 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1 ;
  set sashelp.class ;
  retain counter  ;

  put _n_= "before incrementing" counter= ;
  counter=sum(counter,1) ; 
  put _n_= "after incrementing" counter= ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will see that the first PUT statement shows counter has a missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 ;
  set sashelp.class ;
  retain counter 0;

  put _n_= "before incrementing" counter= ;
  counter=sum(counter,1) ; 
  put _n_= "after incrementing" counter= ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first PUT statement will show counter has the value 0.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 18:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917216#M361304</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-02-21T18:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917218#M361305</link>
      <description>&lt;P&gt;RETAIN on its own does not set any attributes of the variable. RETAIN 0 defines it as numeric and initializes it to zero.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 18:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917218#M361305</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-02-21T18:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917418#M361392</link>
      <description>I get this in the log:&lt;BR /&gt;NOTE: Variable x is uninitialized.&lt;BR /&gt;NOTE: Variable z is uninitialized.&lt;BR /&gt;x=  y=0 z=.</description>
      <pubDate>Thu, 22 Feb 2024 18:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917418#M361392</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2024-02-22T18:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917426#M361394</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA demo;
    LENGTH a b c d ac bc cc dc 8.;
	RETAIN a ; *same as RETAIN a .;
	RETAIN b 0;
	RETAIN c 10;
	*NOTE: doing nothing to variable d (default);

	/* Initialization proceeds as follows */
	ac = a + 1; *_N_ = 1: a = . (the value you provided initially);
				*_N_ = 2: a = . (the value retained from previous step);
				*_N_ = 3: a = 5 (the value retained from previous step);

	bc = b + 1; *_N_ = 1: b = 0 (the value you provided initially);
				*_N_ = 2: b = .	(the value retained from previous step);
				*_N_ = 3: b = 5 (the value retained from previous step);

	cc = c + 1; *_N_ = 1: c = 10 (the value you provided initially);
				*_N_ = 2: c = .	(the value retained from previous step);
				*_N_ = 3: c = 5 (the value retained from previous step);
    
	dc = d + 1; *_N_ = 1: d = . (default behavior: initialized to . beginning every step);
			    *_N_ = 2: d = . (default behavior: initialized to . beginning every step);
				*_N_ = 3: d = . (default behavior: initialized to . beginning every step);

	INPUT a b c d;
	DATALINES;
	. . . .
	5 5 5 5
	. . . .
	;
RUN;

PROC PRINT DATA = demo NOOBS;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="pink_poodle_0-1708629462234.png" style="width: 359px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94009iABF431AC463BFAF3/image-dimensions/359x177?v=v2" width="359" height="177" role="button" title="pink_poodle_0-1708629462234.png" alt="pink_poodle_0-1708629462234.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 19:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917426#M361394</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2024-02-22T19:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Retain vs. Retain 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917431#M361396</link>
      <description>&lt;P&gt;You should normally avoid using RETAIN for variables that you are actually READING in with an INPUT statement.&amp;nbsp; The only case where that makes sense is when the INPUT statement is executed CONDITIONALLY so values are only read in some times so that the retained value can be used in the other cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A better way to see what is happening is to look at the values during the data step itself.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA demo;
  LENGTH a b c d ac bc cc dc 8.;
	RETAIN a ; *same as RETAIN a .;
	RETAIN b 0;
	RETAIN c 10;
	*NOTE: doing nothing to variable d (default);
if _n_=1 then put '  A  B  C  D AC BC CC DC';
put _n_=;
put (_all_) (3.) ' &amp;lt;- START';
	/* Initialization proceeds as follows */
	ac = a + 1; *_N_ = 1: a = . (the value you provided initially);
				*_N_ = 2: a = . (the value retained from previous step);
				*_N_ = 3: a = 5 (the value retained from previous step);

	bc = b + 1; *_N_ = 1: b = 0 (the value you provided initially);
				*_N_ = 2: b = .	(the value retained from previous step);
				*_N_ = 3: b = 5 (the value retained from previous step);

	cc = c + 1; *_N_ = 1: c = 10 (the value you provided initially);
				*_N_ = 2: c = .	(the value retained from previous step);
				*_N_ = 3: c = 5 (the value retained from previous step);
    
	dc = d + 1; *_N_ = 1: d = . (default behavior: initialized to . beginning every step);
			    *_N_ = 2: d = . (default behavior: initialized to . beginning every step);
				*_N_ = 3: d = . (default behavior: initialized to . beginning every step);
put (_all_) (3.) ' &amp;lt;- AFTER IF';
	INPUT a b c d;
put (_all_) (3.) ' &amp;lt;- AFTER INPUT';
DATALINES;
. . . .
5 5 5 5
. . . .
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;  A  B  C  D AC BC CC DC
_N_=1
  .  0 10  .  .  .  .  . &amp;lt;- START
  .  0 10  .  .  1 11  . &amp;lt;- AFTER IF
  .  .  .  .  .  1 11  . &amp;lt;- AFTER INPUT
_N_=2
  .  .  .  .  .  .  .  . &amp;lt;- START
  .  .  .  .  .  .  .  . &amp;lt;- AFTER IF
  5  5  5  5  .  .  .  . &amp;lt;- AFTER INPUT
_N_=3
  5  5  5  .  .  .  .  . &amp;lt;- START
  5  5  5  .  6  6  6  . &amp;lt;- AFTER IF
  .  .  .  .  6  6  6  . &amp;lt;- AFTER INPUT
_N_=4
  .  .  .  .  .  .  .  . &amp;lt;- START
  .  .  .  .  .  .  .  . &amp;lt;- AFTER IF
&lt;/PRE&gt;
&lt;P&gt;Notice also that the data steps ends on the 4th iteration when the INPUT statement reads past the end of the datalines.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 19:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-vs-Retain-0/m-p/917431#M361396</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-22T19:53:55Z</dc:date>
    </item>
  </channel>
</rss>

