<?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 question in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348647#M2277</link>
    <description>&lt;P&gt;To get good answers we need to see what your working with, so&amp;nbsp;&lt;STRONG&gt;post test data in the form of a datastep.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This is just a guess:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  count(COST_CENTER)
  from    (select distinct COST_CENTER from HAVE1 except select distinct COST_CENTER from HAVE2)
  group by  COST_CENTER;
quit;
  &lt;/PRE&gt;</description>
    <pubDate>Mon, 10 Apr 2017 10:51:45 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-04-10T10:51:45Z</dc:date>
    <item>
      <title>Proc SQL question</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348628#M2275</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am relatively new to the world of SQL and am now using SAS Studio as part of my role, so please forgive me if this is an obvious question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I&amp;nbsp;am trying to write a SQl statement that will tell me how many values in one list don't match those in another list, in this instance Cost Centres, but when i run the report it returns hundreds of thousands of rows rather than an expected 30 or 40. Can anyone signpost me as to where I am going wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;create table DQ As&lt;/P&gt;&lt;P&gt;select case when r.CostCentre&amp;nbsp;^= d.CostCentredq then 1&lt;BR /&gt;else 0&lt;BR /&gt;end as WrongCostCentre&lt;/P&gt;&lt;P&gt;from data.resources r, valibla.dq_res d;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 09:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348628#M2275</guid>
      <dc:creator>andrew928</dc:creator>
      <dc:date>2017-04-10T09:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL question</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348635#M2276</link>
      <description>&lt;P&gt;You can make this easier by doing a subquery, for example if we create a data set of records from SASHELP.CLASS of those aged under 15 like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data under_15;
	set sashelp.class(where=(age&amp;lt;15));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;We can then get a count of all records in SASHELP.CLASS where age is 15 or over with a subquery:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select count(age)
	from sashelp.class
	where age not in(select age from under_15);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to create a data set with the over 15's in then do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table over_15
	as select *
	from sashelp.class
	where age not in(select age from under_15);
quit;
	&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Apr 2017 10:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348635#M2276</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2017-04-10T10:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL question</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348647#M2277</link>
      <description>&lt;P&gt;To get good answers we need to see what your working with, so&amp;nbsp;&lt;STRONG&gt;post test data in the form of a datastep.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This is just a guess:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  count(COST_CENTER)
  from    (select distinct COST_CENTER from HAVE1 except select distinct COST_CENTER from HAVE2)
  group by  COST_CENTER;
quit;
  &lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Apr 2017 10:51:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/348647#M2277</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-10T10:51:45Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL question</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/362002#M2582</link>
      <description>&lt;P&gt;This may have been answered adequately already but I didn't see any closure...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The most direct answer to your question is that you forgot the where clause after the join. &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; create table DQ As&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; select &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;case &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; when r.CostCentre&amp;nbsp;^= d.CostCentredq then 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else 0&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end as WrongCostCentre&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; from data.resources r, &amp;nbsp;valibla.dq_res d&lt;/P&gt;&lt;P&gt;&lt;FONT color="#00CCFF"&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; where r.apples=d.apples AND r.oranges=d.oranges;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;quit; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you really just want the rows where WrongCostCentre &amp;nbsp;=1 then add to the where clause, or add the having clause:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; create table DQ As&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; select &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;case &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; when r.CostCentre&amp;nbsp;^= d.CostCentredq then 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else 0&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end as WrongCostCentre&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; from data.resources r, &amp;nbsp;valibla.dq_res d&lt;/P&gt;&lt;P&gt;&lt;FONT color="#00CCFF"&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/STRONG&gt;&lt;FONT color="#000000"&gt;where r.apples=d.apples AND r.oranges=d.oranges&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#00CCFF"&gt;&lt;FONT color="#000000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/FONT&gt;&lt;STRONG&gt;having&amp;nbsp;&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;WrongCostCentre=1;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt; &amp;nbsp;/* Some would consider this an abuse of the having clause */ &amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;quit; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, if you only want WrongCostCentre=1 rows, you don't need the CASE statement in the query. It would just be another condition in the where clause.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 May 2017 15:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/362002#M2582</guid>
      <dc:creator>optimist55116</dc:creator>
      <dc:date>2017-05-26T15:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL question</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/362584#M2609</link>
      <description>&lt;P&gt;I believe you are doing a cartesian comparison of the Nr cases in r vs the Nd cases in d.&amp;nbsp; Even if Nr=Nd (=N)&amp;nbsp;and every cost center was present in both , you'd still have N*N - N observations with a value of wrongcentre =1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I really think you wnat is a list of centre's present in only one of the two data sets.&amp;nbsp; If so, I'd suggest this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table DQ as
    select rcostcentre, costcentredq from
      data.resources as r full outer join valibla.dq_res as d
      on r.costcentre=d.costcentredq 
    except  
    select rcostcentre, costcentredq from
      data.resources as r inner join valibla.dq_res as d
      on r.costcentre=d.costcentredq;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did a test of this with sashelp.names:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class1 (rename=(name=name1)) class2 (rename=(name=name2));
  set sashelp.class (keep=name) end=eoc;
  output class1;
  if name='William' then name='XXXXX';
  output class2;
run;

proc sql;
  create table test as
    select name1, name2 from
       class1 FULL JOIN class2 on name1=name2
    EXCEPT
    select name1, name2 from
       class1 INNER JOIN class2 on name1=name2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 May 2017 01:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Proc-SQL-question/m-p/362584#M2609</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-30T01:37:54Z</dc:date>
    </item>
  </channel>
</rss>

