<?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: Fetching previous row value to use in calculation in PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691752#M210598</link>
    <description>&lt;P&gt;If this is anything other than a homework exercise to get you thinking,&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;DONT'T DO IT.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;&lt;FONT size="3"&gt;The tool for such actions is the data step, period.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Oct 2020 06:04:01 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-10-15T06:04:01Z</dc:date>
    <item>
      <title>Fetching previous row value to use in calculation in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691743#M210594</link>
      <description>&lt;P&gt;Hello ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to calculate a value where I have to pick the value of A of the current rank and divide it with column B value of previous rank i.e. to calculate value of Rank 2 (Column C) = 164/13610.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The calculation has to be done in proc sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Rank&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;148&lt;/TD&gt;&lt;TD&gt;13610&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;164&lt;/TD&gt;&lt;TD&gt;14829&lt;/TD&gt;&lt;TD&gt;164/13610&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;219&lt;/TD&gt;&lt;TD&gt;19566&lt;/TD&gt;&lt;TD&gt;219/19566&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;247&lt;/TD&gt;&lt;TD&gt;24494&lt;/TD&gt;&lt;TD&gt;247/19566&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;475&lt;/TD&gt;&lt;TD&gt;26909&lt;/TD&gt;&lt;TD&gt;475/24494&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;576&lt;/TD&gt;&lt;TD&gt;31694&lt;/TD&gt;&lt;TD&gt;576/26909&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help how to do the calculation for each rank . Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Oct 2020 04:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691743#M210594</guid>
      <dc:creator>adisal</dc:creator>
      <dc:date>2020-10-15T04:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Fetching previous row value to use in calculation in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691747#M210595</link>
      <description>&lt;P&gt;It has been explained numerous times in this week alone, that calculation requiring row-order can't be done with proc sql - at least not with easy to read/maintain code. So here is a data-step solving your problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  _b = lag(b);
 
  if _n_ &amp;gt; 1 then do;
    c = a / _b;
  end;&lt;BR /&gt;&lt;BR /&gt;  drop _b; /* EDIT */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Oct 2020 05:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691747#M210595</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-10-15T05:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: Fetching previous row value to use in calculation in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691748#M210596</link>
      <description>&lt;P&gt;If the ranks in the first column, "Rank", are always sequential and always in order, then you could code the below.&amp;nbsp; I'm joining the table to itself.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the ranks are not always sequential, then a column would have to be added that would start with one and then increment by one until the end of the table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Unranked_Data;
	INFILE	DATALINES	DSD	DLM=','	MISSOVER;

	INPUT	Rank	
			A	
			B
			;

DATALINES;
1,	148,	13610,	 
2,	164,	14829,	13610
3,	219,	19566,	14829
4,	247,	24494,	19566
5,	475,	26909,	24494
6,	576,	31694,	26909
;
RUN;

PROC	SQL	NOPRINT;
	CREATE	TABLE	Ranked_Data	AS
		SELECT	Rank.Rank
				,Rank.A
				,Rank.B
				,(Rank.A/Prev.B)	AS	C	FORMAT	12.6
			FROM			Unranked_Data	Rank
			LEFT	JOIN	Unranked_Data	Prev
				ON	Rank.Rank	=	Prev.Rank +  1
				;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Oct 2020 05:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691748#M210596</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-15T05:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Fetching previous row value to use in calculation in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691752#M210598</link>
      <description>&lt;P&gt;If this is anything other than a homework exercise to get you thinking,&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;DONT'T DO IT.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="5"&gt;&lt;FONT size="3"&gt;The tool for such actions is the data step, period.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Oct 2020 06:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fetching-previous-row-value-to-use-in-calculation-in-PROC-SQL/m-p/691752#M210598</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-15T06:04:01Z</dc:date>
    </item>
  </channel>
</rss>

