<?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: SAS proc sql giving wrong results in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490400#M128296</link>
    <description>&lt;P&gt;LOL #31&lt;/P&gt;
&lt;P&gt;All other SQL parsers manage the feat, so while maybe not a defect per se (and while I much prefer the calculated or 1,2 syntaxes anyway) I'd suggest this is at least a gap or a weakness.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Aug 2018 10:19:13 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2018-08-28T10:19:13Z</dc:date>
    <item>
      <title>SAS proc sql giving wrong results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490005#M128082</link>
      <description>&lt;P&gt;Writing some SQL code which gives results that does not make sense. Is this a bug or is it suppose to be like this? I have used several SQL engines and none gives this results. Two examples below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc SQL;&lt;/P&gt;&lt;P&gt;create table test (&lt;BR /&gt;a varchar(8),&lt;BR /&gt;b varchar(8));&lt;BR /&gt;&lt;BR /&gt;insert into test (a, b) values ('XX_ABC01', 'ABCDXX01');&lt;BR /&gt;insert into test (a, b) values ('XX_ABC01', 'ABCDXX01');&lt;BR /&gt;insert into test (a, b) values ('XX_ABC02', 'ABCDXX02');&lt;BR /&gt;insert into test (a, b) values ('XY_DCY02', 'ABCDXX02');&lt;/P&gt;&lt;P&gt;;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc SQL;&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;substr(a, 1, 6) as a,&lt;BR /&gt;substr(b, 1, 6) as b,&lt;BR /&gt;count(*) as n&lt;BR /&gt;from test&lt;BR /&gt;group by&lt;BR /&gt;substr(a, 1, 6),&lt;BR /&gt;substr(b, 1, 6)&lt;/P&gt;&lt;P&gt;;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;result:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;a b n &lt;TABLE cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;XX_ABC&lt;/TD&gt;&lt;TD&gt;ABCDXX&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;XX_ABC&lt;/TD&gt;&lt;TD&gt;ABCDXX&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;XY_DCY&lt;/TD&gt;&lt;TD&gt;ABCDXX&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SQL SERVER (See SQL fiddle for working example)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;create table test (&lt;BR /&gt;a varchar(8),&lt;BR /&gt;b varchar(8));&lt;BR /&gt;&lt;BR /&gt;insert into test (a, b) values ('XX_ABC01', 'ABCDXX01');&lt;BR /&gt;insert into test (a, b) values ('XX_ABC01', 'ABCDXX01');&lt;BR /&gt;insert into test (a, b) values ('XX_ABC02', 'ABCDXX02');&lt;BR /&gt;insert into test (a, b) values ('XY_DCY02', 'ABCDXX02');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select&lt;BR /&gt;substring(a, 1, 6) as a,&lt;BR /&gt;substring(b, 1, 6) as b,&lt;BR /&gt;count(*) as n&lt;BR /&gt;from test&lt;BR /&gt;group by&lt;BR /&gt;substring(a, 1, 6),&lt;BR /&gt;substring(b, 1, 6) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RESULT (correct in my opinion)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;XX_ABCABCDXX3a b n &lt;TABLE cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;XX_ABC&lt;/TD&gt;&lt;TD&gt;ABCDXX&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;XY_DCY&lt;/TD&gt;&lt;TD&gt;ABCDXX&lt;/TD&gt;&lt;TD&gt;&lt;BR /&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 09:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490005#M128082</guid>
      <dc:creator>Jeg123</dc:creator>
      <dc:date>2018-08-27T09:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS proc sql giving wrong results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490008#M128085</link>
      <description>&lt;P&gt;Since substr(a,1,6) (used in the group by) is a different object than new variable a from the select, SAS sees the need to remerge the summary statistics (see the NOTE in the log).&lt;/P&gt;
&lt;P&gt;Use this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc SQL;
create table test (
  a char(8),
  b char(8)
);
insert into test (a, b) values ('XX_ABC01', 'ABCDXX01');
insert into test (a, b) values ('XX_ABC01', 'ABCDXX01');
insert into test (a, b) values ('XX_ABC02', 'ABCDXX02');
insert into test (a, b) values ('XY_DCY02', 'ABCDXX02');
quit;

proc SQL;
select
  substr(a, 1, 6) as a,
  substr(b, 1, 6) as b,
  count(*) as n
from test
group by calculated a, calculated b
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that SAS has no datatype varchar, only char.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 09:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490008#M128085</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-27T09:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAS proc sql giving wrong results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490395#M128292</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;GROUP BY 1,2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;works too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; It is the same object though. SAS is wrong here no?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 10:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490395#M128292</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-28T10:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS proc sql giving wrong results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490397#M128294</link>
      <description>&lt;P&gt;Logically, it's the same, but technically it's just the result of a function (in the group by of the OP). 1,2 adresses the newly created variables, not the expressions, and is therefore equivalent to my usage of the &lt;EM&gt;calculated&lt;/EM&gt; variables.&lt;/P&gt;
&lt;P&gt;The SQL compiler is obviously not intelligent enough to check if the expressions in the group by are the same as those in the select. Maxim 31 in action.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 10:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490397#M128294</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-28T10:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS proc sql giving wrong results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490400#M128296</link>
      <description>&lt;P&gt;LOL #31&lt;/P&gt;
&lt;P&gt;All other SQL parsers manage the feat, so while maybe not a defect per se (and while I much prefer the calculated or 1,2 syntaxes anyway) I'd suggest this is at least a gap or a weakness.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 10:19:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-proc-sql-giving-wrong-results/m-p/490400#M128296</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-28T10:19:13Z</dc:date>
    </item>
  </channel>
</rss>

