<?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 Adding the last 3 rows in a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10113#M681</link>
    <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I want to add the &lt;B&gt;last 3 rows for every row&lt;/B&gt; for a particular table.&lt;BR /&gt;
&lt;BR /&gt;
Suppose my table looked like this:&lt;BR /&gt;
&lt;BR /&gt;
col1&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
&lt;BR /&gt;
I want the resulting table to be (disregarding first two rows):&lt;BR /&gt;
&lt;BR /&gt;
col1&lt;BR /&gt;
6&lt;BR /&gt;
9&lt;BR /&gt;
12&lt;BR /&gt;
15&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
Sachin</description>
    <pubDate>Tue, 15 Feb 2011 01:05:27 GMT</pubDate>
    <dc:creator>SachinRuk</dc:creator>
    <dc:date>2011-02-15T01:05:27Z</dc:date>
    <item>
      <title>Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10113#M681</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I want to add the &lt;B&gt;last 3 rows for every row&lt;/B&gt; for a particular table.&lt;BR /&gt;
&lt;BR /&gt;
Suppose my table looked like this:&lt;BR /&gt;
&lt;BR /&gt;
col1&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
&lt;BR /&gt;
I want the resulting table to be (disregarding first two rows):&lt;BR /&gt;
&lt;BR /&gt;
col1&lt;BR /&gt;
6&lt;BR /&gt;
9&lt;BR /&gt;
12&lt;BR /&gt;
15&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
Sachin</description>
      <pubDate>Tue, 15 Feb 2011 01:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10113#M681</guid>
      <dc:creator>SachinRuk</dc:creator>
      <dc:date>2011-02-15T01:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10114#M682</link>
      <description>data one;&lt;BR /&gt;
	do col1 = 1 to 15;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data two;&lt;BR /&gt;
	set one;&lt;BR /&gt;
	col2=sum(col1,lag(col1),lag2(col1));&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 15 Feb 2011 01:53:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10114#M682</guid>
      <dc:creator>NickR</dc:creator>
      <dc:date>2011-02-15T01:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10115#M683</link>
      <description>is it possible to do it without the lag function? Only because as an example I said 3 but its most likely to be 12 and may vary.&lt;BR /&gt;
&lt;BR /&gt;
For example can we do this using _n_ and a do loop?&lt;BR /&gt;
/*for each _n_ (row)*/&lt;BR /&gt;
      /*loop 12 times with previous 12 rows and add up*/&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
Sachin</description>
      <pubDate>Tue, 15 Feb 2011 02:51:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10115#M683</guid>
      <dc:creator>SachinRuk</dc:creator>
      <dc:date>2011-02-15T02:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10116#M684</link>
      <description>Hi:&lt;BR /&gt;
  Based on the example data you posted, I don't understand how you will know whether you need to add 3 observation's values or whether you need to add 12 observation's values. Perhaps you have oversimplified your example data and there is more to explain about what you need to do???&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 15 Feb 2011 04:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10116#M684</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-02-15T04:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10117#M685</link>
      <description>Hi Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
Suppose I want to add the last three variables (forget what I said about trying to add variable number of rows), is there another way to do it without using a lag operator?&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Sachin</description>
      <pubDate>Tue, 15 Feb 2011 05:42:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10117#M685</guid>
      <dc:creator>SachinRuk</dc:creator>
      <dc:date>2011-02-15T05:42:46Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10118#M686</link>
      <description>Hi:&lt;BR /&gt;
  I don't feel there's enough information to even guess at an answer. For example...you have a dataset with &lt;I&gt;&lt;U&gt;ONLY&lt;/U&gt;&lt;/I&gt;&lt;U&gt;&lt;/U&gt; 1 variable or column (COL1) and some number of rows or observations?? There are no identifying variables?? It's also sort of hard to "forget" that you mentioned the possibility of a variable number of rows to add up (maybe 3, maybe 12). You do not want to use LAG, but why not?? Are you rejecting LAG for some reason??&lt;BR /&gt;
 &lt;BR /&gt;
  Possibly transposing the data and then using an ARRAY might work. Really, without more information, it's like putting a 500 piece puzzle together with someone who's handing me only 3 puzzle pieces at a time. &lt;BR /&gt;
&lt;BR /&gt;
  Perhaps other folks will have some better ideas.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 15 Feb 2011 06:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10118#M686</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-02-15T06:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10119#M687</link>
      <description>Could this be an oversimplified approach - for each set of 3 observations, only output the third observation (this example uses the SAS implicit OUTPUT approach):&lt;BR /&gt;
&lt;BR /&gt;
data x;&lt;BR /&gt;
do i=1 to 3;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 15 Feb 2011 11:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10119#M687</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2011-02-15T11:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10120#M688</link>
      <description>This might work for an input file.  Not sure about an input dataset or some of the syntax &lt;BR /&gt;
&lt;BR /&gt;
data work.tst(keep=sumcol);&lt;BR /&gt;
infile '/tst.txt';&lt;BR /&gt;
rows_to_add=3;&lt;BR /&gt;
i=rows_to_add;&lt;BR /&gt;
retain sumcol;&lt;BR /&gt;
if _N_ ge rows_to_add;&lt;BR /&gt;
i=rows_to_add;&lt;BR /&gt;
sumcol=0&lt;BR /&gt;
do while (i&amp;gt;0);&lt;BR /&gt;
    input #(-i) inputcol&lt;BR /&gt;
    sumcol = sumcol + inputcol;&lt;BR /&gt;
    I+ -1;&lt;BR /&gt;
    end;&lt;BR /&gt;
&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 15 Feb 2011 12:46:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10120#M688</guid>
      <dc:creator>DBailey</dc:creator>
      <dc:date>2011-02-15T12:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10121#M689</link>
      <description>I’m not sure if this is ultimately what you want, but it works for the simplified data that you posted.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
%let rows_to_sum = 3;&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
input var;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
	call symputx("numvars", n);&lt;BR /&gt;
	if 0 then set test nobs=n;&lt;BR /&gt;
run;&lt;BR /&gt;
data result;&lt;BR /&gt;
	array vars{&amp;amp;numvars} _temporary_;&lt;BR /&gt;
	do i=1 to dim(vars);&lt;BR /&gt;
		set test;&lt;BR /&gt;
		vars{i}=var;&lt;BR /&gt;
	end;&lt;BR /&gt;
	do i=1 to &amp;amp;numvars - &amp;amp;rows_to_sum + 1;&lt;BR /&gt;
		result = 0;&lt;BR /&gt;
		do j=i to i+&amp;amp;rows_to_sum-1;&lt;BR /&gt;
			result+vars{j};&lt;BR /&gt;
		end;&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
	keep result;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=result noobs;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 15 Feb 2011 15:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10121#M689</guid>
      <dc:creator>polingjw</dc:creator>
      <dc:date>2011-02-15T15:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table/parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10122#M690</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
You could use parameters and lags too:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro lagi(nlags=2);&lt;BR /&gt;
%local i;&lt;BR /&gt;
data sumlags(keep= sum);&lt;BR /&gt;
	set test ;&lt;BR /&gt;
	sum=col1;&lt;BR /&gt;
	%do i=1 %to &amp;amp;nlags;	&lt;BR /&gt;
		x=lag&amp;amp;i(col1);&lt;BR /&gt;
	  	sum=sum+x; &lt;BR /&gt;
	%end;	&lt;BR /&gt;
	if sum ne . then output;		&lt;BR /&gt;
 run;&lt;BR /&gt;
%mend lagi;&lt;BR /&gt;
%lagi(nlags=5);&lt;BR /&gt;
&lt;BR /&gt;
Warm regards,&lt;BR /&gt;
Vasile</description>
      <pubDate>Tue, 15 Feb 2011 16:21:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10122#M690</guid>
      <dc:creator>Vasile01</dc:creator>
      <dc:date>2011-02-15T16:21:59Z</dc:date>
    </item>
    <item>
      <title>Re: Adding the last 3 rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10123#M691</link>
      <description>Emmmmm.&lt;BR /&gt;
Maybe You will like stack skill which has mentioned by Art.C before.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
%let rows = 3;&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
input var;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
6&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data want(keep=sum);&lt;BR /&gt;
 set test;&lt;BR /&gt;
 array stack{&amp;amp;rows} _temporary_;&lt;BR /&gt;
 i=mod(_n_,&amp;amp;rows)+1;&lt;BR /&gt;
 stack{i}=var;&lt;BR /&gt;
 sum=sum(of stack{*});&lt;BR /&gt;
 if _n_ ge &amp;amp;rows then output;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 24 Feb 2011 07:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-the-last-3-rows-in-a-table/m-p/10123#M691</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-02-24T07:21:19Z</dc:date>
    </item>
  </channel>
</rss>

