<?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: SQL Syntax error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623360#M183511</link>
    <description>&lt;P&gt;The DISTINCT keyword is for selecting distinct observations.&amp;nbsp; Wrapping those parentheses around the first variable in the select list does nothing, other than confuse people.&amp;nbsp; Just as 4+5 is the same as (4+5) so is&amp;nbsp;(Reporting_INST_NAME) the sames as&amp;nbsp;Reporting_INST_NAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first condition in your HAVING clause does nothing. Since you are grouping on&amp;nbsp;Reporting_INST_NAME there is only one value of&amp;nbsp;Reporting_INST_NAME in each group so that value is also the MAX() and the MIN() value in the group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The effect of the other conditions is that only rows that have the max of &lt;STRONG&gt;ALL&lt;/STRONG&gt; 4 of those variables (within the group of observations with this value of&amp;nbsp;Reporting_INST_NAME) will be selected.&amp;nbsp; So you migth not get any observations for some values of&amp;nbsp;Reporting_INST_NAME.&amp;nbsp; Consider the case when there are exactly four observations where&amp;nbsp;Reporting_INST_NAME='AA' and it so happens that the first one has the maximum values of&amp;nbsp;Reporting_INST_AT among the four and the second has the maximum value of&amp;nbsp;Reporting_INST_AM , etc. Then none of the four has the maximum value of all of them.&amp;nbsp; Or you might get more than one observation in the group if there are more than one observations that does have the max for all four being tested, but the values of the other variables being kept are different so the DISTINCT operation does not weed them out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it that you WANT to do?&lt;/P&gt;</description>
    <pubDate>Sun, 09 Feb 2020 03:07:16 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-02-09T03:07:16Z</dc:date>
    <item>
      <title>SQL Syntax error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623288#M183470</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Would appreciate if someone can advise the cause of the error code pasted below as well&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;222 create table Insttable_without_DupKey as&lt;BR /&gt;223 select DISTINCT(Reporting_INST_NAME), Reporting_INST_AT, Reporting_INST_AM, Reporting_FAC_NUM,FAC_NUM,INST_NAME2,INST_NAME,INST_AM, INST_AT,&lt;BR /&gt;224 Strokecentre2,Strokeunit2,TPA_IS,EVT1718_IS&lt;BR /&gt;225 group by Reporting_INST_NAME&lt;BR /&gt;-----&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &amp;amp;, (, *, **, +, ',', -, '.', /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND, AS, BETWEEN, CONTAINS,&lt;BR /&gt;EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=,&lt;BR /&gt;|, ||, ~, ~=.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;226 having (Reporting_INST_NAME) = MAX(Reporting_INST_NAME)&lt;BR /&gt;227 AND Reporting_INST_AT = MAX(Reporting_INST_AT)&lt;BR /&gt;228 AND Reporting_INST_AM = MAX(Reporting_INST_AM)&lt;BR /&gt;229 AND Strokeunit2 = MAX(Strokeunit2)&lt;BR /&gt;230 ;&lt;BR /&gt;231 quit;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
 create table Insttable_without_DupKey as
 select DISTINCT(Reporting_INST_NAME), Reporting_INST_AT, Reporting_INST_AM, Reporting_FAC_NUM,FAC_NUM,INST_NAME2,INST_NAME,INST_AM, INST_AT,
 Strokecentre2,Strokeunit2,TPA_IS,EVT1718_IS
 group by Reporting_INST_NAME
 having (Reporting_INST_NAME) = MAX(Reporting_INST_NAME)
 AND Reporting_INST_AT = MAX(Reporting_INST_AT)
 AND Reporting_INST_AM = MAX(Reporting_INST_AM)
 AND Strokeunit2 = MAX(Strokeunit2)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Feb 2020 09:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623288#M183470</guid>
      <dc:creator>Ranjeeta</dc:creator>
      <dc:date>2020-02-08T09:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Syntax error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623293#M183472</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240770"&gt;@Ranjeeta&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that the "FROM" clause indicating the input dataset is missing before the "GROUP BY" clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sat, 08 Feb 2020 10:44:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623293#M183472</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-08T10:44:37Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Syntax error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623344#M183505</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
 create table Insttable_without_DupKey1 as
 select DISTINCT(Reporting_INST_NAME), Reporting_INST_AT, Reporting_INST_AM, Reporting_FAC_NUM,FAC_NUM,INST_NAME2,INST_NAME,INST_AM, INST_AT,
 Strokecentre2,Strokeunit2,TPA_IS,EVT1718_IS,StrokeUnit_FYEAR
 from insttable
 group by Reporting_INST_NAME
 having (Reporting_INST_NAME) = MAX(Reporting_INST_NAME)
 AND Reporting_INST_AT = MAX(Reporting_INST_AT)
 AND Reporting_INST_AM = MAX(Reporting_INST_AM)
 AND Strokeunit2 = MAX(Strokeunit2)
 and StrokeUnit_FYEAR =max(StrokeUnit_FYEAR)
;
quit;/*256*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thankyou&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate if someone could explain how the query above works?&lt;/P&gt;&lt;P&gt;Would the select distinct select unique rows for combination of all the columns in the select statement or only Reporting_INST_NAME?&lt;/P&gt;&lt;P&gt;Also the having clause works on the data grouped by ie Reporting_INST_NAME does that mean that if two same ReportingINST_NAMES exist then the row with the higher value of the variables in the having clause would be selected ?&lt;/P&gt;&lt;P&gt;Please advise how the query works?&lt;/P&gt;</description>
      <pubDate>Sat, 08 Feb 2020 23:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623344#M183505</guid>
      <dc:creator>Ranjeeta</dc:creator>
      <dc:date>2020-02-08T23:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Syntax error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623360#M183511</link>
      <description>&lt;P&gt;The DISTINCT keyword is for selecting distinct observations.&amp;nbsp; Wrapping those parentheses around the first variable in the select list does nothing, other than confuse people.&amp;nbsp; Just as 4+5 is the same as (4+5) so is&amp;nbsp;(Reporting_INST_NAME) the sames as&amp;nbsp;Reporting_INST_NAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first condition in your HAVING clause does nothing. Since you are grouping on&amp;nbsp;Reporting_INST_NAME there is only one value of&amp;nbsp;Reporting_INST_NAME in each group so that value is also the MAX() and the MIN() value in the group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The effect of the other conditions is that only rows that have the max of &lt;STRONG&gt;ALL&lt;/STRONG&gt; 4 of those variables (within the group of observations with this value of&amp;nbsp;Reporting_INST_NAME) will be selected.&amp;nbsp; So you migth not get any observations for some values of&amp;nbsp;Reporting_INST_NAME.&amp;nbsp; Consider the case when there are exactly four observations where&amp;nbsp;Reporting_INST_NAME='AA' and it so happens that the first one has the maximum values of&amp;nbsp;Reporting_INST_AT among the four and the second has the maximum value of&amp;nbsp;Reporting_INST_AM , etc. Then none of the four has the maximum value of all of them.&amp;nbsp; Or you might get more than one observation in the group if there are more than one observations that does have the max for all four being tested, but the values of the other variables being kept are different so the DISTINCT operation does not weed them out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it that you WANT to do?&lt;/P&gt;</description>
      <pubDate>Sun, 09 Feb 2020 03:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623360#M183511</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-09T03:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Syntax error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623402#M183534</link>
      <description>&lt;P&gt;I have some duplicates in the data and i want to remove the record that has blank for the variables in the Having clause&lt;/P&gt;&lt;P&gt;I want to do this for every unique combination of Reporting_INST_NAME&amp;nbsp;Reporting_INST_AT Reporting_INST_AM FAC_NUM&lt;/P&gt;&lt;P&gt;I think the code below is achieving the same?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
 create table Insttable_without_DupKey as
 select DISTINCT Reporting_INST_NAME, Reporting_INST_AT, Reporting_INST_AM, Reporting_FAC_NUM,FAC_NUM,INST_NAME2,INST_NAME,INST_AM, INST_AT,
 Strokecentre2,Strokeunit2,TPA_IS,EVT1718_IS,StrokeUnit_FYEAR
 from insttable
 group by Reporting_INST_NAME, Reporting_INST_AT, Reporting_INST_AM, FAC_NUM
 having Strokeunit2 = MAX(Strokeunit2)
 and StrokeUnit_FYEAR = max(StrokeUnit_FYEAR)
;
quit;/*259*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Feb 2020 12:21:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Syntax-error/m-p/623402#M183534</guid>
      <dc:creator>Ranjeeta</dc:creator>
      <dc:date>2020-02-09T12:21:34Z</dc:date>
    </item>
  </channel>
</rss>

