<?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 Counting row by group through proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625066#M184203</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;How can i add count rows by groups through proc sql&lt;/P&gt;&lt;P&gt;Have dataset&lt;/P&gt;&lt;P&gt;x y&lt;/P&gt;&lt;P&gt;1 a&lt;/P&gt;&lt;P&gt;1 b&lt;/P&gt;&lt;P&gt;1 c&lt;/P&gt;&lt;P&gt;2 g&lt;/P&gt;&lt;P&gt;2 p&lt;/P&gt;&lt;P&gt;3 f&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;wanted output&amp;nbsp;&lt;/P&gt;&lt;P&gt;x y n&lt;/P&gt;&lt;P&gt;1 a 1&lt;/P&gt;&lt;P&gt;1 b 2&lt;/P&gt;&lt;P&gt;1 c 3&lt;/P&gt;&lt;P&gt;2 g 1&lt;/P&gt;&lt;P&gt;2 p 2&lt;/P&gt;&lt;P&gt;3 f 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Grouping variable is x&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i know&amp;nbsp; how to do it with data step&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sort data = have ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;if first.x then N =1;&lt;/P&gt;&lt;P&gt;else N+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However i am finding equivalent code in Proc SQL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"row_number() over partition by " does not work in proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 16 Feb 2020 07:52:40 GMT</pubDate>
    <dc:creator>V_R</dc:creator>
    <dc:date>2020-02-16T07:52:40Z</dc:date>
    <item>
      <title>Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625066#M184203</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;How can i add count rows by groups through proc sql&lt;/P&gt;&lt;P&gt;Have dataset&lt;/P&gt;&lt;P&gt;x y&lt;/P&gt;&lt;P&gt;1 a&lt;/P&gt;&lt;P&gt;1 b&lt;/P&gt;&lt;P&gt;1 c&lt;/P&gt;&lt;P&gt;2 g&lt;/P&gt;&lt;P&gt;2 p&lt;/P&gt;&lt;P&gt;3 f&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;wanted output&amp;nbsp;&lt;/P&gt;&lt;P&gt;x y n&lt;/P&gt;&lt;P&gt;1 a 1&lt;/P&gt;&lt;P&gt;1 b 2&lt;/P&gt;&lt;P&gt;1 c 3&lt;/P&gt;&lt;P&gt;2 g 1&lt;/P&gt;&lt;P&gt;2 p 2&lt;/P&gt;&lt;P&gt;3 f 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Grouping variable is x&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i know&amp;nbsp; how to do it with data step&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sort data = have ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by x;&lt;/P&gt;&lt;P&gt;if first.x then N =1;&lt;/P&gt;&lt;P&gt;else N+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However i am finding equivalent code in Proc SQL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"row_number() over partition by " does not work in proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 07:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625066#M184203</guid>
      <dc:creator>V_R</dc:creator>
      <dc:date>2020-02-16T07:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625069#M184206</link>
      <description>&lt;P&gt;With SQL use a Group By clause. Looking at your sample data/desired result: What's the grouping variable? Why is n=1 for 1a but n=2 for 1b? If the grouping variable would be X then n should be 3 for the first 3 rows, if it's Y then it should be 1 for all rows.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 07:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625069#M184206</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-16T07:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625070#M184207</link>
      <description>&lt;P&gt;Grouping variable is x&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 07:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625070#M184207</guid>
      <dc:creator>V_R</dc:creator>
      <dc:date>2020-02-16T07:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625072#M184209</link>
      <description>&lt;P&gt;The data step you've posted is not really implementing a count by group but it's just counting up and you reset the count by group. There is not really something equivalent in ANSI SQL (many databases have extensions ANSI SQL - analytic functions - which allow for similar things).&lt;/P&gt;
&lt;P&gt;With ANSI SQL you can have a count by group - but that works against sets of rows and not sequentially like with a SAS data step (compare the differences returned by below code).&lt;/P&gt;
&lt;P&gt;Once you understand the differences between a SAS data step and SQL you can take full advantage of it and use whatever you need.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input x y $;
  datalines;
1 a
1 b
1 c
2 g
2 p
3 f
;
proc print;
run;

data want;
  set have;
  by x;
  if first.x then
    N =1;
  else N+1;
run;
proc print;
run;

proc sql;
  select x, y, count(*) as n
  from have
  group by x
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 16 Feb 2020 08:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625072#M184209</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-16T08:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625075#M184212</link>
      <description>&lt;P&gt;For sql part&amp;nbsp;&lt;/P&gt;&lt;P&gt;output with your code is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;x y n&lt;BR /&gt;1 a 3
1 b 3
1 c 3
2 g 2
2 p 2
3 f 1&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;what i want is &lt;BR /&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;x y n&lt;BR /&gt;1 a 1
1 b 2
1 c 3
2 g 1
2 p 2
3 f 1&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 08:18:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625075#M184212</guid>
      <dc:creator>V_R</dc:creator>
      <dc:date>2020-02-16T08:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625077#M184214</link>
      <description>&lt;P&gt;As you see, it is very simple in a data step. Maxim 14: Use the Right Tool.&lt;/P&gt;
&lt;P&gt;Stay with the data step.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 08:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625077#M184214</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-16T08:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625078#M184215</link>
      <description>&lt;P&gt;That's what I've tried to tell you: Use a SAS data step for such a use case, use SAS SQL for a group count. Take advantage of the differences between the SAS data step and SQL.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 08:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625078#M184215</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-16T08:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625089#M184218</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; However i am finding equivalent code in Proc SQL.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;SQL is not very suited to processing rows in a given order.&lt;/P&gt;
&lt;P&gt;A data step is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt; "row_number() over partition by " does not work&lt;/P&gt;
&lt;P&gt;This non-ANSI feature is not implemented in SAS. Time to &lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/Add-window-functions-in-SAS-SQL/idi-p/462556" target="_self"&gt;vote&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2020 09:15:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625089#M184218</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-16T09:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625118#M184227</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/128563"&gt;@V_R&lt;/a&gt;&amp;nbsp; &amp;nbsp;These kind of questions seems more of university fun puzzles rather than a practical application in industry. How I wish I can reverse the clock and go back to Uni. I am missing those days at the lab.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The idea is to use MONOTONIC() and RANK by GROUP.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
  input x y $;
  datalines;
1 a
1 b
1 c
2 g
2 p
3 f
;
proc sql;
create table want as
select a.x,a.y,count(b.m) as n
from (select *,monotonic() as m from have) a
left join 
(select *,monotonic() as m from have) b
on a.x=b.x and b.m&amp;lt;=a.m
group by a.x,a.y
order by a.x,n;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 16 Feb 2020 14:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/625118#M184227</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-16T14:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: Counting row by group through proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/669663#M200911</link>
      <description>&lt;P&gt;You'd be surprised-- I came to this thread looking for a solution to this as a SAS user doing data analytics for business! Needed to randomly select X number of observations from each value of the grouped variable, with X being variable depending on the value. Idea being to evenly distribute the control group to with the other group based on this variable. So I randomly ordered my members in the group, and then needed to label them to prepare for selection. So your solution does have real-world value &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 19:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-row-by-group-through-proc-sql/m-p/669663#M200911</guid>
      <dc:creator>jmann132</dc:creator>
      <dc:date>2020-07-15T19:13:46Z</dc:date>
    </item>
  </channel>
</rss>

