<?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: What is the equivalent SQL code for first. or last. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899305#M355468</link>
    <description>And what is the rule? Which observation should get picked in that case?</description>
    <pubDate>Thu, 19 Oct 2023 17:48:55 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-10-19T17:48:55Z</dc:date>
    <item>
      <title>What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899243#M355442</link>
      <description>&lt;P&gt;Is there an SQL equivalent to the following code?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data tst1;&lt;BR /&gt;infile cards delimiter='09x';&lt;BR /&gt;input st $2. @5 id1 $6. @13 id2 $6. @21 pay dollar10.2;&lt;BR /&gt;cards;&lt;BR /&gt;AK  000753  352689  $945.00&lt;BR /&gt;AK  000753  332446  $14,175.00&lt;BR /&gt;AK  079773  047274  $0.00&lt;BR /&gt;AK  080409  744304  $0.00&lt;BR /&gt;AK  094749  348343  $18,794.28&lt;BR /&gt;AK  109935  709535  $0.00&lt;BR /&gt;AK  143363  378625  $132,141.56&lt;BR /&gt;CA  332181  398805  $381.43&lt;BR /&gt;CO  062290  612954  $0.00&lt;BR /&gt;CT  094749  718936  $0.00&lt;BR /&gt;CT  094749  201278  $4,355.00&lt;BR /&gt;FL  070780  372943  $0.00&lt;BR /&gt;FL  070780  352206  $6,433.83&lt;BR /&gt;IL  073560  566269  $15,776.78&lt;BR /&gt;MD  118837  319500  $960.30&lt;BR /&gt;NJ  173824  983294  $6,227.86&lt;BR /&gt;NM  076332  889071  $0.00&lt;BR /&gt;OR  159021  656278  $0.00&lt;BR /&gt;TX  158684  156785  $0.00&lt;BR /&gt;TX  158684  849608  $80.03&lt;BR /&gt;;;;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=tst1;&lt;BR /&gt;   by st id1 pay;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data tst2;&lt;BR /&gt;   set tst1;&lt;BR /&gt;   by st id1;&lt;BR /&gt;   if last.id1;&lt;BR /&gt;run;&lt;BR /&gt;   &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 16:06:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899243#M355442</guid>
      <dc:creator>Julie4435637</dc:creator>
      <dc:date>2023-10-19T16:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899245#M355443</link>
      <description>&lt;P&gt;There is no equivalent. Use DATA step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 14:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899245#M355443</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-19T14:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899246#M355444</link>
      <description>&lt;P&gt;None. SQL is intended to operate on sets of values. So there is no "first" or "last" operator.&lt;/P&gt;
&lt;P&gt;If you expect to identify a "first" value then you need to add a variable to the data set for the order you expect and to identify such before any SQL code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps if you describe what you are attempting to do in SQL someone can suggest an alternate approach.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 14:18:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899246#M355444</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-19T14:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899247#M355445</link>
      <description>&lt;P&gt;I don't think it is possible to re-write in proc sql.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In SQL(like ms sql , or Teradata SQL) , if you wish to write then may be will have to carefully partition by and rank function (window functions in general) .&lt;/P&gt;&lt;P&gt;but partition by and rank functions do not work in Proc sql .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, writing it using data step is the way forward i suppose.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot .&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 14:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899247#M355445</guid>
      <dc:creator>okayMrN</dc:creator>
      <dc:date>2023-10-19T14:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899278#M355456</link>
      <description>There isn't one, but if you examine the underlying assumptions, there usually is a workaround. For example, you may have another variable such as a date, where the last record really reflects the last date or something like that. Unfortunately that does rely on understanding the data and problem.</description>
      <pubDate>Thu, 19 Oct 2023 15:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899278#M355456</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-19T15:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899292#M355461</link>
      <description>&lt;P&gt;I just updated my post with a small sample.&amp;nbsp; I basically want to keep records within a by group (st &amp;amp; id1) with the max value of pay. This code may have to converted to use in SQL later - that is why I would like to avoid data step.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 16:08:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899292#M355461</guid>
      <dc:creator>Julie4435637</dc:creator>
      <dc:date>2023-10-19T16:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899294#M355462</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39634"&gt;@Julie4435637&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I just updated my post with a small sample.&amp;nbsp; I basically want to keep records within a by group (st &amp;amp; id1) with the max value of pay. This code may have to converted to use in SQL later - that is why I would like to avoid data step.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as 
   select *
   from tst1
   group by ST,id1
   having pay= max(pay)
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;but duplicate max values will appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 16:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899294#M355462</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-19T16:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899296#M355463</link>
      <description>&lt;P&gt;This will get you close...but if you have duplicate maximums in the data table, the SQL will not match the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select t1.* from tst1 as t1
right join(select st, id1, max(pay) as max_pay from tst1 group by st, id1) as f
on t1.st=f.st and t1.id1 = f.id1 and t1.pay= f.max_pay;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This use case will 'break' the match.&lt;/P&gt;
&lt;PRE&gt;data tst1;
infile cards delimiter='09x';
input st $2. @5 id1 $6. @13 id2 $6. @21 pay dollar10.2;
cards;
AK  000753  352689  $945.00
AK  000753  332446  $14,175.00
AK  079773  047274  $0.00
AK  080409  744304  $0.00
AK  094749  348343  $18,794.28
AK  109935  709535  $0.00
AK  143363  378625  $132,141.56
CA  332181  398805  $381.43
CO  062290  612954  $0.00
CT  094749  718936  $0.00
CT  094749  201278  $4,355.00
FL  070780  372943  $0.00
FL  070780  352206  $6,433.83
IL  073560  566269  $15,776.78
MD  118837  319500  $960.30
NJ  173824  983294  $6,227.86
NM  076332  889071  $0.00
OR  159021  656278  $0.00
&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;OR  159021  656279  $0.00&lt;/STRONG&gt;&lt;/FONT&gt;
TX  158684  156785  $0.00
TX  158684  849608  $80.03
;;;;
run;&lt;/PRE&gt;
&lt;P&gt;SQL will return both records as they both match the maximum but SAS will only keep the last record. Which is correct depends on your usage.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 16:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899296#M355463</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-19T16:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899304#M355467</link>
      <description>&lt;P&gt;That is a problem when the max(pay) has duplicates within a group.&amp;nbsp; I need the state&amp;amp;id1 to be distinct.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 17:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899304#M355467</guid>
      <dc:creator>Julie4435637</dc:creator>
      <dc:date>2023-10-19T17:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899305#M355468</link>
      <description>And what is the rule? Which observation should get picked in that case?</description>
      <pubDate>Thu, 19 Oct 2023 17:48:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899305#M355468</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-10-19T17:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899310#M355472</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39634"&gt;@Julie4435637&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I just updated my post with a small sample.&amp;nbsp; I basically want to keep records within a by group (st &amp;amp; id1) with the max value of pay. This code may have to converted to use in SQL later - that is why I would like to avoid data step.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Can you give us some context about why this has to be in SQL, in more depth than saying "may have to converted to us in SQL later"? As always, the context may be helpful in finding the right solution here. Because without context, the idea of taking code that works and translating it to some other part of SAS where it may not work seems like a poor idea.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 18:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899310#M355472</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-19T18:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899317#M355475</link>
      <description>&lt;P&gt;I don't have a rule but for my purposes it could be the the min(id2) value when there is a grouping where id1 and pay are the same.&amp;nbsp; I have the code to pull max(pay) but need something to pull just 1 record when there is a tie.&amp;nbsp; I wanted the code in a generic SQL version because this code will need to pushed eventually on another platform that doesn't use SAS but SQL based.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2023 19:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899317#M355475</guid>
      <dc:creator>Julie4435637</dc:creator>
      <dc:date>2023-10-19T19:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899362#M355490</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39634"&gt;@Julie4435637&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;... it could be the the min(id2) value when there is a grouping where id1 and pay are the same.&amp;nbsp; I have the code to pull max(pay) but need something to pull just 1 record when there is a tie.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could apply &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879" target="_blank" rel="noopener"&gt;Reeza&lt;/A&gt;'s &lt;A href="https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899296/highlight/true#M355463" target="_blank" rel="noopener"&gt;solution&lt;/A&gt;&amp;nbsp;to a view (in the outer query) where the ties have been removed:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;proc sql;&lt;/FONT&gt;
create view noties as
select st, id1, min(id2) as id2, pay from tst1
group by st, id1, pay;

&lt;FONT color="#999999"&gt;create table want as
select t1.* from &lt;FONT color="#000000"&gt;&lt;STRONG&gt;noties&lt;/STRONG&gt;&lt;/FONT&gt; as t1
right join(select st, id1, max(pay) as max_pay from tst1 group by st, id1) as f
on t1.st=f.st and t1.id1 = f.id1 and t1.pay= f.max_pay;&lt;/FONT&gt;

drop view noties;
&lt;FONT color="#999999"&gt;quit;&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Oct 2023 11:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899362#M355490</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-10-20T11:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: What is the equivalent SQL code for first. or last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899371#M355495</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39634"&gt;@Julie4435637&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check out this paper if you want to see SQL implementation for first. &amp;amp; last.&amp;nbsp;&lt;A title="Advanced Programming Techniques with PROC SQL" href="https://support.sas.com/resources/papers/proceedings17/0930-2017.pdf" target="_blank" rel="noopener"&gt;Advanced Programming Techniques with PROC SQL&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;If you are trying to apply the SQL to a third party Relational Database such as Teradata/Oracle to name few, you may need to check this paper&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title="Your Database Can Do SAS® Too!" href="https://support.sas.com/resources/papers/proceedings17/1180-2017.pdf" target="_blank" rel="noopener"&gt;Your Database Can Do SAS® Too! &lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 12:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-equivalent-SQL-code-for-first-or-last/m-p/899371#M355495</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-10-20T12:47:27Z</dc:date>
    </item>
  </channel>
</rss>

