<?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: Please help: how to fetch top 1 value within select statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74779#M16099</link>
    <description>Share your SAS-generated log with whatever error your are receiving - that would be more helpful to address any SAS program problem.  Also, start out with testing your program by either using the SASHELP sample datasets and/or you can build your own SAS sample data by reading a subset of your current master file to create a WORK file and then run/test your program.&lt;BR /&gt;
&lt;BR /&gt;
Regarding your post-reply, the SAS program code was likely truncated (due to characters you may have included with it) so read this "guidelines" suggestion post and then go back and EDIT your post or post another reply with your log and any errors along with code executed:&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://support.sas.com/forums/thread.jspa?messageID=27609" target="_blank"&gt;http://support.sas.com/forums/thread.jspa?messageID=27609&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.

Message was edited by: sbb</description>
    <pubDate>Sun, 11 Apr 2010 14:55:27 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2010-04-11T14:55:27Z</dc:date>
    <item>
      <title>Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74774#M16094</link>
      <description>What is the equivalent of "top n" within SAS? I'm having trouble with defining the statement pasted below:&lt;BR /&gt;
&lt;BR /&gt;
[pre]SELECT A.COLUMN1, A.COLUMN2, A.COLUMN3,&lt;BR /&gt;
(SELECT &lt;B&gt;TOP 1&lt;/B&gt; B.COLUM4 FROM &lt;U&gt;TABLE_B&lt;/U&gt; AS B&lt;BR /&gt;
WHERE A.COLUMN1 = SUBSTR(B.COLUMN1,1,17)) AS XX&lt;BR /&gt;
FROM &lt;U&gt;TABLE_A&lt;/U&gt; AS A[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Your help would be much appreciated.

Message was edited by: P12345678</description>
      <pubDate>Sun, 11 Apr 2010 11:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74774#M16094</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-04-11T11:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74775#M16095</link>
      <description>Suggest using Google advanced search argument below with the SAS support website resources.&lt;BR /&gt;
&lt;BR /&gt;
+"proc sql" +"top n" +report site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Sun, 11 Apr 2010 12:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74775#M16095</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-04-11T12:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74776#M16096</link>
      <description>I did that, of course, but I didn't find a solution to my problem.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for your answer.</description>
      <pubDate>Sun, 11 Apr 2010 13:03:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74776#M16096</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-04-11T13:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74777#M16097</link>
      <description>Hi:&lt;BR /&gt;
  Using Scott's Google search, this Tech Support note was one of the hits:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/30/867.html" target="_blank"&gt;http://support.sas.com/kb/30/867.html&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
and it shows a PROC FREQ approach to find the top N (along with Order=Freq) to display the FREQ results in descending order.&lt;BR /&gt;
&lt;BR /&gt;
and this paper (another hit)&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi25/25/aa/25p006.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi25/25/aa/25p006.pdf&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
shows a PROC SQL approach for top N.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Sun, 11 Apr 2010 13:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74777#M16097</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-04-11T13:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74778#M16098</link>
      <description>I've tried all of the solutions I found using google search, like outobs, fetchobs, etc. but I always get an error (I'm probably not using it correctly)...&lt;BR /&gt;
&lt;BR /&gt;
So if anyone can edit the short code I pasted, and not only paste links, it would be greatly appreciated....&lt;BR /&gt;
&lt;BR /&gt;
I also tried with distinct (since I know all the values per certain joining record are the same and I'm going to get one distinct value from table B per record in table A), but it takes hours to process in SAS EG and I have to kill it eventually. The 'A' table has more than 2 million rows.&lt;BR /&gt;
&lt;BR /&gt;
SELECT A.COLUMN1, A.COLUMN2, A.COLUMN3,&lt;BR /&gt;
(SELECT &lt;B&gt;DISTINCT &lt;/B&gt;B.COLUM4 FROM TABLE_B AS B&lt;BR /&gt;
WHERE A.COLUMN1 = SUBSTR(B.COLUMN1,1,17)) AS XX&lt;BR /&gt;
FROM TABLE_A AS A</description>
      <pubDate>Sun, 11 Apr 2010 14:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74778#M16098</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-04-11T14:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74779#M16099</link>
      <description>Share your SAS-generated log with whatever error your are receiving - that would be more helpful to address any SAS program problem.  Also, start out with testing your program by either using the SASHELP sample datasets and/or you can build your own SAS sample data by reading a subset of your current master file to create a WORK file and then run/test your program.&lt;BR /&gt;
&lt;BR /&gt;
Regarding your post-reply, the SAS program code was likely truncated (due to characters you may have included with it) so read this "guidelines" suggestion post and then go back and EDIT your post or post another reply with your log and any errors along with code executed:&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://support.sas.com/forums/thread.jspa?messageID=27609" target="_blank"&gt;http://support.sas.com/forums/thread.jspa?messageID=27609&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.

Message was edited by: sbb</description>
      <pubDate>Sun, 11 Apr 2010 14:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74779#M16099</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-04-11T14:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74780#M16100</link>
      <description>There are various ways in SAS to do this. Here a SQL approach:&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  do GroupId=1 to 5;&lt;BR /&gt;
    do id=1 to 10;&lt;BR /&gt;
      var=ceil(ranuni(0)*50);&lt;BR /&gt;
      output;&lt;BR /&gt;
    end;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select a.*&lt;BR /&gt;
    from have as a inner join&lt;BR /&gt;
      (select groupid,max(var) as var &lt;BR /&gt;
        from have &lt;BR /&gt;
          group by groupid) as b&lt;BR /&gt;
    on a.groupid=b.groupid and a.var=b.var&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Mon, 12 Apr 2010 09:53:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74780#M16100</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-04-12T09:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: Please help: how to fetch top 1 value within select statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74781#M16101</link>
      <description>Thanks a lot Patrick! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 12 Apr 2010 16:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Please-help-how-to-fetch-top-1-value-within-select-statement/m-p/74781#M16101</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-04-12T16:22:41Z</dc:date>
    </item>
  </channel>
</rss>

