<?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 Insert a Row at the end of the data with totals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720665#M223260</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In this example we create a total row and add it to the data set.&lt;/P&gt;
&lt;P&gt;I want to ask some questions please:&lt;/P&gt;
&lt;P&gt;1- Why Retain is not necesary here?&lt;/P&gt;
&lt;P&gt;As I understand Retain&amp;nbsp;&lt;SPAN&gt;telling &lt;/SPAN&gt;&lt;EM&gt;SAS&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;not to reset the variables to missing at the beginning of each iteration of the DATA step.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So why in this case the values are keeping from one row to next row?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2-At the first iteration ct has null value and then&amp;nbsp;&lt;CODE class=" language-sas"&gt;ct+count&amp;nbsp;&amp;nbsp; add&amp;nbsp;null&amp;nbsp;value&amp;nbsp;with&amp;nbsp;23.&lt;/CODE&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;As far as I know when we use "+" to sum null with value then we get null.&lt;/P&gt;
&lt;P&gt;So why in this case null+23 get 23?&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var $ COUNT;
cards;
DATA1 23
DATA2 35
DATA3 27
DATA4 24
DATA5 25
DATA6 22
DATA7 29
;
Run;

data want;
set have end=last;
ct+count;
output;
if last then do;
var='Total';
Count=CT;
output;
end;
drop ct;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 20 Feb 2021 19:42:22 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-02-20T19:42:22Z</dc:date>
    <item>
      <title>Insert a Row at the end of the data with totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720665#M223260</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In this example we create a total row and add it to the data set.&lt;/P&gt;
&lt;P&gt;I want to ask some questions please:&lt;/P&gt;
&lt;P&gt;1- Why Retain is not necesary here?&lt;/P&gt;
&lt;P&gt;As I understand Retain&amp;nbsp;&lt;SPAN&gt;telling &lt;/SPAN&gt;&lt;EM&gt;SAS&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;not to reset the variables to missing at the beginning of each iteration of the DATA step.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So why in this case the values are keeping from one row to next row?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2-At the first iteration ct has null value and then&amp;nbsp;&lt;CODE class=" language-sas"&gt;ct+count&amp;nbsp;&amp;nbsp; add&amp;nbsp;null&amp;nbsp;value&amp;nbsp;with&amp;nbsp;23.&lt;/CODE&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;As far as I know when we use "+" to sum null with value then we get null.&lt;/P&gt;
&lt;P&gt;So why in this case null+23 get 23?&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var $ COUNT;
cards;
DATA1 23
DATA2 35
DATA3 27
DATA4 24
DATA5 25
DATA6 22
DATA7 29
;
Run;

data want;
set have end=last;
ct+count;
output;
if last then do;
var='Total';
Count=CT;
output;
end;
drop ct;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Feb 2021 19:42:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720665#M223260</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-02-20T19:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Row at the end of the data with totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720668#M223263</link>
      <description>&lt;P&gt;1) AS CT is not an input variable and the accumulation is done by&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;ct+count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;then CT is implicitly retained. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case you accumulate by ct=ct+coutnt or by ct=sum(ct, xount) - you'll need add a retain statement.&lt;/P&gt;
&lt;P&gt;In all cases you add the retain to be on the safe side.&lt;/P&gt;
&lt;P&gt;2) The usage of&amp;nbsp;&lt;STRONG&gt;ct+count&lt;/STRONG&gt; neglegts or skups null values.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Feb 2021 20:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720668#M223263</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-20T20:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Row at the end of the data with totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720705#M223273</link>
      <description>&lt;P&gt;Please read about the SUM statement you are using.&amp;nbsp; The documentation will answer both questions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1dfiqj146yi2cn1maeju9wo7ijs.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1dfiqj146yi2cn1maeju9wo7ijs.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2021 03:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720705#M223273</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-21T03:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a Row at the end of the data with totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720706#M223274</link>
      <description>&lt;P&gt;The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ct+count:&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is what sas call a&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lestmtsref&amp;amp;docsetTarget=n1dfiqj146yi2cn1maeju9wo7ijs.htm&amp;amp;locale=en" target="_self"&gt;Sum Statement&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Among other points it says:&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;So take a look at the "Before" and "After" values of totage below, especially the &lt;EM&gt;&lt;STRONG&gt;before&lt;/STRONG&gt;&lt;/EM&gt; value for observation 1:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  put _n_=  'Before: ' totage= z2. @;
  set sashelp.class (obs=3);
  totage+age;
  put 'After: ' totage=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;The other pertinent point, as you have discovered is:&lt;/SPAN&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The variable's value is retained from one iteration to the next, as if it had appeared in a RETAIN statement.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;And finally, it functions like the SUM function, which will treat missing values like zeroes.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Feb 2021 03:42:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-Row-at-the-end-of-the-data-with-totals/m-p/720706#M223274</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-21T03:42:20Z</dc:date>
    </item>
  </channel>
</rss>

