<?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: How to write a SQL query to summarize and filter a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309573#M66647</link>
    <description>&lt;P&gt;Have to use SQL ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ sub $ marks;
cards;
a maths 20
a sci 30
b maths 30
b sci 10
b myt 10
c math 30
c sci 25
;
run;
proc sort data=have;by name marks sub;run;

proc sql;
create table key as
select name,min(sub) as sub
from (select *,
(
select count(*) from (select distinct name,marks from have) as b
  where a.name=b.name and b.marks between a.marks and
   (select min(marks) from have where name=a.name)
) as idx
from have as a
where calculated idx=1
)
group by name;


create table want(drop=sum) as
select sum(marks) as sum,
case when not exists(select * from key where name=a.name and sub=a.sub)
 then ' ' else name end as name,
sub,
marks,
case when  not exists(select * from key where name=a.name and sub=a.sub)
 then . else 
 calculated sum end as total

 from have as a
  group by name;
quit;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5676iF39210089044C25B/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="x.png" title="x.png" /&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 06 Nov 2016 05:58:37 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-11-06T05:58:37Z</dc:date>
    <item>
      <title>How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309515#M66614</link>
      <description>&lt;P&gt;data abc;&lt;/P&gt;
&lt;P&gt;input name $ sub $ marks;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;P&gt;a maths 20&lt;/P&gt;
&lt;P&gt;a sci 30&lt;/P&gt;
&lt;P&gt;b maths 30&lt;/P&gt;
&lt;P&gt;b sci 10&lt;/P&gt;
&lt;P&gt;c math 30&lt;/P&gt;
&lt;P&gt;c sci 25&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1] here i want names to be appear for single time only&lt;BR /&gt;2] need to sum up marks for each name&lt;BR /&gt;3] need those name only whose total is greater than fifty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how to do this using sql ? thanks&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 17:16:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309515#M66614</guid>
      <dc:creator>Attyslogin</dc:creator>
      <dc:date>2016-11-05T17:16:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309517#M66615</link>
      <description>&lt;P&gt;Proc SQL;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select name, sum(marks) as total /*2*/&lt;/P&gt;
&lt;P&gt;from have&lt;/P&gt;
&lt;P&gt;group by name /*1*/&lt;/P&gt;
&lt;P&gt;having sum(marks)&amp;gt;50; /*3*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 17:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309517#M66615</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-05T17:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309519#M66616</link>
      <description>&lt;P&gt;Something like this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc;
input name $ sub $ marks;
cards;
a maths 20
a sci 30
b maths 30
b sci 10
c math 30
c sci 25
;
run;

proc sql;
   create table want as
   select name
         ,sum(marks) as sumofmarks
   from abc
   group by name
   having sumofmarks &amp;gt; 50
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Nov 2016 17:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309519#M66616</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-05T17:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309520#M66617</link>
      <description>&lt;P&gt;what if i want output in this form ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;name &amp;nbsp;sub &amp;nbsp; &amp;nbsp; marks total_marks&lt;BR /&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; math &amp;nbsp; &amp;nbsp;10 &amp;nbsp; &amp;nbsp; &amp;nbsp;50&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sci &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;math &amp;nbsp; &amp;nbsp; 30 &amp;nbsp; &amp;nbsp; &amp;nbsp;55&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sci &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 25&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 17:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309520#M66617</guid>
      <dc:creator>Attyslogin</dc:creator>
      <dc:date>2016-11-05T17:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309521#M66618</link>
      <description>&lt;P&gt;And it absolutely HAS to be done in proc sql? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 17:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309521#M66618</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-05T17:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309531#M66624</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88783"&gt;@Attyslogin&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;what if i want output in this form ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;name &amp;nbsp;sub &amp;nbsp; &amp;nbsp; marks total_marks&lt;BR /&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; math &amp;nbsp; &amp;nbsp;10 &amp;nbsp; &amp;nbsp; &amp;nbsp;50&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sci &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;math &amp;nbsp; &amp;nbsp; 30 &amp;nbsp; &amp;nbsp; &amp;nbsp;55&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sci &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 25&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you can use proc report to print it. Or possibly proc tabulate.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2016 21:26:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309531#M66624</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-05T21:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309573#M66647</link>
      <description>&lt;P&gt;Have to use SQL ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input name $ sub $ marks;
cards;
a maths 20
a sci 30
b maths 30
b sci 10
b myt 10
c math 30
c sci 25
;
run;
proc sort data=have;by name marks sub;run;

proc sql;
create table key as
select name,min(sub) as sub
from (select *,
(
select count(*) from (select distinct name,marks from have) as b
  where a.name=b.name and b.marks between a.marks and
   (select min(marks) from have where name=a.name)
) as idx
from have as a
where calculated idx=1
)
group by name;


create table want(drop=sum) as
select sum(marks) as sum,
case when not exists(select * from key where name=a.name and sub=a.sub)
 then ' ' else name end as name,
sub,
marks,
case when  not exists(select * from key where name=a.name and sub=a.sub)
 then . else 
 calculated sum end as total

 from have as a
  group by name;
quit;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5676iF39210089044C25B/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="x.png" title="x.png" /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Nov 2016 05:58:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309573#M66647</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-06T05:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SQL query to summarize and filter a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309587#M66658</link>
      <description>&lt;P&gt;Actually this could be simple as&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 name $ sub $ marks;
cards;
a maths 20
a sci 30
b maths 30
b sci 10
b myt 10
c math 30
c sci 25
;
run;
proc sql;
select
case when sub=min(sub) then name else ' ' end as new_name,
sub,
marks,
case when sub=min(sub) then sum(marks) else . end as total
 from have
  group by name
   order by name,sub;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Nov 2016 11:02:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SQL-query-to-summarize-and-filter-a-dataset/m-p/309587#M66658</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-06T11:02:46Z</dc:date>
    </item>
  </channel>
</rss>

