<?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: PROC SQL: Select records based on a variable value in the last record. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76713#M22239</link>
    <description>Assuming that data is sorted by YEAR (ascending), this would suit your needs:&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select max(YEAR) into :MAX_YEAR from DATA;&lt;BR /&gt;
create table RESULT as&lt;BR /&gt;
select * from DATA where &amp;amp;MAX_YEAR-3 le YEAR le &amp;amp;MAX_YEAR;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
First, the target year is read into a macro var.&lt;BR /&gt;
Then this value is used to filter the data accordingly.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
    <pubDate>Mon, 26 Oct 2009 12:23:00 GMT</pubDate>
    <dc:creator>DanielSantos</dc:creator>
    <dc:date>2009-10-26T12:23:00Z</dc:date>
    <item>
      <title>PROC SQL: Select records based on a variable value in the last record.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76712#M22238</link>
      <description>SAS table sorted by YEAR:&lt;BR /&gt;
YEAR X Y Z&lt;BR /&gt;
2000   2 7 5&lt;BR /&gt;
2000   3 4 6&lt;BR /&gt;
.&lt;BR /&gt;
2001   1 2 7&lt;BR /&gt;
.&lt;BR /&gt;
2009   7 5 1       (last record)&lt;BR /&gt;
&lt;BR /&gt;
Based on the  value 2009 for YEAR in the &lt;B&gt;last record&lt;/B&gt; I want to select all records with YEAR &amp;gt;= 2009 - 4,  i.e. the records for the last 5 years.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;The value for YEAR in the last record is not supposed to be known.&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
So if YEAR=2007 in the last record I would like to select all records with YEAR in [2003,2007]. How can this be done with PROC SQL?

Message was edited by: ErnestoC</description>
      <pubDate>Mon, 26 Oct 2009 10:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76712#M22238</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-10-26T10:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: Select records based on a variable value in the last record.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76713#M22239</link>
      <description>Assuming that data is sorted by YEAR (ascending), this would suit your needs:&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select max(YEAR) into :MAX_YEAR from DATA;&lt;BR /&gt;
create table RESULT as&lt;BR /&gt;
select * from DATA where &amp;amp;MAX_YEAR-3 le YEAR le &amp;amp;MAX_YEAR;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
First, the target year is read into a macro var.&lt;BR /&gt;
Then this value is used to filter the data accordingly.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Mon, 26 Oct 2009 12:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76713#M22239</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-10-26T12:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: Select records based on a variable value in the last record.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76714#M22240</link>
      <description>If the table is large, you would might consider to use data step techniques to read the last record directly, &lt;BR /&gt;
since the SQL select() max will result in a resource consuming table scan.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Mon, 26 Oct 2009 15:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76714#M22240</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-10-26T15:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: Select records based on a variable value in the last record.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76715#M22241</link>
      <description>why ONLY SQL?&lt;BR /&gt;
There must be some reason you could explain!&lt;BR /&gt;
Because SQL does not acknowledge row-order, it is unable to take advantage of that information, which is available to a data step reading a SAS table.&lt;BR /&gt;
Perhaps your undeclared reason might point to alternative approaches like dbms-specific opportunities.&lt;BR /&gt;
Would you like to explain why you can't use a data step for this (it makes reading SAS data in reverse, easy).&lt;BR /&gt;
 &lt;BR /&gt;
PeterC</description>
      <pubDate>Tue, 27 Oct 2009 09:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Select-records-based-on-a-variable-value-in-the-last/m-p/76715#M22241</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-10-27T09:06:02Z</dc:date>
    </item>
  </channel>
</rss>

