<?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: Rolling calculation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868516#M343100</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use 980.2- 10 +2*x for the third row you do not get 970.6 you get 970.8.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input id    x   z;
datalines;
1   -0.1   1000
1   0.2    .
1   0.3    .
2   -0.2   1000
2   0.1    .
2   -0.3   .
;

data want;
   set have;
   by id;
   retain y;
   if first.id then y= z-10+2*x;
   else do;
      z=y;
      y= y-10+2*x;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;Please try to provide data in the form of data step code. We may make assumptions about values that do not exist about your data just showing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain will keep the value of a variable across the data step boundary.&lt;/P&gt;
&lt;P&gt;The By statement creates automatic variables First. and Last. that are numeric 1 or 0 to indicate whether the current observation is the first or last of that variables by group (1=true 0=false) and can be used to set/reset values conditionally at starts and end.&lt;/P&gt;
&lt;P&gt;After that it is just a matter of timing in the code when to assign z and when to calculate the new y.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't actually need the value of Y in the output set you can Drop it.&lt;/P&gt;</description>
    <pubDate>Thu, 06 Apr 2023 20:30:27 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-04-06T20:30:27Z</dc:date>
    <item>
      <title>Rolling calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868501#M343088</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to calculate a rolling sum, which includes a variable that needs to be updated for each row.&lt;/P&gt;
&lt;P&gt;The data I have is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp; x&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;-0.1&amp;nbsp; &amp;nbsp;1000&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0.2&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.2&amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to fill in the missing values of Z as follows:&lt;/P&gt;
&lt;P&gt;For the first row of each id form a new variable y equal to 1000- 10 +2*x. Then, the next value of z which is missing should be filled with the lagged value of y. This process starts again afresh for id=2 and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp; x&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;-0.1&amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1000-10+2*(-0.1)=989.8&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0.2&amp;nbsp; &amp;nbsp; &amp;nbsp; 989.8&amp;nbsp; &amp;nbsp; &amp;nbsp;989.8-10+2*0.2=980.2&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; 980.2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;970.6&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.2&amp;nbsp; &amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1000-10+2*(-0.2)=989.6&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 989.6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;979.8&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 979.8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;969.2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Costas&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 19:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868501#M343088</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-04-06T19:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868510#M343095</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;data want;
set have;
retain y;
if missing(z) then z = y;
y = z - 10 + 2 * x;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;id	x	   z	    y
1	-0.1	1000	989.8
1	0.2	989.8	980.2
1	0.3     980.2	970.8
2	-0.2	1000	989.6
2	0.1	989.6	979.8
2	-0.3	979.8	969.2&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Apr 2023 20:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868510#M343095</guid>
      <dc:creator>FloydNevseta</dc:creator>
      <dc:date>2023-04-06T20:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868513#M343097</link>
      <description>&lt;P&gt;Use a RETAINed variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
retain y;
if first.id
then y = z;
else z = y;
y = y - 10 + 2 * x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 20:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868513#M343097</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-06T20:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868515#M343099</link>
      <description>&lt;P&gt;Slightly different answers than you but I think this is the right idea:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $   x         z;
cards;
1     -0.1   1000
1      0.2      .
1       0.3      .
2       -0.2     1000
2        0.1        .
2        -0.3        .
;;;;


data want;
set have;
by id;
retain y;
if first.id then y = z;
y = y-10+2*x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs	id	x	z	y
1	1	-0.1	1000	989.8
2	1	0.2	.	980.2
3	1	0.3	.	970.8
4	2	-0.2	1000	989.6
5	2	0.1	.	979.8
6	2	-0.3	.	969.2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22290"&gt;@costasRO&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to calculate a rolling sum, which includes a variable that needs to be updated for each row.&lt;/P&gt;
&lt;P&gt;The data I have is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp; x&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;-0.1&amp;nbsp; &amp;nbsp;1000&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0.2&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.2&amp;nbsp; &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to fill in the missing values of Z as follows:&lt;/P&gt;
&lt;P&gt;For the first row of each id form a new variable y equal to 1000- 10 +2*x. Then, the next value of z which is missing should be filled with the lagged value of y. This process starts again afresh for id=2 and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp; x&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;z&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;-0.1&amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1000-10+2*(-0.1)=989.8&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 0.2&amp;nbsp; &amp;nbsp; &amp;nbsp; 989.8&amp;nbsp; &amp;nbsp; &amp;nbsp;989.8-10+2*0.2=980.2&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; 980.2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;970.6&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.2&amp;nbsp; &amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1000-10+2*(-0.2)=989.6&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 989.6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;979.8&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 979.8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;969.2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Costas&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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, 06 Apr 2023 20:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868515#M343099</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-06T20:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868516#M343100</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use 980.2- 10 +2*x for the third row you do not get 970.6 you get 970.8.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input id    x   z;
datalines;
1   -0.1   1000
1   0.2    .
1   0.3    .
2   -0.2   1000
2   0.1    .
2   -0.3   .
;

data want;
   set have;
   by id;
   retain y;
   if first.id then y= z-10+2*x;
   else do;
      z=y;
      y= y-10+2*x;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;Please try to provide data in the form of data step code. We may make assumptions about values that do not exist about your data just showing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain will keep the value of a variable across the data step boundary.&lt;/P&gt;
&lt;P&gt;The By statement creates automatic variables First. and Last. that are numeric 1 or 0 to indicate whether the current observation is the first or last of that variables by group (1=true 0=false) and can be used to set/reset values conditionally at starts and end.&lt;/P&gt;
&lt;P&gt;After that it is just a matter of timing in the code when to assign z and when to calculate the new y.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't actually need the value of Y in the output set you can Drop it.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2023 20:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868516#M343100</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-06T20:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868555#M343121</link>
      <description>&lt;P&gt;Thank you,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13813"&gt;@FloydNevseta&lt;/a&gt;&amp;nbsp; this works perfectly.&lt;/P&gt;
&lt;P&gt;The codes provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; also do what I want. Seems that&amp;nbsp; the correct use of retain is the key here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As advised, in the future, I will post a SAS data set&amp;nbsp; to make things easier.&lt;/P&gt;
&lt;P&gt;Thank you all very much!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2023 06:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-calculation/m-p/868555#M343121</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-04-07T06:17:13Z</dc:date>
    </item>
  </channel>
</rss>

