<?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 Why is x=x+y not working but x+y works? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847605#M335105</link>
    <description>&lt;P&gt;Coming from python, I am trying to understand by x=x+y to reassign a variable does not work in SAS but x+y works.&lt;/P&gt;
&lt;P&gt;I have data set spg.usa as shown below.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_0-1670104006557.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77981iE2A07C8D537B5C91/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nietzsche_0-1670104006557.png" alt="Nietzsche_0-1670104006557.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I simply want to create a new variable &lt;STRONG&gt;Accum&lt;/STRONG&gt; in the data that adds up the &lt;STRONG&gt;WageRate&lt;/STRONG&gt; variable.&lt;/P&gt;
&lt;P&gt;Why code this code work&lt;/P&gt;
&lt;PRE&gt;data temp;
	set spg.usa;
	&lt;STRONG&gt;Accum+wagerate;&lt;/STRONG&gt;
run;
proc print data=temp;run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_1-1670104189570.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77982i15AB81243C98197E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nietzsche_1-1670104189570.png" alt="Nietzsche_1-1670104189570.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;but this code with x=x+y structure does not work&lt;/P&gt;
&lt;PRE&gt;data temp;
	set spg.usa;
	&lt;STRONG&gt;Accum=Accum+wagerate;&lt;/STRONG&gt;
run;
proc print data=temp;run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_2-1670104408414.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77983i66B9E35FA3D931EE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nietzsche_2-1670104408414.png" alt="Nietzsche_2-1670104408414.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but on page 145 of the prep guide,&amp;nbsp; it works when assign the variable with a new value based on the original value (both sides of the equation).&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_0-1670104717354.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77984i75C0F26D9212ADD7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Nietzsche_0-1670104717354.png" alt="Nietzsche_0-1670104717354.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 03 Dec 2022 22:00:41 GMT</pubDate>
    <dc:creator>Nietzsche</dc:creator>
    <dc:date>2022-12-03T22:00:41Z</dc:date>
    <item>
      <title>Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847605#M335105</link>
      <description>&lt;P&gt;Coming from python, I am trying to understand by x=x+y to reassign a variable does not work in SAS but x+y works.&lt;/P&gt;
&lt;P&gt;I have data set spg.usa as shown below.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_0-1670104006557.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77981iE2A07C8D537B5C91/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nietzsche_0-1670104006557.png" alt="Nietzsche_0-1670104006557.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I simply want to create a new variable &lt;STRONG&gt;Accum&lt;/STRONG&gt; in the data that adds up the &lt;STRONG&gt;WageRate&lt;/STRONG&gt; variable.&lt;/P&gt;
&lt;P&gt;Why code this code work&lt;/P&gt;
&lt;PRE&gt;data temp;
	set spg.usa;
	&lt;STRONG&gt;Accum+wagerate;&lt;/STRONG&gt;
run;
proc print data=temp;run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_1-1670104189570.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77982i15AB81243C98197E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nietzsche_1-1670104189570.png" alt="Nietzsche_1-1670104189570.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;but this code with x=x+y structure does not work&lt;/P&gt;
&lt;PRE&gt;data temp;
	set spg.usa;
	&lt;STRONG&gt;Accum=Accum+wagerate;&lt;/STRONG&gt;
run;
proc print data=temp;run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_2-1670104408414.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77983i66B9E35FA3D931EE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nietzsche_2-1670104408414.png" alt="Nietzsche_2-1670104408414.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but on page 145 of the prep guide,&amp;nbsp; it works when assign the variable with a new value based on the original value (both sides of the equation).&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_0-1670104717354.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77984i75C0F26D9212ADD7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Nietzsche_0-1670104717354.png" alt="Nietzsche_0-1670104717354.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Dec 2022 22:00:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847605#M335105</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-12-03T22:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847607#M335106</link>
      <description>&lt;P&gt;If you create a variable in a data step, like your ACCUM variable, the default behavior is to set it to missing at the start of each iteration of the data step. (In a simple data step like your examples you can think of one iteration being one observation.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So when you use a normal assignment statement like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Accum=Accum+wagerate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are adding the missing value in ACCUM to the value in WAGERATE.&amp;nbsp; Since normal addition with missing values results in a missing result you are setting ACCUM to a missing value on every observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But when you use a SUM statement like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Accum+wagerate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are saying that you want&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The values of ACCUM to be RETAINed (that is not reset to missing at the start of every iteration)&lt;/LI&gt;
&lt;LI&gt;That WAGERATE is added to ACCUM using the SUM() function instead of normal addition so that missing values are ignored.&lt;/LI&gt;
&lt;LI&gt;That ACCUM should be initialized to zero (unless you have an explicit RETAIN statement that initializes it to something else.)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;That is what the SUM statement was designed for, to generate this type of cumulative sum.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Dec 2022 22:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847607#M335106</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-03T22:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847610#M335108</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nietzsche_0-1670105448144.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77985i376AB0F4CC760615/image-size/large?v=v2&amp;amp;px=999" role="button" title="Nietzsche_0-1670105448144.png" alt="Nietzsche_0-1670105448144.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you clarity point 3?&lt;/P&gt;
&lt;P&gt;Aren't point 1 and 3 contradictory to each other? How can Accum be RETAINed and initialised to zero?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyways based on your reply&lt;/P&gt;
&lt;P&gt;I use SUM function and RETAIN statement to get it to work.&lt;/P&gt;
&lt;PRE&gt;data temp;
	set spg.usa;
	retain Accum;
	Accum=sum(Accum,wagerate);
run;
proc print data=temp;run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 03 Dec 2022 22:13:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847610#M335108</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-12-03T22:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847611#M335109</link>
      <description>&lt;P&gt;Point 1 means that at the start of the operation of your DATA step, the variable is assigned a value of zero. Point 3 means that if you want it to have a value of 5.37 at the start of the data set, then you have to initialize it via a RETAIN statement.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Dec 2022 22:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847611#M335109</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-12-03T22:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847617#M335114</link>
      <description>&lt;P&gt;You data step with the RETAIN statement and regular assignment statement does not assign any initial value to ACCUM. So it start with a missing value instead of starting with zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works the same as the SUM statement for your data because WAGERATE did not have a missing value for the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input x @@;
cards;
. 1 2 3 4 
;

data want;
  set have;
  sum1+x;
  retain sum2;
  sum2=sum(sum2,x);
  retain sum3 10 ;
  sum3+x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;OBS    x    sum1    sum2    sum3

 1     .      0       .      10
 2     1      1       1      11
 3     2      3       3      13
 4     3      6       6      16
 5     4     10      10      20
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 00:53:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/847617#M335114</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-04T00:53:45Z</dc:date>
    </item>
    <item>
      <title>Re: Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/848426#M335436</link>
      <description>&lt;PRE&gt;data temp;
	set spg.usa;
	retain Accum 0;
	Accum=sum(Accum,wagerate);
run;
proc print data=temp;run;&lt;/PRE&gt;
&lt;P&gt;Better?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 21:27:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/848426#M335436</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-12-07T21:27:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why is x=x+y not working but x+y works?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/848430#M335439</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.earnings;
	Amount=1000;
	Rate=0.75/12;

	do month=1 to 12;
		Earned+(amount+earned)*rate;
	end;
run;&lt;/PRE&gt;
&lt;P&gt;if I find the above SUM statement too complicated, do you think this is an acceptabe/good substitute by breaking down the sum statement and add an extra variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.earnings;
	principal=1000;
	Rate=0.75/12;

	do month=1 to 12;
		retain earned 0;
		total_amount=principal+earned;
		earned=earned+(total_amount)*rate;
		output;
	end;
run;

proc print data=earnings;
	var principal rate month total_amount earned;
run;
&lt;/PRE&gt;
&lt;P&gt;I just find my version is easier to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 21:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-x-x-y-not-working-but-x-y-works/m-p/848430#M335439</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-12-07T21:40:16Z</dc:date>
    </item>
  </channel>
</rss>

