<?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: use of plus(+) after a variable name? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481335#M124480</link>
    <description>&lt;P&gt;This construct is called "the SUM statement". It's equivalent to 2 statements:&lt;/P&gt;&lt;PRE&gt;  retain totsal 0 ;&amp;nbsp;
  totsal = totsal + salary ;&lt;/PRE&gt;&lt;P&gt;Thus, totsal is initialized to 0 at compile time, and the value of totsal is not set to a missing value every time the new iteration of the DATA step intrinsic loop begins (i.e. at the top of the step). This way, totsal is incremented by salary in each iteration. Without the RETAIN statement (or its implied equivalent in the SUM statement), totsal would be initially missing, and the + operator would try adding a non-missing value of salary to a missing value, which would result in a missing value, plus you'd get the "missing values were generated" message in the log. If, instead of the RETAIN above, you simply initialize total to 0, i.e. code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;  totsal = 0 ;
  totsal = totsal + salary ;&lt;/PRE&gt;&lt;P&gt;that would not work as you want, either. This is because in the first iteration of the implied loop, the summation would result in 0+salary=salary. However, in the very next iteration, totsal will be set to a missing value at the top of the step, then assigned 0, and as a result, you'd get totsal = salary for each output record. This behavior can be altered only if you take explicit control of the file-reading loop and thus prevent program control from ever reaching the top of the step:&lt;/P&gt;&lt;PRE&gt;data empdata ;
  totsal = 0 ;
  do until (eof) ;
    merge employee salary(rename=(name=fname)) end=eof ;
    by fname ;
    totsal  = totsal + salary ;
    output ;
  end ;
  stop ;
run;&lt;/PRE&gt;&lt;P&gt;The reason it works is that the initialization is now located outside the file-reading loop, and all processing is finished before program control can reach the top of the step again. This construct is a specific form of what is known as the DoW-loop. It has a number of advantages compared to doing everything inside the implied loop. Though in this case it offers none, thinking of how it works is useful to get a better idea of how the DATA step operates in general.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Paul D.&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jul 2018 03:22:09 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2018-07-26T03:22:09Z</dc:date>
    <item>
      <title>use of plus(+) after a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481302#M124471</link>
      <description>&lt;P&gt;Hi, here's my code:&lt;/P&gt;&lt;P&gt;I can understand them all but the line with totsal+ salary;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where did that totsal+ come from? and what does it do?&lt;/P&gt;&lt;P&gt;even this was not declared, the program works fine.&lt;/P&gt;&lt;P&gt;Is it something like: totsal = totsal + salary?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA employee;
	INPUT fname $ age;
	datalines;
	Bruce 30
	Dan 40
	;
RUN;

DATA salary;
	INPUT name $ salary;
	datalines;
	Bruce 25000
	Bruce 35000
	Dan 25000
	;
RUN;

data empdata;
	merge employee salary(rename=name=fname);
	by fname;
	totsal+ salary;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Jul 2018 23:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481302#M124471</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2018-07-25T23:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: use of plus(+) after a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481303#M124472</link>
      <description>&lt;P&gt;This is a summation command. For each record in the data set, salary is added to the previous value of totsal (and totsal is zero at the start of the data set).&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Salary&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Totsal&lt;/P&gt;
&lt;P&gt;100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 100&lt;/P&gt;
&lt;P&gt;125&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 225&lt;/P&gt;
&lt;P&gt;70&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 295&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Is it something like: totsal = totsal + salary?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No, that's different. That doesn't work unless somehow totsal has been assigned a value earlier in the data set for each record.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jul 2018 23:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481303#M124472</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-25T23:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: use of plus(+) after a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481335#M124480</link>
      <description>&lt;P&gt;This construct is called "the SUM statement". It's equivalent to 2 statements:&lt;/P&gt;&lt;PRE&gt;  retain totsal 0 ;&amp;nbsp;
  totsal = totsal + salary ;&lt;/PRE&gt;&lt;P&gt;Thus, totsal is initialized to 0 at compile time, and the value of totsal is not set to a missing value every time the new iteration of the DATA step intrinsic loop begins (i.e. at the top of the step). This way, totsal is incremented by salary in each iteration. Without the RETAIN statement (or its implied equivalent in the SUM statement), totsal would be initially missing, and the + operator would try adding a non-missing value of salary to a missing value, which would result in a missing value, plus you'd get the "missing values were generated" message in the log. If, instead of the RETAIN above, you simply initialize total to 0, i.e. code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;  totsal = 0 ;
  totsal = totsal + salary ;&lt;/PRE&gt;&lt;P&gt;that would not work as you want, either. This is because in the first iteration of the implied loop, the summation would result in 0+salary=salary. However, in the very next iteration, totsal will be set to a missing value at the top of the step, then assigned 0, and as a result, you'd get totsal = salary for each output record. This behavior can be altered only if you take explicit control of the file-reading loop and thus prevent program control from ever reaching the top of the step:&lt;/P&gt;&lt;PRE&gt;data empdata ;
  totsal = 0 ;
  do until (eof) ;
    merge employee salary(rename=(name=fname)) end=eof ;
    by fname ;
    totsal  = totsal + salary ;
    output ;
  end ;
  stop ;
run;&lt;/PRE&gt;&lt;P&gt;The reason it works is that the initialization is now located outside the file-reading loop, and all processing is finished before program control can reach the top of the step again. This construct is a specific form of what is known as the DoW-loop. It has a number of advantages compared to doing everything inside the implied loop. Though in this case it offers none, thinking of how it works is useful to get a better idea of how the DATA step operates in general.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Paul D.&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 03:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481335#M124480</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-07-26T03:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: use of plus(+) after a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481523#M124571</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This construct is called "the SUM statement". It's equivalent to 2 statements:&lt;/P&gt;
&lt;PRE&gt;  retain totsal 0 ;&amp;nbsp;
  totsal = totsal + salary ;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;What about missing?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;40   data;
41      input salary;
42      retain totsal totsal2 0 ;
43      totsal = totsal + salary;
44      totsal2 = sum(totsal2,salary);
45      totsal3 + salary;
46      cards;

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).
      3 at 43:20
NOTE: The data set WORK.DATA1 has 4 observations and 4 variables.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 249px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22047i9C5F917F2ED4A13E/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:26:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481523#M124571</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2018-07-26T14:26:23Z</dc:date>
    </item>
    <item>
      <title>Re: use of plus(+) after a variable name?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481538#M124580</link>
      <description>&lt;P&gt;John,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're right about that. Assuming that salary is not missing is making assumptions about data.&lt;/P&gt;&lt;P&gt;I stand corrected. The definition of the SUM statement should be reformulated as that it is equivalent to the statements:&lt;/P&gt;&lt;PRE&gt;  retain &lt;EM&gt;var&lt;/EM&gt; 0 ;
  &lt;EM&gt;var&lt;/EM&gt; = sum (var, &lt;EM&gt;expression&lt;/EM&gt;) ;&lt;/PRE&gt;&lt;P&gt;Thanks for pointing that out. Nicely dovetails into the point of the benefit of being contradicted I made one post eariler.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 14:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/use-of-plus-after-a-variable-name/m-p/481538#M124580</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-07-26T14:59:06Z</dc:date>
    </item>
  </channel>
</rss>

