<?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 Top Observations Of Each Grouping in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888194#M350932</link>
    <description>&lt;P&gt;I tried a few solutions online and kept getting errors. How do output the top 10 observations by grouping? Current code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.BUCKET1,
				t1.BUCKET2,
				t1.CODE,
				SUM(CASE WHEN t1.CRITERIA1='A' THEN t1.SUM_OF_CNT
					END) AS A_CNT
		FROM WORK.HAVE t1
		WHERE t1.CRITERIA2 = 'B'
		GROUP BY 1,2,3
		ORDER BY t1.BUCKET1,
				 t1.BUCKET2,
				 CALCULATED A_CNT DESC;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There can be any number of combinations of BUCKET1 and BUCKET2. With this code, each combination gets as many rows as there are CODES within that combination. How do I only output the top 10 codes for each combination?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
    <pubDate>Mon, 07 Aug 2023 18:19:59 GMT</pubDate>
    <dc:creator>mhoward2</dc:creator>
    <dc:date>2023-08-07T18:19:59Z</dc:date>
    <item>
      <title>Top Observations Of Each Grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888194#M350932</link>
      <description>&lt;P&gt;I tried a few solutions online and kept getting errors. How do output the top 10 observations by grouping? Current code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.BUCKET1,
				t1.BUCKET2,
				t1.CODE,
				SUM(CASE WHEN t1.CRITERIA1='A' THEN t1.SUM_OF_CNT
					END) AS A_CNT
		FROM WORK.HAVE t1
		WHERE t1.CRITERIA2 = 'B'
		GROUP BY 1,2,3
		ORDER BY t1.BUCKET1,
				 t1.BUCKET2,
				 CALCULATED A_CNT DESC;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There can be any number of combinations of BUCKET1 and BUCKET2. With this code, each combination gets as many rows as there are CODES within that combination. How do I only output the top 10 codes for each combination?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 18:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888194#M350932</guid>
      <dc:creator>mhoward2</dc:creator>
      <dc:date>2023-08-07T18:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: Top Observations Of Each Grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888198#M350934</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc rank data=have descending;
    var a_cnt;
    by bucket1 buckt2;
    ranks a_cnt_ranks;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When a_ctn_ranks is &amp;lt;= 10, those are the observations you want.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 18:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888198#M350934</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-07T18:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Top Observations Of Each Grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888202#M350937</link>
      <description>Thank you! Is there no way to do it within the same SQL Step?</description>
      <pubDate>Mon, 07 Aug 2023 18:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888202#M350937</guid>
      <dc:creator>mhoward2</dc:creator>
      <dc:date>2023-08-07T18:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Top Observations Of Each Grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888212#M350941</link>
      <description>&lt;P&gt;I would not even try to do this in SQL. It is the wrong tool for this problem. SAS has already programmed this for you in PROC RANK.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can it be done in SQL? I'm sure it can, I'm sure it is more difficult than PROC RANK, and I don't know how to do this in SQL (nor do I want to know).&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 19:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888212#M350941</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-07T19:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: Top Observations Of Each Grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888247#M350957</link>
      <description>&lt;P&gt;I really &lt;STRONG&gt;hate&lt;/STRONG&gt; requests involving an adjective like "top" without defining it. What does top mean in this context? The COUNT of combinations with the most observations is only ONE possible interpretation.&lt;/P&gt;
&lt;P&gt;Top could be by alphabetic or numeric value order the bucket variable(s)&lt;/P&gt;
&lt;P&gt;By the observations with the largest count of Bucket1 for each value of Bucket2 or vice versa.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm sure I could come up with a few.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not expect use to use your code for definition because you have said it is getting errors. So it obviously does not contain a workable definition of "top".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, any question about errors in code should include the LOG with the code and all the messages as well as the errors.&lt;/P&gt;
&lt;P&gt;Example data can go a long way as well. What if your data does not have any observations where Criteria1='A' and Criteria2='B'?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 22:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888247#M350957</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-07T22:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Top Observations Of Each Grouping</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888252#M350962</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in case you are using SAS Viya and CAS tables (datasets in CASLIBs)&amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A useful action is &lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=casanpg&amp;amp;docsetTarget=cas-simple-topk.htm&amp;amp;locale=en" rel="noopener noreferrer"&gt;simple.topK&lt;/A&gt;, which selects the top K and bottom K values for variables in a data set, based on a user-specified ranking order. The example below returns the top 5 and bottom 5 values for two variables based on their frequency:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="wp_syntax"&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="code"&gt;
&lt;PRE class="sas" style="font-family: monospace;"&gt;&lt;SPAN&gt;proc cas&lt;/SPAN&gt;;
simple.topk / &lt;SPAN&gt;table&lt;/SPAN&gt;=&lt;SPAN&gt;"TABLE_NAME"&lt;/SPAN&gt; 
              aggregator=&lt;SPAN&gt;"N"&lt;/SPAN&gt;,                        
              inputs=&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;SPAN&gt;"VAR1"&lt;/SPAN&gt;,&lt;SPAN&gt;"VAR2"&lt;/SPAN&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;,
              topk=&lt;SPAN&gt;5&lt;/SPAN&gt;,
              bottomk=&lt;SPAN&gt;5&lt;/SPAN&gt;;
&lt;SPAN&gt;quit&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Simple&lt;/STRONG&gt; is a rich action set with heaps of useful options covered in the &lt;A href="https://go.documentation.sas.com/?docsetId=casanpg&amp;amp;docsetTarget=cas-simple-freq.htm&amp;amp;docsetVersion=8.5&amp;amp;locale=en" rel="noopener noreferrer"&gt;documentation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;DIV id="ConnectiveDocSignExtentionInstalled" data-extension-version="1.0.4"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 07 Aug 2023 23:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Top-Observations-Of-Each-Grouping/m-p/888252#M350962</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-08-07T23:03:02Z</dc:date>
    </item>
  </channel>
</rss>

