<?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 - outermost query can see fields through nested querys in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477815#M286170</link>
    <description>&lt;P&gt;Thanks Quentin, glad it's not just me. I've submitted a ticket and will update this thread with their response.&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jul 2018 09:15:29 GMT</pubDate>
    <dc:creator>EvoluZion3</dc:creator>
    <dc:date>2018-07-13T09:15:29Z</dc:date>
    <item>
      <title>PROC SQL - outermost query can see fields through nested querys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477014#M286168</link>
      <description>&lt;P&gt;Hi all, we have come across this interesting feature in PROC SQL and wondered what your thoughts were. We're currently using EG5.1 and SAS9.3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We are finding that an outer query can see fieldnames from deep inside nested queries, whereas a SELECT should only be able to see the results immediately inside the FROM. For all other SQL platforms I've used (MS SQL, Oracle, Access) this shouldn't be allowed, indeed they error when I replicate the same problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To see what I mean, please run the below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example 1A works (or rather errors) as I'd expect, however example 1B shows that just by aliasing the example table's fieldname (even using the same name) the outer query can see "through" the middle query into the inner query. I've tried this with a query nested four times with the same result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* Build an example table */

data WORK.MYDATA;
input MYID;
datalines; 
1
2
3
;




/* Example 1A - this gives a "column not found" error as expected, as the outermost SELECT cannot see "MYID", only "NEWFIELDNAME777" */
PROC SQL;
SELECT MYID FROM (
SELECT MYID AS NEWFIELDNAME777 FROM (
SELECT MYID FROM WORK.MYDATA
)
);
QUIT;




/* Example 1B - just by aliasing the fieldname in the innermost query, this now returns records */
PROC SQL;
SELECT MYID FROM (
SELECT MYID AS NEWFIELDNAME777 FROM (
SELECT MYID AS MYID FROM WORK.MYDATA
)
);
QUIT;




/* Example 2A - if the middle query aggregates the data, the aggregate is returned alongside each record (the outer query can see both the middle query and the innermost query) */
PROC SQL;
SELECT MYID, NEWFIELDNAME777 FROM (
SELECT SUM(MYID) AS NEWFIELDNAME777 FROM (
SELECT MYID AS MYID FROM WORK.MYDATA
)
);
QUIT;




/* Example 2B - if the middle query aggregates the data, but I use UNION to replicate the table, only the last record is returned, alongside the aggregate. This should return the same results as 2A */
PROC SQL;
SELECT MYID, NEWFIELDNAME777 FROM (
SELECT SUM(MYID) AS NEWFIELDNAME777 FROM (
SELECT 1 AS MYID FROM WORK.MYDATA WHERE MYID = 1 UNION
SELECT 2 AS MYID FROM WORK.MYDATA WHERE MYID = 2 UNION
SELECT 3 AS MYID FROM WORK.MYDATA WHERE MYID = 3
)
);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Jul 2018 09:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477014#M286168</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-07-11T09:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - outermost query can see fields through nested querys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477259#M286169</link>
      <description>&lt;P&gt;Well that's surprising to me.&amp;nbsp; Seems like a bug.&amp;nbsp; You don't even need the query in the middle to reference MYID.&amp;nbsp; If you use the _tree option, you can see that the _tree that is shown when the problem occurs, is almost the same as if you just did select * from mydata.&amp;nbsp; It's as if the compiler is completely skipping the the middle query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd suggest sending it in to tech support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data mydata ;
  myid=1 ;
run ;

*errors as expected ;
proc sql _tree ;
  select myid from
    (select 0 as foo from
      (select myid from mydata)
     )
 ;
quit ;

*surprising bug (?). Should error, but returns myid from inner most query ; 
*Note the short tree ;
proc sql _tree ;
  select myid from
    (select 0 as foo from
      (select myid as myid from mydata)
     )
 ;
quit ;

*works as expected ;
proc sql _tree ;
  select * from
    (select 0 as foo from
      (select myid from mydata)
     )
 ;
quit ;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jul 2018 21:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477259#M286169</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-07-11T21:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - outermost query can see fields through nested querys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477815#M286170</link>
      <description>&lt;P&gt;Thanks Quentin, glad it's not just me. I've submitted a ticket and will update this thread with their response.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 09:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/477815#M286170</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-07-13T09:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - outermost query can see fields through nested querys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/478578#M286171</link>
      <description>&lt;P&gt;I received this email from SAS Technical Support:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;Thank you for your email regarding the nested SQL select queries.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;I've just been running the code and I see what you mean.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;I can't find any previous mention of this in our knowledge base or Defect system, so what I will do is raise this with my Global Support team as a candidate for a Defect.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="cs2654ae3a"&gt;&lt;SPAN class="cs9867eb741"&gt;&lt;SPAN style="font-size: 8.0pt;"&gt;I'll be in touch as soon as I have more information for you .&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jul 2018 08:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/478578#M286171</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-07-17T08:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - outermost query can see fields through nested querys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/481551#M286172</link>
      <description>&lt;P&gt;This has been raised by SAS as a software fault, and will be fixed in the future. I'm surprised no-one else has hit this problem before.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 15:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-outermost-query-can-see-fields-through-nested-querys/m-p/481551#M286172</guid>
      <dc:creator>EvoluZion3</dc:creator>
      <dc:date>2018-07-26T15:16:16Z</dc:date>
    </item>
  </channel>
</rss>

