<?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 Using previous row to compute new values.  data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637771#M189612</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi, I'm having a headache computing new variables based on the observation above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have variables&amp;nbsp; P_1, bad and goods in a merged table, from I am calculating as follows:&lt;/P&gt;&lt;P&gt;Cumbad is 1 for the first observations, then Cumbad + bad. Is the cumulative values of bad.&lt;/P&gt;&lt;P&gt;Cumgood is the same as cumbad.&lt;/P&gt;&lt;P&gt;CumPercentBad is Cumbad / total of bads, which do not know how to calculate it. I tried max(bads) not success at all. So, just put the whole number as such.&lt;/P&gt;&lt;P&gt;CumPercentGood the same as above.&lt;/P&gt;&lt;P&gt;Total accepted is cumulative of the total of bads and goods in descending order. If it the first observation then total accepted 5000, else, total accepted minus bad (from the above observation), minus good (from the above observations).&lt;/P&gt;&lt;P&gt;Solving the problem from total accepted should fix the problem with the rest of the variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I tried.&lt;/P&gt;&lt;P&gt;I uploaded a txt if you want to give it a try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data matrix;
	set merged;
	* by P_1;
	* retain CumBad CumGood; 
	retain _all_ ;
		  If _n_ = 1 then CumBad + Bad; else CumBad = CumBad + Bad; * works fine;
		  If _n_ = 1 then CumGood + Good; else CumGood = CumGood + Good; * works fine;
		  Cum_Perc_Bad = CumBad / 747; * Total of Bads, in fact, 747 is the max value of CumBads. works fine;
		  Cum_Perc_Good = CumGood / 4253; * Total of Gods; 4253 is the max value of CumGood. works fine;
		  Abs_diff = abs(Cum_Perc_Bad - Cum_Perc_Good); * works fine;
		  if _n_ = 1 then Total_Accepted + 5000 ; else Total_Accepted = Total_Accepted - Bad - Good; * Total accepted starts in 5000. esle Total_Accepted - bad[n-1] and good [n-1]. It fails, as it takes the current bad and good value observations instead of the above ;
		  Accepted_rate = Total_Accepted / 5000 ; * 5000 total number of goods and bads, in fact, the sum of bads and goods. Works fine;
		  If _n_ = 1 then Num_Bads_among_Acc +  747; else Num_Bads_among_Acc = Num_Bads_among_Acc - Bad; * 747 is the total os bads. It fails, as it takes bad[n] instead of bad [n-1];	  
Bad_Rate_among_Acc = Num_Bads_among_Acc / Total_Accepted;
	      If _n_ = 1 then Num_Goods_among_Acc + 4253; else Num_Goods_among_Acc = Num_Goods_among_Acc - Good ; * 4253 is the total of goods. It fails, as it takes good[n] instead of good [n-1];
		  Marg_BR = Good / Total_Accepted ; * It works ;
run; 
proc print data = matrix; format Accepted_rate Bad_Rate_among_Acc percent8.2; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I am getting is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Output.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37944i72103EE18190A564/image-size/large?v=v2&amp;amp;px=999" role="button" title="Output.PNG" alt="Output.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The desired output would be like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37943i2A30D184F864F462/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;P&gt;Thank you in advance.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Apr 2020 02:06:42 GMT</pubDate>
    <dc:creator>OscarUvalle</dc:creator>
    <dc:date>2020-04-06T02:06:42Z</dc:date>
    <item>
      <title>Using previous row to compute new values.  data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637771#M189612</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi, I'm having a headache computing new variables based on the observation above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have variables&amp;nbsp; P_1, bad and goods in a merged table, from I am calculating as follows:&lt;/P&gt;&lt;P&gt;Cumbad is 1 for the first observations, then Cumbad + bad. Is the cumulative values of bad.&lt;/P&gt;&lt;P&gt;Cumgood is the same as cumbad.&lt;/P&gt;&lt;P&gt;CumPercentBad is Cumbad / total of bads, which do not know how to calculate it. I tried max(bads) not success at all. So, just put the whole number as such.&lt;/P&gt;&lt;P&gt;CumPercentGood the same as above.&lt;/P&gt;&lt;P&gt;Total accepted is cumulative of the total of bads and goods in descending order. If it the first observation then total accepted 5000, else, total accepted minus bad (from the above observation), minus good (from the above observations).&lt;/P&gt;&lt;P&gt;Solving the problem from total accepted should fix the problem with the rest of the variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I tried.&lt;/P&gt;&lt;P&gt;I uploaded a txt if you want to give it a try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data matrix;
	set merged;
	* by P_1;
	* retain CumBad CumGood; 
	retain _all_ ;
		  If _n_ = 1 then CumBad + Bad; else CumBad = CumBad + Bad; * works fine;
		  If _n_ = 1 then CumGood + Good; else CumGood = CumGood + Good; * works fine;
		  Cum_Perc_Bad = CumBad / 747; * Total of Bads, in fact, 747 is the max value of CumBads. works fine;
		  Cum_Perc_Good = CumGood / 4253; * Total of Gods; 4253 is the max value of CumGood. works fine;
		  Abs_diff = abs(Cum_Perc_Bad - Cum_Perc_Good); * works fine;
		  if _n_ = 1 then Total_Accepted + 5000 ; else Total_Accepted = Total_Accepted - Bad - Good; * Total accepted starts in 5000. esle Total_Accepted - bad[n-1] and good [n-1]. It fails, as it takes the current bad and good value observations instead of the above ;
		  Accepted_rate = Total_Accepted / 5000 ; * 5000 total number of goods and bads, in fact, the sum of bads and goods. Works fine;
		  If _n_ = 1 then Num_Bads_among_Acc +  747; else Num_Bads_among_Acc = Num_Bads_among_Acc - Bad; * 747 is the total os bads. It fails, as it takes bad[n] instead of bad [n-1];	  
Bad_Rate_among_Acc = Num_Bads_among_Acc / Total_Accepted;
	      If _n_ = 1 then Num_Goods_among_Acc + 4253; else Num_Goods_among_Acc = Num_Goods_among_Acc - Good ; * 4253 is the total of goods. It fails, as it takes good[n] instead of good [n-1];
		  Marg_BR = Good / Total_Accepted ; * It works ;
run; 
proc print data = matrix; format Accepted_rate Bad_Rate_among_Acc percent8.2; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I am getting is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Output.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37944i72103EE18190A564/image-size/large?v=v2&amp;amp;px=999" role="button" title="Output.PNG" alt="Output.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The desired output would be like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37943i2A30D184F864F462/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;P&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Apr 2020 02:06:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637771#M189612</guid>
      <dc:creator>OscarUvalle</dc:creator>
      <dc:date>2020-04-06T02:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using previous row to compute new values.  data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637772#M189613</link>
      <description>&lt;P&gt;Your RETAIN statement is doing nothing.&amp;nbsp; The _ALL_ keyword is evaluated when the statement is compiled.&amp;nbsp; At that point the only variables that have been defined to the compiler are the ones being read from the input dataset MERGED.&amp;nbsp; But variables being read from source datasets are always retained.&amp;nbsp; So it does nothing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either list the actual variables that you want to retain. Or move it to the end of the data step where the _ALL_ variable list will actually include ALL of the variables.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Apr 2020 02:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637772#M189613</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-06T02:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using previous row to compute new values.  data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637870#M189655</link>
      <description>&lt;P&gt;Yes Tom, I have now stated all the variables I need to retain, yet not having the desired output.&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the values are ok for the first row but not for the rest. The problem I think is in the "else" section where I need to take the three values from the row above, instead, it is taking the values within the current row.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data matrix;
	set merged;
	retain CumBad CumGood Bad Good Total_Accepted 5000 Num_Bads_among_Acc 747  Num_Goods_among_Acc 4253;
		  If _n_ = 1 then CumBad + Bad; else CumBad = CumBad + Bad; * connected to retain;
		  If _n_ = 1 then CumGood + Good; else CumGood = CumGood + Good; * connected to retain;
		  Cum_Perc_Bad = CumBad / 747; * Total of Bads;
		  Cum_Perc_Good = CumGood / 4253; * Total of Gods;
		  IF _n_ = 1 then Total_Accepted + 0 ; else Total_Accepted = Total_Accepted - Bad - Good ;
		  Abs_diff = abs(Cum_Perc_Bad - Cum_Perc_Good);
		  Accepted_rate = Total_Accepted / 5000 ; * 5000 total number of goods and bads;
		  If _n_ = 1 then Num_Bads_among_Acc +  0; else Num_Bads_among_Acc = Num_Bads_among_Acc - Bad; * connected to retain;
		  Bad_Rate_among_Acc = Num_Bads_among_Acc / Total_Accepted  ;
	      If _n_ = 1 then Num_Goods_among_Acc + 0; else Num_Goods_among_Acc = Num_Goods_among_Acc - Good ; * connected to retain;
		  Marg_BR = Good / Total_Accepted ;
run; 
proc print data = matrix; format Accepted_rate Bad_Rate_among_Acc percent8.2; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Apr 2020 15:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637870#M189655</guid>
      <dc:creator>OscarUvalle</dc:creator>
      <dc:date>2020-04-06T15:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using previous row to compute new values.  data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637873#M189657</link>
      <description>I do not see where you are MAKING this new variable BAD that you want to retain from the previous observation.&lt;BR /&gt;If BAD is a variable that is being read from the input data then you need assign it to a new variable that you can retain.  Or use the LAG() function. Make sure to NOT use the LAG() conditionally.&lt;BR /&gt;Something like:&lt;BR /&gt;lag_bad=lag(bad);&lt;BR /&gt;if xxxx then yyy = zzz + bad;&lt;BR /&gt;else yyy = zzz + lag_bad;&lt;BR /&gt;</description>
      <pubDate>Mon, 06 Apr 2020 15:40:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-previous-row-to-compute-new-values-data-step/m-p/637873#M189657</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-06T15:40:08Z</dc:date>
    </item>
  </channel>
</rss>

