<?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 sas query how to find the n highest number in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240159#M44277</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;have to find the n highest no.&lt;/P&gt;&lt;P&gt;data salary;&lt;BR /&gt;input sal;&lt;BR /&gt;cards;&lt;BR /&gt;23&lt;BR /&gt;12&lt;BR /&gt;.&lt;BR /&gt;45&lt;BR /&gt;0&lt;BR /&gt;54&lt;BR /&gt;-21&lt;BR /&gt;43&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%let N=3;&lt;BR /&gt;proc sql;&lt;BR /&gt;select a.sal from salary a where &amp;amp;N= (select count(distinct sal) from salary b where a.sal&amp;lt;=b.sal);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please explain the logic that is used in this query.&lt;/P&gt;</description>
    <pubDate>Sun, 20 Dec 2015 05:32:39 GMT</pubDate>
    <dc:creator>pawandh</dc:creator>
    <dc:date>2015-12-20T05:32:39Z</dc:date>
    <item>
      <title>sas query how to find the n highest number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240159#M44277</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;have to find the n highest no.&lt;/P&gt;&lt;P&gt;data salary;&lt;BR /&gt;input sal;&lt;BR /&gt;cards;&lt;BR /&gt;23&lt;BR /&gt;12&lt;BR /&gt;.&lt;BR /&gt;45&lt;BR /&gt;0&lt;BR /&gt;54&lt;BR /&gt;-21&lt;BR /&gt;43&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%let N=3;&lt;BR /&gt;proc sql;&lt;BR /&gt;select a.sal from salary a where &amp;amp;N= (select count(distinct sal) from salary b where a.sal&amp;lt;=b.sal);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please explain the logic that is used in this query.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Dec 2015 05:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240159#M44277</guid>
      <dc:creator>pawandh</dc:creator>
      <dc:date>2015-12-20T05:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: sas query how to find the n highest number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240162#M44280</link>
      <description>&lt;P&gt;This is kind of &lt;SPAN&gt;Subqueries&lt;/SPAN&gt;&amp;nbsp;called "&lt;A href="http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/viewer.htm#a001361644.htm" target="_self"&gt;Correlated Subqueries&lt;/A&gt;"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where &lt;SPAN&gt;subquery requires a value or values to be passed to it by the outer query&lt;/SPAN&gt;&lt;SPAN&gt;. Where&amp;nbsp;the outer query select the row from the its table and then passes the value to the subquery.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So in the first step for example:&lt;/SPAN&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;SPAN style="line-height: 20px;"&gt;The outer query will select 23&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="line-height: 20px;"&gt;then pass it to the sub query&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;So it will look like:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;&lt;SPAN&gt;select count(distinct sal) from salary b where&amp;nbsp;&lt;STRONG&gt;23&lt;/STRONG&gt;&amp;lt;=b.sal;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so it will return the couunt of all sal &amp;lt; = 23: count(23:&amp;nbsp;&lt;SPAN&gt;23,45,54,43) =&lt;STRONG&gt; 4&lt;/STRONG&gt;, &amp;nbsp;which is the order of the number 23 in descending order&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Then the outer quey will look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;proc sql;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;select a.sal from salary a where &lt;STRONG&gt;3= 4&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;so it will not return any result as the condition in where is&amp;nbsp;not valid.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;....... it will continue like this till the subquery return 3&amp;nbsp;and this will happen when the outer query pass the third highest number which is 43 in your case.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;when that happen the sub query will look like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;&lt;SPAN&gt;select count(distinct sal) from salary b where&amp;nbsp;&lt;STRONG&gt;43&lt;/STRONG&gt;&amp;lt;=b.sal;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;so it will return the couunt of all sal &amp;lt; =&amp;nbsp;43: count(45,54,43) =3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then the outer quey will look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;proc sql;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;select a.sal from salary a where &lt;STRONG&gt;3=3&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;in that case the where statment will be&amp;nbsp;valid&amp;nbsp;and the outer query will return &lt;STRONG&gt;43&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Dec 2015 11:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240162#M44280</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2015-12-20T11:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: sas query how to find the n highest number</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240394#M44345</link>
      <description>&lt;P&gt;&lt;SPAN class="login-bold"&gt;Thanku&amp;nbsp;for such a deep n perfect explanation.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2015 04:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sas-query-how-to-find-the-n-highest-number/m-p/240394#M44345</guid>
      <dc:creator>pawandh</dc:creator>
      <dc:date>2015-12-22T04:48:29Z</dc:date>
    </item>
  </channel>
</rss>

