<?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: PROC SQL by Example in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537239#M147711</link>
    <description>Remove the CREATE TABLE portion if you don't want a table.</description>
    <pubDate>Wed, 20 Feb 2019 22:22:56 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-02-20T22:22:56Z</dc:date>
    <item>
      <title>PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537219#M147700</link>
      <description>&lt;P&gt;I have two data sets with common id variable. The first one has car_type and the second one has a flag variable. My goal is to use PROC SQL to find the number of car_type is "A" (in data 1) and flag = 1 (in data 2) by the id. For example, for the data sets below, the count would&amp;nbsp; be 1 since there are id = 1, 2, 6 with car_type of A (in data 1) and id = 1 with flag = 1 (in data 2).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks, see data code below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data old_1;
input id car_type $;
datalines;
1 A
2 A
3 B
4 C
5 D
6 A
;

data old_2;
input id flag;
datalines;
1 1
2 0
3 1
4 1
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Feb 2019 21:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537219#M147700</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-02-20T21:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537222#M147701</link>
      <description>&lt;P&gt;Let's start with what you have tried so far...&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 21:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537222#M147701</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-02-20T21:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537225#M147702</link>
      <description>&lt;P&gt;Since this is clearly homework or practice you should give it an attempt first, or at least show what you've tried if you've already done that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to join the data by ID&lt;/P&gt;
&lt;P&gt;See this example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n00fjmxaad37mgn1rszsatq472zs.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n00fjmxaad37mgn1rszsatq472zs.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And then filter via a WHERE statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have two data sets with common id variable. The first one has car_type and the second one has a flag variable. My goal is to use PROC SQL to find the number of car_type is "A" (in data 1) and flag = 1 (in data 2) by the id. For example, for the data sets below, the count would&amp;nbsp; be 1 since there are id = 1, 2, 6 with car_type of A (in data 1) and id = 1 with flag = 1 (in data 2).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks, see data code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data old_1;
input id car_type $;
datalines;
1 A
2 A
3 B
4 C
5 D
6 A
;

data old_2;
input id flag;
datalines;
1 1
2 0
3 1
4 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 21:45:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537225#M147702</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-20T21:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537232#M147704</link>
      <description>&lt;P&gt;I tried and almost there. Just haven't found a way to show the count only. So far, the code works but always showing the result as a table not a single number.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data old_1;
input id car_type $;
datalines;
1 A
2 A
3 B
4 C
5 D
6 A
;

data old_2;
input id flag;
datalines;
1 1
2 0
3 1
4 1
;

proc sql;

create table test as
  select 
    count(*) as count,
    t1.id
  from old_2 t1
  where t1.id in (select t2.id from old_1 t2 where car_type = 'A') and t1.flag 
  ;

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Feb 2019 22:08:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537232#M147704</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-02-20T22:08:21Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537239#M147711</link>
      <description>Remove the CREATE TABLE portion if you don't want a table.</description>
      <pubDate>Wed, 20 Feb 2019 22:22:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537239#M147711</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-20T22:22:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537244#M147712</link>
      <description>&lt;P&gt;You are amost there indeed. But notice the note&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;NOTE: The query requires remerging summary statistics back with the original data.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the log? It says that you have asked for a summary function (count) but also for a variable that is not summarized (id). So the count has to be copied next to every value of id in the result. Just remove the mention of id, and the problem disappears.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table test as
  select 
    count(*) as count
  from old_2
  where id in (select id from old_1 where car_type = 'A') and flag 
  ;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(I also removed unnecessary aliases)&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 22:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537244#M147712</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-02-20T22:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL by Example</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537256#M147718</link>
      <description>&lt;P&gt;Great! Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Feb 2019 23:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-by-Example/m-p/537256#M147718</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-02-20T23:39:29Z</dc:date>
    </item>
  </channel>
</rss>

