<?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 for Each BY in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555294#M154531</link>
    <description>&lt;P&gt;Something I fancied to get your results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
set a;
by a;
if first.a then grp=1;
else grp+1;
c=b**grp;
put c;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 01 May 2019 07:43:05 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-05-01T07:43:05Z</dc:date>
    <item>
      <title>RETAIN for Each BY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555282#M154522</link>
      <description>&lt;P&gt;Now I am multiplying the following numbers recursively.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input a b;
cards;
1 1.1
1 1.1
1 1.1
1 1.1
1 1.1
2 1.2
2 1.2
2 1.2
2 1.2
2 1.2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I can use RETAIN to do this.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
set a;
by a;
retain c 1;
c=c*b;&lt;BR /&gt;put c;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem is, SAS multiplies all the numbers, while I want to apply RETAIN for each BY—restarting at the sixth here.&lt;/P&gt;&lt;PRE&gt;16   data a;
17   set a;
18   by a;
19   retain c;
20   if first.a then c=1;
21   c=c*b;
22   put c;
23   run;

1.1
1.21
1.331
1.4641
1.61051
1.2
1.44
1.728
2.0736
2.48832
NOTE: There were 10 observations read from the data set WORK.A.
NOTE: The data set WORK.A has 10 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds&lt;/PRE&gt;&lt;P&gt;Is there any way to do this? By the way, I know that&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.a then c=1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;works in this case, but I wonder whether RETAIN is directly applicable for each BY more generally—I tried IF FIRST.A THEN RETAIN C 1; here but did not work. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 05:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555282#M154522</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-05-01T05:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN for Each BY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555288#M154527</link>
      <description>&lt;P&gt;Is there any way to do this? By the way, I know that&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;a &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; c&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;works in this case, but I wonder whether RETAIN is directly applicable for each BY more generally—I tried IF FIRST.A THEN RETAIN C 1; here but did not work. Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course won't work because RETAIN&amp;nbsp; is a compile time statement whereas IF THEN is an execution time statement. Are you looking for something fancy?&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 06:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555288#M154527</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-01T06:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN for Each BY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555289#M154528</link>
      <description>&lt;P&gt;Or you could mimic a retain to make it seem like as though you declared only once with a DOW loop&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data a;
c=1;
do until(last.a);
set a;
by a;
c=c*b;
put c;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Nevertheless, I see no value regardless&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 07:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555289#M154528</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-01T07:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN for Each BY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555293#M154530</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/173881"&gt;@Junyong&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain is a compile-time statement, and it just tells SAS not to reset the value to missing before each input record is read. It is not necessary to supply a value to supply a value in the retain statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So everything works if you assign the value of b to c in the first observation in each by group:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data b;
	set a;
	by a;
	retain c;
	if first.a then c = b;
	else c = c * b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 May 2019 07:34:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555293#M154530</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-05-01T07:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN for Each BY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555294#M154531</link>
      <description>&lt;P&gt;Something I fancied to get your results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
set a;
by a;
if first.a then grp=1;
else grp+1;
c=b**grp;
put c;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 May 2019 07:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-for-Each-BY/m-p/555294#M154531</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-01T07:43:05Z</dc:date>
    </item>
  </channel>
</rss>

