<?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 statement makes me confusing.. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489459#M127806</link>
    <description>&lt;P&gt;Hi: Here's a concrete example of what happens without the retain, using this test data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a1;
  infile datalines;
  input id $ x;
datalines;
A01 11 
A02 13 
A03 14 
A04 16 
A05  6 
A06 15 
A07 12 
A08 14 
A09  5 
A10 12 
;
run;

proc print data=a1;
  title 'What is in A1';
run;

data a2; 
  set a1;
retain sumx 0;
sumx=sumx+x;
run;

proc print data=a2;
  title 'What is in A2 when using RETAIN';
run;

data a3;
  set a1;
  sumx = sumx+x;
run;

proc print data=a3;
  title 'What is in A3 withOUT using RETAIN';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And, here are results from dataset A2 vs dataset A3:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="retain_vs_no_retain.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22731iD2E98E39CC80B431/image-size/large?v=v2&amp;amp;px=999" role="button" title="retain_vs_no_retain.png" alt="retain_vs_no_retain.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps explain it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
    <pubDate>Fri, 24 Aug 2018 00:47:59 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2018-08-24T00:47:59Z</dc:date>
    <item>
      <title>Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489445#M127797</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2; set a1;
retain sumx 0;
sumx=sumx+x;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Dear friends,&lt;/P&gt;&lt;P&gt;what I was told about retain is that it initializes the variable to zero.&lt;/P&gt;&lt;P&gt;When used with 'set' statement, since set reads records one by one,&lt;/P&gt;&lt;P&gt;in the example above, records of dataset a1 will flow into dataset a2, will iterate through records and add them up. (sumx=sum+x;)&lt;/P&gt;&lt;P&gt;Now, this is the confusing part. sumx=sumx+x; gets executed every time, and what about retain?&lt;/P&gt;&lt;P&gt;Is it re-initialized to zero every time? I know it's not but its name - RETAIN - is pretty much confusing.&lt;/P&gt;&lt;P&gt;Does it really have a function that has to do with RETAINING the values within?&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 23:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489445#M127797</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2018-08-23T23:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489449#M127800</link>
      <description>&lt;P&gt;The best way to answer (and to memorise the answer) this type of question is to try.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you remove the &lt;FONT face="courier new,courier"&gt;retain&lt;/FONT&gt; in your data step, you'll see that&amp;nbsp;SUMX is reset to missing with each iteration (in other words, it i&lt;SPAN&gt;s reset to missing with&amp;nbsp;&lt;/SPAN&gt;each new observation in A1). That's the default behaviour.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Retain&lt;/FONT&gt; is a compile-time instruction that instructs SAS&amp;nbsp; to&amp;nbsp;&lt;STRONG&gt;*not*&lt;/STRONG&gt;&amp;nbsp;reset variables when iterating the data step.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 23:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489449#M127800</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-23T23:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489459#M127806</link>
      <description>&lt;P&gt;Hi: Here's a concrete example of what happens without the retain, using this test data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a1;
  infile datalines;
  input id $ x;
datalines;
A01 11 
A02 13 
A03 14 
A04 16 
A05  6 
A06 15 
A07 12 
A08 14 
A09  5 
A10 12 
;
run;

proc print data=a1;
  title 'What is in A1';
run;

data a2; 
  set a1;
retain sumx 0;
sumx=sumx+x;
run;

proc print data=a2;
  title 'What is in A2 when using RETAIN';
run;

data a3;
  set a1;
  sumx = sumx+x;
run;

proc print data=a3;
  title 'What is in A3 withOUT using RETAIN';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And, here are results from dataset A2 vs dataset A3:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="retain_vs_no_retain.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22731iD2E98E39CC80B431/image-size/large?v=v2&amp;amp;px=999" role="button" title="retain_vs_no_retain.png" alt="retain_vs_no_retain.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps explain it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 00:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489459#M127806</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-08-24T00:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489460#M127807</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215463"&gt;@jimmychoi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;what I was told about retain is that it initializes the variable to zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Start with the documentation instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a good page to bookmark, it lets you quickly launch to any documentation you need easily:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=pgmsashome&amp;amp;docsetTarget=home.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=pgmsashome&amp;amp;docsetTarget=home.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain is a statement, so click on STATEMENTS and then R, then RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;Specifies that all columns or all columns in the column list will have their values retained between executions of the RUN method.&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In fact,&amp;nbsp;initialization is not the primary function of RETAIN at all, its a nice to have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to&amp;nbsp;initialize variables, use this method instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if _n_=1 then do;
    x=1; y=2;
end;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Aug 2018 00:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489460#M127807</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-24T00:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489488#M127823</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This statement in the Viya docs you're citing, "Specifies that all columns or all columns in the column list will have their values retained between executions of the RUN method." is confusing to the core. To anyone capable of reading plain English it would mean that the values will remain unchanged between the executions no matter what - which is absolutely NOT the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With respect to the unfortunate RETAIN statement, SAS has never fixed its docs since Version 5 (and perhaps earilier) to plainly say that RETAIN does not &lt;EM&gt;retain&lt;/EM&gt; anything but merely prevents certain variables from being automatically reset to missing values at the top of the implied loop. I suspect this confusion will never end.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 04:13:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489488#M127823</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-24T04:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489539#M127861</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;: Many thanks for sharing this helpful link.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, some of the "quick links" there lead to pages where DATA Step documentation is interspersed with DS2, FedSQL and other special documentation. (This is of course the same issue as with search results on support.sas.com and also with the built-in help files since the advent of DS2 etc.) As a consequence, many keywords occur with two or more meanings, i.e. there are multiple links for the same keyword.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is too easy to accidentally click the wrong link, as you have demonstrated. The &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=p0t2ac0tfzcgbjn112mu96hkgg9o.htm&amp;amp;locale=en" target="_blank"&gt;"SAS DATA Step Statements: Reference"&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;says about RETAIN:&lt;/SPAN&gt;&lt;/P&gt;
&lt;H1 id="p0t2ac0tfzcgbjn112mu96hkgg9o" class="xis-title"&gt;&lt;FONT size="3"&gt;&lt;STRONG&gt;RETAIN Statement&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/H1&gt;
&lt;P class="xis-shortDescription"&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;Causes a variable that is created by an INPUT or assignment statement to retain its value from one iteration of the DATA step to the next.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And this sounds much more familiar than the description of the DS2 statement of the same name.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 10:22:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489539#M127861</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-24T10:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489621#M127889</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;The first time I searched RETAIN I think it returned something in the TTEST docs so I totally understand that. I've been finding searches to be useless both via Google or SAS site, it seems you need to know exactly where to click/look.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;The language could be clearer, no disagreement there.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 14:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489621#M127889</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-24T14:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489638#M127896</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My dog-eared SAS User's Guide: Basics Version 5 Edition says:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"The RETAIN statement causes a variable created by an INPUT statement or assignment statement to retain its value from the previous iteration of the DATA step." The only change in 2018 from 1985 is "from one iteration ... to the next" instead of "from the previous iteration".&amp;nbsp;But at least the old manual states in the &lt;EM&gt;very next&lt;/EM&gt; sentence: "Without a RETAIN statement, the SAS System automatically sets variables created by INPUT or assignment statements to missing&lt;EM&gt; before&lt;/EM&gt; each iteration of the DATA step.", so an alert reader would hopefully understand right there and then that RETAIN merely turns the automatic clean-up action off.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The statement is still not entirely accurate since it doesn't tell the &lt;EM&gt;whole truth&lt;/EM&gt;, for there are other variables, not only those "&lt;SPAN&gt;created by INPUT or assignment statements" that are set to missing at the top of the DATA step implied loop, such as X in:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;call missing (X) ;&lt;/P&gt;
&lt;P&gt;y = sum (X, 0) ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and so on where X is clearly &lt;EM&gt;not&lt;/EM&gt; created by either an INPUT or assignment statement, and yet not retained (in the correct sense).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 15:17:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489638#M127896</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-24T15:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489749#M127952</link>
      <description>&lt;P&gt;For extra confusion consider:&lt;/P&gt;
&lt;PRE&gt;data a2; 
   set a1;
   sumx+x;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, if you RETAIN a variable in a dataset referenced on a SET statement you get the value from the dataset each time the SET statement iterates.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 20:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489749#M127952</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-24T20:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489772#M127956</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh yes, and examples of this kind can be multiplied. At least in V5 manual, the initial explanation of RETAIN is followed by a full page occupied by a table showing which sorts of variables aren't affected by RETAIN and which are and how. In particular, it says that variables brought in by SET, MERGE, and UPDATE aren't affected. Now, they would've added MODIFY but it wasn't part of SAS back then.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 20:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489772#M127956</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-24T20:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489836#M127987</link>
      <description>&lt;P&gt;Note that any variable that&amp;nbsp;exists in a input dataset ( via SET, MERGE ... statement ) is also retained.&amp;nbsp; Whether it is mentioned in a RETAIN statement (or a sum statement) or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is just that when a new observation is read the new value read overwrites the retained value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is why one to many merges work.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Aug 2018 18:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489836#M127987</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-25T18:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Retain statement makes me confusing..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489846#M127991</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the old V5 manual all the RETAIN actions are laid out clearly and their details under different scenarios are spelled out in Table 4.7, page 196. It is just the first-sentence verbiage that is misleading, and it's remained practically unchanged ever since. One may say that it's been firmly retained ;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Aug 2018 21:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-statement-makes-me-confusing/m-p/489846#M127991</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-25T21:16:32Z</dc:date>
    </item>
  </channel>
</rss>

