<?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: How do I retain a value within a group and calculate in current row? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617837#M181121</link>
    <description>Please show what you've tried as well. Any particular reason you're creating two copies of each data set?</description>
    <pubDate>Thu, 16 Jan 2020 17:41:38 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-01-16T17:41:38Z</dc:date>
    <item>
      <title>How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617831#M181118</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello SAS Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am following up with a problem that I am stuck at. I want to calculate 2 variables - a) one depends on a previous record and b) depends on a). Here is an illustrative example:&lt;/P&gt;&lt;P&gt;/*&lt;BR /&gt;If Index = 1 then Open=0&lt;BR /&gt;else Open=Prev_Close&lt;/P&gt;&lt;P&gt;Leeway=Limit-Close&lt;BR /&gt;*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Sample Have;
input ID Index Order Limit;
datalines;
1 1 5 50
1 2 12 50
1 3 45 50
2 1 10 100
2 2 30 100
2 3 15 100
;
run;

Data Sample_Evaluation Want;
input ID Index Order Limit Open Close Leeway;
datalines;
1 1 5 50 0 5 45
1 2 12 50 5 17 33
1 3 45 50 17 62 -12
2 1 10 100 0 10 90
2 2 30 100 10 40 60
2 3 15 100 40 55 45
;
run;

proc print data=Sample_Evaluation; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have looked at the following posts:&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/New-SAS-User/How-do-I-retain-a-value-within-a-group-based-on-several/m-p/591676#M15243" target="_blank"&gt;https://communities.sas.com/t5/New-SAS-User/How-do-I-retain-a-value-within-a-group-based-on-several/m-p/591676#M15243&lt;/A&gt;&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/370765#M88549" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/370765#M88549&lt;/A&gt;&lt;/P&gt;&lt;P&gt;but have not been successful at my attempts to generate desired results yet. Please advise. Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;bests&lt;/P&gt;&lt;P&gt;IC&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 17:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617831#M181118</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-16T17:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617837#M181121</link>
      <description>Please show what you've tried as well. Any particular reason you're creating two copies of each data set?</description>
      <pubDate>Thu, 16 Jan 2020 17:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617837#M181121</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-16T17:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617844#M181124</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by ID;
	retain open close leeway;

	if first.id then
		do;
			open=0;
			close=order;
			leeway=limit-order;
		end;
	else
		do;
			open=close;
			close=open + order;
			leeway=limit - close;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jan 2020 17:55:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617844#M181124</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-16T17:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617851#M181129</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/283996"&gt;@icatNYC&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;T think there is something missing in your algoritm, as you don't specify how close should be computed. Based on your output I came up with the following (using your input):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=oOrder); set have;
 	by ID;
	length Open Close Leeway 8;
	retain Open Close;
	oOrder = lag(Order);
	if first.ID then do;
		Open = 0;
		Close = Order;
	end;
	else do;
		Close = Close + Order;
		Open = Open + oOrder;
	end;
	leeway = Limit-Close;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="lag2.gif" style="width: 512px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35387i4CDF717265470892/image-size/large?v=v2&amp;amp;px=999" role="button" title="lag2.gif" alt="lag2.gif" /&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>Thu, 16 Jan 2020 18:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617851#M181129</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-01-16T18:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617887#M181141</link>
      <description>&lt;P&gt;I kept 2 datasets as it seems to be the posting convention to use Have/Want pairs. Here is what I have tried:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Option compress=yes;

Data Sample Have;
input ID Index Order Limit;
datalines;
1 1 5 50
1 2 12 50
1 3 45 50
2 1 10 100
2 2 30 100
2 3 15 100
;
run;

Data Sample_Evaluation;
retain Close;
set Sample;
if index=1 then do;
Open=0;
Close=Open+Order;
Leeway=Limit-Close;
end;

if index ge 1 then do;
Open=Close;
Close=Open+Order; 
Leeway=Limit-Close;
end;
run;

Data Sample_Evaluation;
retain ID Index Order Limit Open Close Leeway;
set Sample_Evaluation;
run;


Data Want;
input ID Index Order Limit Open Close Leeway;
datalines;
1 1 5 50 0 5 45
1 2 12 50 5 17 33
1 3 45 50 17 62 -12
2 1 10 100 0 10 90
2 2 30 100 10 40 60
2 3 15 100 40 55 45
;
run;

proc print data=Sample_Evaluation; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The print output is:&amp;nbsp;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;TABLE border="0" cellspacing="1" cellpadding="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;The SAS System&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Obs ID Index Order Limit Open Close Leeway123456 &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;28&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;67&lt;/TD&gt;&lt;TD&gt;-17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;80&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;65&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 16 Jan 2020 19:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617887#M181141</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-16T19:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617894#M181143</link>
      <description>&lt;P&gt;Yes, I did not post my attempt at the code earlier which has the logic to calculate close. I posted it as a response to Reeza's request but I should have been mindful earlier. my apologies!&amp;nbsp;&lt;/P&gt;&lt;P&gt;You have correctly guessed the intended logic for close though - I am evaluating your response.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 19:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617894#M181143</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-16T19:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617902#M181145</link>
      <description>&lt;P&gt;Thank you. I think this works for my sample but please give me a moment to verify on the larger complex condition that I am working on. I will confirm and post when done.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 19:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617902#M181145</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-16T19:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I retain a value within a group and calculate in current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617933#M181163</link>
      <description>&lt;P&gt;Thank you. The code works as intended. I am able to follow your steps.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 21:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-retain-a-value-within-a-group-and-calculate-in-current/m-p/617933#M181163</guid>
      <dc:creator>icatNYC</dc:creator>
      <dc:date>2020-01-16T21:08:59Z</dc:date>
    </item>
  </channel>
</rss>

