<?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: Does the order matter while resetting values in RETAIN statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695930#M212458</link>
    <description>Thanks Richard for the detail explanation. I just constructed the example dataset and choose random names for the variables. My idea was to get the theoretical basis for RETAIN statement.&lt;BR /&gt;In my case, I was getting 1 more count as the previous subjid has variable1='NOT DONE' as the last record.</description>
    <pubDate>Mon, 02 Nov 2020 13:26:41 GMT</pubDate>
    <dc:creator>kingCobra</dc:creator>
    <dc:date>2020-11-02T13:26:41Z</dc:date>
    <item>
      <title>Does the order matter while resetting values in RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695922#M212451</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have had a theoretical question in mind for long time.&lt;/P&gt;
&lt;P&gt;Does the order matter while resetting values in RETAIN statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ex -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by&amp;nbsp;subjid parameter visit;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;retain total 0;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by subjid parameter visit;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;if first.parameter then total=0;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;if variable1='NOT DONE' then total=total+1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does it produce the same result if I change the order of the statement marked in red?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;&lt;FONT color="#FF0000"&gt;if variable1='NOT DONE' then total=total+1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&lt;FONT color="#FF0000"&gt;if first.parameter then total=0;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In some complex cases I think I have seen the resetting statement (&lt;FONT color="#000000"&gt;if first.parameter then total=0;) at #1 does not actually reset the value of total. But I could not found any theoretical&amp;nbsp;basis for the ordering of the statements.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Any help will be appreciated.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 12:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695922#M212451</guid>
      <dc:creator>kingCobra</dc:creator>
      <dc:date>2020-11-02T12:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: Does the order matter while resetting values in RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695925#M212454</link>
      <description>&lt;P&gt;You could try it and find out (and let us know what happens).&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 12:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695925#M212454</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-02T12:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: Does the order matter while resetting values in RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695928#M212456</link>
      <description>&lt;P&gt;The RETAIN statement is not executable, it is a compile time statement that sets the initial value of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;In the sample the extra emphasis on compile time behavior is shown by placing the RETAIN statement at the bottom of the step.&amp;nbsp; The first PUT shows the initialization value.&lt;/P&gt;
&lt;PRE&gt;data _null_;
  put x=;
  set sashelp.class;
  retain x 123;
run;&lt;/PRE&gt;
&lt;P&gt;For your case of REATAINing a total value that incrementally counts the number of NOT DONEs over a &lt;STRONG&gt;parameter&lt;/STRONG&gt; within a &lt;STRONG&gt;subjid&lt;/STRONG&gt;,&amp;nbsp;the order of statements absolutely matters.&amp;nbsp; The "total" might be better named or labeled "how_many_not_dones_so_far"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first. conditional must occur first&lt;/P&gt;
&lt;PRE&gt;if first.parameter then total=0;
if variable1='NOT DONE' then total=total+1;&lt;/PRE&gt;
&lt;P&gt;Consider what would happen if the statements were reversed, AND, (edge case) the data had NOT DONE in the first row of the group?&lt;/P&gt;
&lt;PRE&gt;if variable1='NOT DONE' then total=total+1;
if first.parameter then total=0;&lt;/PRE&gt;
&lt;P&gt;In the (edge case) your total would be one less that what it should be because the total starting at first.parameter state should be 1, not 0.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Nov 2020 13:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695928#M212456</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-11-02T13:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Does the order matter while resetting values in RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695930#M212458</link>
      <description>Thanks Richard for the detail explanation. I just constructed the example dataset and choose random names for the variables. My idea was to get the theoretical basis for RETAIN statement.&lt;BR /&gt;In my case, I was getting 1 more count as the previous subjid has variable1='NOT DONE' as the last record.</description>
      <pubDate>Mon, 02 Nov 2020 13:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Does-the-order-matter-while-resetting-values-in-RETAIN-statement/m-p/695930#M212458</guid>
      <dc:creator>kingCobra</dc:creator>
      <dc:date>2020-11-02T13:26:41Z</dc:date>
    </item>
  </channel>
</rss>

