<?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: what happens when one increment a variable without &amp;quot;=&amp;quot; in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401804#M97509</link>
    <description>&lt;P&gt;+ is the &lt;EM&gt;Sum Operator&lt;/EM&gt;; if you think of it that way, you will remember half of the reason for this at least!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;+ does two things:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* Retain the variable on the left side of the operator&lt;/P&gt;
&lt;P&gt;* Sum the two sides of the operator, just as the SUM function does&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember, the&amp;nbsp;SUM function considers missing values to be zero when added to a nonmissing value.&amp;nbsp; This operator works the same way.&lt;/P&gt;</description>
    <pubDate>Fri, 06 Oct 2017 14:59:18 GMT</pubDate>
    <dc:creator>snoopy369</dc:creator>
    <dc:date>2017-10-06T14:59:18Z</dc:date>
    <item>
      <title>what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401755#M97505</link>
      <description>&lt;P&gt;I just became surprised about how it works when one increment a variable without using "=". The strange thing happens when I have missing values. In that case it seems that it take the last value that was not missing (from a previous observation). And this even happens if I first replace a possible missing value with some other number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I expected that the two&amp;nbsp;dataset&amp;nbsp;"outdata1" and "outdata2" below&amp;nbsp;would be the same, but they&amp;nbsp;became very different. Only&amp;nbsp;difference is that "n=n+1" is replaced by "n+1".&amp;nbsp;I understand what happens but not the logic. It seems not intuitive.&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 abc;
do i=1 to 10;
  text=ifc( 5&amp;lt;=i&amp;lt;=8,'NR','  ');
  output;
end;
keep text ;
run;

data outdata1;
  set abc;
  if text ne 'NR' then n=5;
  if n=. then n=-5;
  n+1;
run;

data outdata2;
  set abc;
  if text ne 'NR' then n=5;
  if n=. then n=-5;
  n=n+1;
run;
proc print data=outdata1 ;
run;
proc print data=outdata2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the two dataset becomes&lt;/P&gt;
&lt;TABLE class="table" rules="all" frame="box" cellspacing="0" cellpadding="5" summary="Procedure Print: Data Set WORK.OUTDATA1"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;text&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;n&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;4&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;5&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;6&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;7&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;8&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;9&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;10&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;
&lt;P&gt;6&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;TABLE class="table" rules="all" frame="box" cellspacing="0" cellpadding="5" summary="Procedure Print: Data Set WORK.OUTDATA2"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Obs&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;text&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;n&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;4&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;5&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;-4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;6&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;-4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;7&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;-4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;8&lt;/TH&gt;
&lt;TD class="l data"&gt;NR&lt;/TD&gt;
&lt;TD class="r data"&gt;-4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;9&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TH class="r rowheader" scope="row"&gt;10&lt;/TH&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 06 Oct 2017 13:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401755#M97505</guid>
      <dc:creator>JacobSimonsen</dc:creator>
      <dc:date>2017-10-06T13:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401769#M97506</link>
      <description>&lt;P&gt;It sounds like the software is working as expected.&amp;nbsp; Here's some of the documentation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000289454.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000289454.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, this statement has triple meaning:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;varname + (some numeric value);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It means:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Retain VARNAME&lt;/LI&gt;
&lt;LI&gt;Give VARNAME an initial value of zero (unless a RETAIN statement overrides that initial value)&lt;/LI&gt;
&lt;LI&gt;Increment VARNAME.&amp;nbsp; However, if the value to the right of the plus sign is missing, ignore it.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;That sounds like the result you are getting, so it's to be expected.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 13:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401769#M97506</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-06T13:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401804#M97509</link>
      <description>&lt;P&gt;+ is the &lt;EM&gt;Sum Operator&lt;/EM&gt;; if you think of it that way, you will remember half of the reason for this at least!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;+ does two things:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* Retain the variable on the left side of the operator&lt;/P&gt;
&lt;P&gt;* Sum the two sides of the operator, just as the SUM function does&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember, the&amp;nbsp;SUM function considers missing values to be zero when added to a nonmissing value.&amp;nbsp; This operator works the same way.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 14:59:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401804#M97509</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2017-10-06T14:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401836#M97513</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;N+1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is an example of a SUM statement. The SUM statement takes the form&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;varname+expression;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM statement will mark the target variable to be retained and will initialize it to 0. When it executes it is as if you used this statement instead&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;varname=sum(varname,expression);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM function and the + operator work differently when one or more of the input is missing. The + operation will result in a missing value if either of the inputs are missing. The SUM() function will ignore missing values.&amp;nbsp; So the result will only be missing if all of the inputs are missing.&lt;/P&gt;
&lt;P&gt;Instead of replacing 'n+1' with 'n=n+1' you could have replaced it with these two statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain n 0;
n=sum(n,1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401836#M97513</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-06T16:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401882#M97526</link>
      <description>&lt;P&gt;Thank you for your answers - I think they are all right.&lt;/P&gt;
&lt;P&gt;But, why is it that when I replace the missing value in "n" with -5, then it instead used the retained value from last observation.&lt;/P&gt;
&lt;P&gt;I see that the sum function and the + operator works different for missing values, but in this example, n is exactly not missing.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 18:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401882#M97526</guid>
      <dc:creator>JacobSimonsen</dc:creator>
      <dc:date>2017-10-06T18:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401885#M97529</link>
      <description>&lt;P&gt;That's because it is initialized to zero, as the first answer noted.&amp;nbsp; So it's never missing unless it's set explicitly to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  put n=;
  if n=. then n=-5;
  n+1;
  put n=;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the log contents.&amp;nbsp; It starts out at *zero*.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 18:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401885#M97529</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2017-10-06T18:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401887#M97530</link>
      <description>&lt;P&gt;Yes&amp;nbsp;- thats the reason. Now I understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 18:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401887#M97530</guid>
      <dc:creator>JacobSimonsen</dc:creator>
      <dc:date>2017-10-06T18:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: what happens when one increment a variable without "="</title>
      <link>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401889#M97532</link>
      <description>&lt;P&gt;If the N is retained and starts with an initial value of 0 then if cannot become missing unless you set it to missing.&amp;nbsp;I do not see anywhere that you are setting N to missing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If N is NOT retained than it will start out as missing on each iteration of the data step.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 18:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/what-happens-when-one-increment-a-variable-without-quot-quot/m-p/401889#M97532</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-06T18:58:07Z</dc:date>
    </item>
  </channel>
</rss>

