<?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 Group by count obs in proc sq; in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/712977#M219882</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dup;
set sashelp.class;
proc sort data =dup;
by age;
run;

data d;
set dup;
by Age ;
if first.Age then n=0;
n+1;proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi Guys&lt;/P&gt;&lt;P&gt;Good Morning&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to get same output using proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jan 2021 05:23:36 GMT</pubDate>
    <dc:creator>BrahmanandaRao</dc:creator>
    <dc:date>2021-01-21T05:23:36Z</dc:date>
    <item>
      <title>Group by count obs in proc sq;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/712977#M219882</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dup;
set sashelp.class;
proc sort data =dup;
by age;
run;

data d;
set dup;
by Age ;
if first.Age then n=0;
n+1;proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi Guys&lt;/P&gt;&lt;P&gt;Good Morning&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to get same output using proc sql&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 05:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/712977#M219882</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-01-21T05:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Group by count obs in proc sq;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/712978#M219883</link>
      <description>&lt;P&gt;You want an accumulating sum in SQL, correct?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 05:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/712978#M219883</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-21T05:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: Group by count obs in proc sq;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/713015#M219907</link>
      <description>&lt;P&gt;Very simple.&lt;/P&gt;
&lt;P&gt;COUNT(*) GROUP BY AGE in the SQL SELECT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Belay that, you want a running count.&lt;/P&gt;
&lt;P&gt;Maxim 14: Use the Right Tool, which is the data step. Don't waste your brain cycles on stupidity when there's a simple solution you already have.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 09:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/713015#M219907</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-21T09:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Group by count obs in proc sq;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/713017#M219909</link>
      <description>Hi Krutbremser&lt;BR /&gt;i want count as like as datastep output in proc sql&lt;BR /&gt;your code give only count age wise but i want&lt;BR /&gt;&lt;BR /&gt;--------------------------------------------------------------------------------&lt;BR /&gt;The SAS System&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Obs Name Sex Age Height Weight n&lt;BR /&gt;1 Joyce F 11 51.3 50.5 1&lt;BR /&gt;2 Thomas M 11 57.5 85.0 2&lt;BR /&gt;3 James M 12 57.3 83.0 1&lt;BR /&gt;4 Jane F 12 59.8 84.5 2&lt;BR /&gt;5 John M 12 59.0 99.5 3&lt;BR /&gt;6 Louise F 12 56.3 77.0 4&lt;BR /&gt;7 Robert M 12 64.8 128.0 5&lt;BR /&gt;8 Alice F 13 56.5 84.0 1&lt;BR /&gt;9 Barbara F 13 65.3 98.0 2&lt;BR /&gt;10 Jeffrey M 13 62.5 84.0 3&lt;BR /&gt;11 Alfred M 14 69.0 112.5 1&lt;BR /&gt;12 Carol F 14 62.8 102.5 2&lt;BR /&gt;13 Henry M 14 63.5 102.5 3&lt;BR /&gt;14 Judy F 14 64.3 90.0 4&lt;BR /&gt;15 Janet F 15 62.5 112.5 1&lt;BR /&gt;16 Mary F 15 66.5 112.0 2&lt;BR /&gt;17 Ronald M 15 67.0 133.0 3&lt;BR /&gt;18 William M 15 66.5 112.0 4&lt;BR /&gt;19 Philip M 16 72.0 150.0 1&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 21 Jan 2021 09:43:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/713017#M219909</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-01-21T09:43:46Z</dc:date>
    </item>
    <item>
      <title>Re: Group by count obs in proc sq;</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/713018#M219910</link>
      <description>&lt;P&gt;Just to show how it's done:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select
  *,
  (
    select count(*)
    from sashelp.class t2
    where t2.age = t1.age
    and t2.name le t1.name
  ) as n
from sashelp.class t1
order by age, name
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that sub-selects perform horribly, so on any larger dataset this will cause time and resource issues.&lt;/P&gt;
&lt;P&gt;To illustrate this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
do i = 1 to 100000;
  output;
end;
run;

proc sql;
create table want as
  select
    *,
    (
      select count(*)
      from class t2
      where t2.age = t1.age
      and t2.name le t1.name
    ) as n
  from class t1
  order by age, name
;
quit;

proc sort
  data=class
  out=dup
;
by age;
run;

data d;
set dup;
by Age;
if first.Age
then n = 1;
else n + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt; 73         data class;
 74         set sashelp.class;
 75         do i = 1 to 100000;
 76           output;
 77         end;
 78         run;
 
 NOTE: There were 19 observations read from the data set SASHELP.CLASS.
 NOTE: The data set WORK.CLASS has 1900000 observations and 6 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.10 seconds
       cpu time            0.09 seconds
       
 
 79         
 80         proc sql;
 81         create table want as
 82           select
 83             *,
 84             (
 85               select count(*)
 86               from class t2
 87               where t2.age = t1.age
 88               and t2.name le t1.name
 89             ) as n
 90           from class t1
 91           order by age, name
 92         ;
 NOTE: Table WORK.WANT created, with 1900000 rows and 7 columns.
 
 93         quit;
 NOTE:  Verwendet wurde: PROZEDUR SQL - (Gesamtverarbeitungszeit):
       real time           5.82 seconds
       cpu time            5.97 seconds
       
 
 94         
 95         proc sort
 96           data=class
 97           out=dup
 98         ;
 99         by age;
 100        run;
 
 NOTE: There were 1900000 observations read from the data set WORK.CLASS.
 NOTE: The data set WORK.DUP has 1900000 observations and 6 variables.
 NOTE:  Verwendet wurde: PROZEDUR SORT - (Gesamtverarbeitungszeit):
       real time           0.51 seconds
       cpu time            0.88 seconds
       
 
 101        
 102        data d;
 103        set dup;
 104        by Age;
 105        if first.Age
 106        then n = 1;
 107        else n + 1;
 108        run;
 
 NOTE: There were 1900000 observations read from the data set WORK.DUP.
 NOTE: The data set WORK.D has 1900000 observations and 7 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.31 seconds
       cpu time            0.30 seconds
&lt;/PRE&gt;
&lt;P&gt;You can see that there's already a factor of 6 between DATA and SQL; this difference grow exponentially with dataset sizes.&lt;/P&gt;
&lt;P&gt;Rule of thumb: in 90% of cases, DATA step solutions outperform SQL. On top of being easier to read and maintain.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 09:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-by-count-obs-in-proc-sq/m-p/713018#M219910</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-21T09:45:42Z</dc:date>
    </item>
  </channel>
</rss>

