<?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: Why SUM statement gives answer but Retain Statement gives missing values? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895883#M39836</link>
    <description>&lt;P&gt;Look at your log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are RETAINing a variable (total) initialized to missing.&amp;nbsp; Therefore the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total=total+salary;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will generate an error message, because arithmetic operators will return missing from missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could try intializing total to zero, which would do what you expect as long as SALARY is not missing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain TOTAL 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The summing statement implies retain and also operates almost like the SUM function.&amp;nbsp; &amp;nbsp;Which is to say that missing values are ignored.&amp;nbsp; This function (like MEAN, STD, and all other stat functions) will skip missing values in the argument list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I say the statement is "almost" like the SUM function, because the SUM of two missings is missing, but the sum statement (see observation 1 below) generates a zero.&amp;nbsp; I haven't looked for it , but I think it is documented that the SUM statement acts as if the new (retained) variable is initialized as 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do i=.,1,2; output; end;
run;
data _null_;
  set have;
  retain sumfunc ;
  sumfunc=sum(sumfunc,i);
  sumstatement+i;
  put (_all_) (=);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which generates the log&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;62   data _null_;
63     set have;
64     retain sumfunc ;
65     sumfunc=sum(sumfunc,i);
66     sumstatement+i;
67     put (_all_) (=);
68   run;

i=. sumfunc=. sumstatement=0
i=1 sumfunc=1 sumstatement=1
i=2 sumfunc=3 sumstatement=3
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 65:11
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 26 Sep 2023 12:25:53 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-09-26T12:25:53Z</dc:date>
    <item>
      <title>Why SUM statement gives answer but Retain Statement gives missing values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895877#M39834</link>
      <description>&lt;P&gt;For practice I was using SUM statement and tried to check if I get same answer using RETAIN statement. To my surprise I get missing values using the RETAIN statement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Till now whatever I learned, I assumed either can be used.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I'm confused in what situation should I use either of them?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="POOJA_J_0-1695729583607.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88348iD9798FDFE3991866/image-size/medium?v=v2&amp;amp;px=400" role="button" title="POOJA_J_0-1695729583607.png" alt="POOJA_J_0-1695729583607.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="POOJA_J_1-1695729603009.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88349i383C01240A25B0BB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="POOJA_J_1-1695729603009.png" alt="POOJA_J_1-1695729603009.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="POOJA_J_2-1695729622803.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88350iED2C493D9D7E0B13/image-size/medium?v=v2&amp;amp;px=400" role="button" title="POOJA_J_2-1695729622803.png" alt="POOJA_J_2-1695729622803.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="POOJA_J_3-1695729646256.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88351iA7E1EA677F1DC4E0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="POOJA_J_3-1695729646256.png" alt="POOJA_J_3-1695729646256.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2023 12:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895877#M39834</guid>
      <dc:creator>POOJA_J</dc:creator>
      <dc:date>2023-09-26T12:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Why SUM statement gives answer but Retain Statement gives missing values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895881#M39835</link>
      <description>&lt;P&gt;The SUM STATEMENT does two things and RETAIN is just one of them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to use the SUM() function to handle any missing values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total=sum(total,salary);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also you need to initialize total to zero to mimic what the SUM statement will do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain total 0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Sep 2023 12:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895881#M39835</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-26T12:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: Why SUM statement gives answer but Retain Statement gives missing values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895883#M39836</link>
      <description>&lt;P&gt;Look at your log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are RETAINing a variable (total) initialized to missing.&amp;nbsp; Therefore the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;total=total+salary;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will generate an error message, because arithmetic operators will return missing from missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could try intializing total to zero, which would do what you expect as long as SALARY is not missing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain TOTAL 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The summing statement implies retain and also operates almost like the SUM function.&amp;nbsp; &amp;nbsp;Which is to say that missing values are ignored.&amp;nbsp; This function (like MEAN, STD, and all other stat functions) will skip missing values in the argument list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I say the statement is "almost" like the SUM function, because the SUM of two missings is missing, but the sum statement (see observation 1 below) generates a zero.&amp;nbsp; I haven't looked for it , but I think it is documented that the SUM statement acts as if the new (retained) variable is initialized as 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do i=.,1,2; output; end;
run;
data _null_;
  set have;
  retain sumfunc ;
  sumfunc=sum(sumfunc,i);
  sumstatement+i;
  put (_all_) (=);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which generates the log&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;62   data _null_;
63     set have;
64     retain sumfunc ;
65     sumfunc=sum(sumfunc,i);
66     sumstatement+i;
67     put (_all_) (=);
68   run;

i=. sumfunc=. sumstatement=0
i=1 sumfunc=1 sumstatement=1
i=2 sumfunc=3 sumstatement=3
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 65:11
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Sep 2023 12:25:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895883#M39836</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-26T12:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Why SUM statement gives answer but Retain Statement gives missing values?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895884#M39837</link>
      <description>&lt;P&gt;Maxim 1: Read the Documentation.&lt;/P&gt;
&lt;P&gt;From&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;SUM Statement&lt;/A&gt;&amp;nbsp;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The variable is automatically set to 0 before SAS reads the first observation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;and&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;SAS treats an expression that produces a missing value as zero.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Both of these are not done if you use RETAIN without initialization, and a simple calculation with + in the assignment.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2023 12:26:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Why-SUM-statement-gives-answer-but-Retain-Statement-gives/m-p/895884#M39837</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-09-26T12:26:21Z</dc:date>
    </item>
  </channel>
</rss>

