<?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: sequence number by group-Why no need retain? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924798#M364007</link>
    <description>&lt;P&gt;You &lt;EM&gt;did&lt;/EM&gt; use the sum statement. Compare your statement&lt;/P&gt;
&lt;PRE&gt;seq + 1;&lt;/PRE&gt;
&lt;P&gt;to the syntax or the examples in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;H2 id="n1f47bvrxfgfe4n1dfuhtfn0mm4j" class="xisDoc-title"&gt;Syntax&lt;/H2&gt;
&lt;DIV id="n1re5e43ge6az7n1c1opa3pqdys4" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm#p0t5pd0rd5za0bn1mo8anf6mty70" target="_blank"&gt;variable&lt;/A&gt;&lt;/EM&gt;+&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm#p0q3kltjvam38pn1ip6tltc7qgr0" target="_blank"&gt;expression&lt;/A&gt;&lt;/EM&gt;;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;total + x;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The sum statement is one of the rare exceptions where the name of the statement does not occur in the code. Note the difference to other uses of the plus sign in &lt;EM&gt;expressions&lt;/EM&gt; as part of other statements, e.g., an &lt;EM&gt;assignment statement&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;seq =&lt;/STRONG&gt; &lt;/FONT&gt;seq + 1;&lt;/PRE&gt;
&lt;P&gt;a &lt;EM&gt;DO statement&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;do&lt;/STRONG&gt;&lt;/FONT&gt; i=1 &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;to&lt;/STRONG&gt;&lt;/FONT&gt; seq + 1;&lt;/PRE&gt;
&lt;P&gt;a &lt;EM&gt;WHERE statement&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;where&lt;/STRONG&gt;&lt;/FONT&gt; x &amp;lt;= seq + 1;&lt;/PRE&gt;
&lt;P&gt;etc.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Apr 2024 10:27:35 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-04-18T10:27:35Z</dc:date>
    <item>
      <title>sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924677#M363962</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to generate seq number by group.&lt;/P&gt;
&lt;P&gt;The group is defined by 4 variables.&lt;/P&gt;
&lt;P&gt;This code is working 100%.&lt;/P&gt;
&lt;P&gt;My question- What is the reason that no need to use retain and SAS knows to keep the value from one observation to the next observation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by z r v q;
if first.q
then seq = 1;
else seq + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 11:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924677#M363962</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-17T11:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924678#M363963</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a side effect of the sum statement. In the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt; see subsection "variable" and section "Comparisons".&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2024 11:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924678#M363963</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-04-17T11:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924796#M364005</link>
      <description>&lt;P&gt;But in my code I didnt use SUM (I used plus)&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;seq + 1&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Apr 2024 10:04:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924796#M364005</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-18T10:04:51Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924797#M364006</link>
      <description>&lt;P&gt;If you had clicked on the link provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; you would see that the Sum statement is exactly what you are talking about.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 10:23:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924797#M364006</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-18T10:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924798#M364007</link>
      <description>&lt;P&gt;You &lt;EM&gt;did&lt;/EM&gt; use the sum statement. Compare your statement&lt;/P&gt;
&lt;PRE&gt;seq + 1;&lt;/PRE&gt;
&lt;P&gt;to the syntax or the examples in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;H2 id="n1f47bvrxfgfe4n1dfuhtfn0mm4j" class="xisDoc-title"&gt;Syntax&lt;/H2&gt;
&lt;DIV id="n1re5e43ge6az7n1c1opa3pqdys4" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm#p0t5pd0rd5za0bn1mo8anf6mty70" target="_blank"&gt;variable&lt;/A&gt;&lt;/EM&gt;+&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm#p0q3kltjvam38pn1ip6tltc7qgr0" target="_blank"&gt;expression&lt;/A&gt;&lt;/EM&gt;;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;total + x;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The sum statement is one of the rare exceptions where the name of the statement does not occur in the code. Note the difference to other uses of the plus sign in &lt;EM&gt;expressions&lt;/EM&gt; as part of other statements, e.g., an &lt;EM&gt;assignment statement&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;seq =&lt;/STRONG&gt; &lt;/FONT&gt;seq + 1;&lt;/PRE&gt;
&lt;P&gt;a &lt;EM&gt;DO statement&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;do&lt;/STRONG&gt;&lt;/FONT&gt; i=1 &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;to&lt;/STRONG&gt;&lt;/FONT&gt; seq + 1;&lt;/PRE&gt;
&lt;P&gt;a &lt;EM&gt;WHERE statement&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;where&lt;/STRONG&gt;&lt;/FONT&gt; x &amp;lt;= seq + 1;&lt;/PRE&gt;
&lt;P&gt;etc.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 10:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924798#M364007</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-04-18T10:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924799#M364008</link>
      <description>&lt;P&gt;&amp;nbsp;so the rule is that SUM statement doesn't have built in Retain whereas plus have built in retain?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 10:32:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924799#M364008</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-18T10:32:36Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924800#M364009</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;so the rule is that SUM statement doesn't have built in Retain whereas plus have built in retain?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You are making stuff up. No one said that. There is no "plus" statement, it is the SUM statement, you are using the SUM statement. Please click on the link from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; and see for yourself.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 10:40:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924800#M364009</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-18T10:40:52Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924801#M364010</link>
      <description>&lt;P&gt;Heecdd&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please look on the difference between these 2 codes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID X Y Z;
cards;
1 10 20 30
1 11 12 13
1 14 15 18
2 20 20 20 
2 20 20 40
;
Run;

data want;
set have;
by ID;
Seq+1; /****No need retain***/
if first.ID then Seq=1;
run;

data want;
set have;
retain seq;
by ID;
seq=sum(Seq,1); /****Must have retain***/
if first.ID then Seq=1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here are the ways I found to calculate seq by group&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID X Y Z;
cards;
1 10 20 30
1 11 12 13
1 14 15 18
2 20 20 20 
2 20 20 40
;
Run;

data want;
set have;
by ID;
Seq+1; /****No need retain***/
if first.ID then Seq=1;
run;

data want;
set have;
retain seq;
by ID;
seq=sum(Seq,1); /****Must have retain***/
if first.ID then Seq=1;
run;


data want;
set have;
by ID;
if first.ID then seq = 1;
else seq + 1;
run;
/****No need retain***/


data want;
set have;
by ID;
retain seq;
if first.ID then seq=0;
seq=sum(seq,1); 
run;
/****Must have retain***/


data want;
do seq=1 by 1 until last.ID;
set have;
by ID;
output;
end;
run;
/****No need retain***/


  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Apr 2024 11:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924801#M364010</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-04-18T11:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924802#M364011</link>
      <description>&lt;P&gt;You are now confusing the SUM function with the SUM statement. These are not the same. The SUM function does require a RETAIN, again as clearly stated at the link from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; which you should really look at. The SUM statement does not require a RETAIN, as clearly stated at the link.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 11:37:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924802#M364011</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-18T11:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: sequence number by group-Why no need retain?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924807#M364014</link>
      <description>&lt;P&gt;Once the sum statement is used for a (numeric) variable anywhere in a DATA step, that variable is automatically retained and initialized to zero (unless other statements override this, e.g., a RETAIN statement specifying a different initial value). These changes to the default behavior (i.e., no retaining, initialization with a missing value) occur at &lt;EM&gt;compile&lt;/EM&gt; time. They are effective even if the sum statement is never &lt;EM&gt;executed&lt;/EM&gt;, as in the artificial example below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set sashelp.class;
seq=seq+100;
if 0 then seq+42;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result (abbreviated PROC PRINT output for dataset TEST)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;Obs    Name       Sex    Age    Height    Weight     seq

  1    Alfred      M      14     69.0      112.5     100
  2    Alice       F      13     56.5       84.0     200
  3    Barbara     F      13     65.3       98.0     300
  .
  .  
  . 
 18    Thomas      M      11     57.5       85.0    1800
 19    William     M      15     66.5      112.0    1900&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without the sum statement &lt;FONT face="courier new,courier"&gt;seq+42;&lt;/FONT&gt; -- although never executed due to the IF condition -- variable &lt;FONT face="courier new,courier"&gt;seq&lt;/FONT&gt; would be missing in all observations because adding 100 to the initial missing value would produce another missing value again and again (and a note about this in the log).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your last example (where the parentheses around the UNTIL condition are missing)&lt;/P&gt;
&lt;PRE&gt;data want;
do seq=1 by 1 until&lt;STRONG&gt;(&lt;/STRONG&gt;last.ID&lt;STRONG&gt;)&lt;/STRONG&gt;;
  set have;
  by ID;
  output;
end;
run;&lt;/PRE&gt;
&lt;P&gt;a RETAIN statement is not needed because each ID BY-group is processed within a single iteration of the DATA step. Variable &lt;FONT face="courier new,courier"&gt;seq&lt;/FONT&gt; is &lt;EM&gt;not&lt;/EM&gt; retained, but set to 1 (and later incremented if a BY group has more than one observation) by the iterative DO statement in each DATA step iteration.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2024 12:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sequence-number-by-group-Why-no-need-retain/m-p/924807#M364014</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-04-18T12:02:10Z</dc:date>
    </item>
  </channel>
</rss>

