<?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 giving result when expected not to in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723822#M224684</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Can you tell us more how a correlated subquery can replace and swap variable names, and the the name from the outer query and replace it in the subquery?&lt;/P&gt;
&lt;P&gt;Any other cases where this name-swapping takes place?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think anything special is happening here. It's just that variables from &lt;EM&gt;all&lt;/EM&gt;&amp;nbsp;contributing tables are valid in an sql-expression in a subquery (cf. the wording of the error message obtained with &lt;FONT face="courier new,courier"&gt;sex&lt;STRONG&gt;&lt;EM&gt;3&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;). The expression &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt; in the subquery does &lt;U&gt;&lt;EM&gt;not&lt;/EM&gt;&lt;/U&gt; select values from variable &lt;FONT face="courier new,courier"&gt;sex&lt;/FONT&gt; in SASHELP.CLASS&amp;nbsp;(using a "wrong" or "swapped" name). Rather it's a &lt;EM&gt;constant&lt;/EM&gt; (like the &lt;FONT face="courier new,courier"&gt;'M'&lt;/FONT&gt; in my earlier example) w.r.t. the subquery for each record from WORK.CLASS. As a consequence, the subquery results in a temporary table consisting of as many identical values (equal to the current value of &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt;&amp;nbsp;from WORK.CLASS) as there are observations in SASHELP.CLASS, i.e. 19. Trivially, the resulting WHERE condition is always met, unless the table in the FROM clause of the subquery was empty. So, &lt;EM&gt;any non-empty&lt;/EM&gt; dataset, even if totally unrelated to WORK.CLASS in terms of variable names, types and values, could be used as well in place of SASHELP.CLASS in the subquery.&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 09:35:44 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-03-05T09:35:44Z</dc:date>
    <item>
      <title>Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723508#M224524</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;When I run the below proc sql code it returns results and I did expect it to give an error. Can anyone explain why?&lt;/P&gt;&lt;P&gt;Running SAS 9.4 on Unix. I expected it to fail since variable sex2 is not in sashelp.class but instead work.class with 10 obs was returned. Have entered my log notes as well as the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;*-- Creating subset of data renaming variable sex to sex2 --*;
data work.class(rename=(sex=sex2));
  set sashelp.class(where=(sex='M'));
run;

NOTE: There were 10 observations read from the data set SASHELP.CLASS.
      WHERE sex='M';
NOTE: The data set WORK.CLASS has 10 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds 

*-- Selecting from subset using sex2 in (select sex2) --*;
*-- Expected this to fail instead class2 is returned  --*;
*-- as result                                         --*;
proc sql;
  create table class2 as
  select * from class
  where sex2 in(select sex2 from sashelp.class); /* sex2 is not in sashelp.class */
quit;&lt;BR /&gt;NOTE: Table WORK.CLASS2 created, with 10 rows and 5 columns. 
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Mar 2021 16:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723508#M224524</guid>
      <dc:creator>Ukar441</dc:creator>
      <dc:date>2021-03-04T16:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723643#M224584</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/372336"&gt;@Ukar441&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You created a &lt;EM&gt;correlated&lt;/EM&gt; subquery, i.e., the sql-expression &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt; in the subquery is recognized as that variable from WORK.CLASS in the outer query. As such it is syntactically correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Equivalently, you could have written:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table class2 as
  select * from class &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;a&lt;/STRONG&gt;&lt;/FONT&gt;
  where sex2 in (select &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;a.&lt;/STRONG&gt;&lt;/FONT&gt;sex2 from sashelp.class);
quit;&lt;/PRE&gt;
&lt;P&gt;or (since &lt;FONT face="courier new,courier"&gt;sex2='M'&lt;/FONT&gt; for all observations in WORK.CLASS)&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table class2 as
  select * from class
  where sex2 in (select &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;'M'&lt;/STRONG&gt;&lt;/FONT&gt; from sashelp.class);
quit;&lt;/PRE&gt;
&lt;P&gt;Like variable&amp;nbsp;&lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt; (but unlike, say, &lt;FONT face="courier new,courier"&gt;sex&lt;STRONG&gt;&lt;EM&gt;3&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;, which &lt;EM&gt;would&lt;/EM&gt; cause an error) the character constant&amp;nbsp;&lt;FONT face="courier new,courier"&gt;'M'&lt;/FONT&gt;&amp;nbsp;(or any other character constant for that matter) is a valid sql-expression which can be compared to values of character variable &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt; from the outer query.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Mar 2021 21:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723643#M224584</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-04T21:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723734#M224643</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Can you tell us more how a correlated subquery can replace and swap variable names, and the the name from the outer query and replace it in the subquery?&lt;/P&gt;
&lt;P&gt;Any other cases where this name-swapping takes place?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 00:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723734#M224643</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-05T00:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723817#M224681</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your answer. It was really helpful and explained it well. Following your reasoning gives the following conclusion. If we change the where clause somewhat I get these results.&lt;/P&gt;&lt;PRE&gt;where sex2 in(select sex2 from sashelp.class);
NOTE: Table WORK.CLASS2 created, with 10 rows and 5 columns.

where sex2 in(select 'F' from sashelp.class);
where sex2 in(select 'Hello' from sashelp.class);
where sex2 in(select name from sashelp.class);
NOTE: Table WORK.CLASS2 created, with 0 rows and 5 columns.

where sex2 in(select a.sex2 from sashelp.class as a);
ERROR: Column sex2 could not be found in the table/view identified with the correlation name A.
ERROR: Unresolved reference to table/correlation name a.&lt;/PRE&gt;&lt;P&gt;First example is the original and there sex2 is correlated with sex2 in work.class.&lt;/P&gt;&lt;P&gt;Next three examples evaluates the character expression and gives expected = rows.&lt;/P&gt;&lt;P&gt;Last example when I explicitly point to sashelp.class with a.sex2 I get the error I expected in the first place.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However what was new to me was that in example 1,2 and 3 the "from sashelp.class" in a sense is ignored. Example 1 the select uses sex2 from another table and in 2 and 3 from sashelp.class is sort of ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is not quite a variable swap as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;suggest but more of an order for searching.&lt;/P&gt;&lt;P&gt;Totally new to me and makes me wonder how many bugs I've created over the years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 09:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723817#M224681</guid>
      <dc:creator>Ukar441</dc:creator>
      <dc:date>2021-03-05T09:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723822#M224684</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Can you tell us more how a correlated subquery can replace and swap variable names, and the the name from the outer query and replace it in the subquery?&lt;/P&gt;
&lt;P&gt;Any other cases where this name-swapping takes place?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think anything special is happening here. It's just that variables from &lt;EM&gt;all&lt;/EM&gt;&amp;nbsp;contributing tables are valid in an sql-expression in a subquery (cf. the wording of the error message obtained with &lt;FONT face="courier new,courier"&gt;sex&lt;STRONG&gt;&lt;EM&gt;3&lt;/EM&gt;&lt;/STRONG&gt;&lt;/FONT&gt;). The expression &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt; in the subquery does &lt;U&gt;&lt;EM&gt;not&lt;/EM&gt;&lt;/U&gt; select values from variable &lt;FONT face="courier new,courier"&gt;sex&lt;/FONT&gt; in SASHELP.CLASS&amp;nbsp;(using a "wrong" or "swapped" name). Rather it's a &lt;EM&gt;constant&lt;/EM&gt; (like the &lt;FONT face="courier new,courier"&gt;'M'&lt;/FONT&gt; in my earlier example) w.r.t. the subquery for each record from WORK.CLASS. As a consequence, the subquery results in a temporary table consisting of as many identical values (equal to the current value of &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt;&amp;nbsp;from WORK.CLASS) as there are observations in SASHELP.CLASS, i.e. 19. Trivially, the resulting WHERE condition is always met, unless the table in the FROM clause of the subquery was empty. So, &lt;EM&gt;any non-empty&lt;/EM&gt; dataset, even if totally unrelated to WORK.CLASS in terms of variable names, types and values, could be used as well in place of SASHELP.CLASS in the subquery.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 09:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723822#M224684</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-05T09:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723827#M224688</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/372336"&gt;@Ukar441&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your answer. (...)&lt;/P&gt;
&lt;P&gt;However what was new to me was that in example 1,2 and 3 the "from sashelp.class" in a sense is ignored. (...)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You're welcome. In these examples the only impact of "&lt;FONT face="courier new,courier"&gt;sashelp.class&lt;/FONT&gt;" is the number of (identical) observations resulting from the subquery (i.e. 19). Interestingly, PROC SQL seems to be clever enough not to waste resources to create a huge trivial table if that number is large. [Edit: The run time does not depend on that number.] It uses the same methods whether a constant (like &lt;FONT face="courier new,courier"&gt;'M&lt;/FONT&gt;') is selected or a variable from the outer query like &lt;FONT face="courier new,courier"&gt;sex2&lt;/FONT&gt;, but the set of methods is different in the situation where a variable like &lt;FONT face="courier new,courier"&gt;name&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;sex&lt;/FONT&gt; is selected in the subquery. This can be seen by using the undocumented option &lt;FONT face="courier new,courier"&gt;_method&lt;/FONT&gt; of the PROC SQL statement:&lt;/P&gt;
&lt;PRE&gt;130  proc sql _method;
131    create table class2 as
132    select * from class
133    where sex2 in (select sex from sashelp.class);

NOTE: SQL execution methods chosen are:

      sqxcrta
          sqxfil
              sqxsrc( WORK.CLASS )

NOTE: SQL subquery execution methods chosen are:

&lt;FONT color="#3366FF"&gt;          sqxsubq
              sqxsrc( SASHELP.CLASS )
&lt;/FONT&gt;NOTE: Table WORK.CLASS2 created, with 10 rows and 5 columns.

134  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


135
136  proc sql _method;
137    create table class2 as
138    select * from class
139    where sex2 in (select sex2 from sashelp.class);

NOTE: SQL execution methods chosen are:

      sqxcrta
          sqxfil
              sqxsrc( WORK.CLASS )

NOTE: SQL subquery execution methods chosen are:

        &lt;FONT color="#3366FF"&gt;  sqxsubq&lt;/FONT&gt;
           &lt;FONT color="#FF0000"&gt;   sqxreps&lt;/FONT&gt;
                  &lt;FONT color="#3366FF"&gt;sqxsrc( SASHELP.CLASS )&lt;/FONT&gt;
NOTE: Table WORK.CLASS2 created, with 10 rows and 5 columns.

140  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


141
142  proc sql _method;
143    create table class2 as
144    select * from class
145    where sex2 in (select 'X' from sashelp.class);

NOTE: SQL execution methods chosen are:

      sqxcrta
          sqxfil
              sqxsrc( WORK.CLASS )

NOTE: SQL subquery execution methods chosen are:

      &lt;FONT color="#3366FF"&gt;    sqxsubq&lt;/FONT&gt;
    &lt;FONT color="#FF0000"&gt;          sqxreps&lt;/FONT&gt;
                 &lt;FONT color="#3366FF"&gt; sqxsrc( SASHELP.CLASS )&lt;/FONT&gt;
NOTE: Table WORK.CLASS2 created, with 0 rows and 5 columns.&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 10:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723827#M224688</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-05T10:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql giving result when expected not to</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723838#M224692</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I-m not the first one puzzled by this. Looked at your answer and googled the method sqxreps and one of the first links was this one&amp;nbsp;&lt;A href="https://stackoverflow.com/questions/34185428/proc-sql-subquery-based-on-nonexisitng-column-returns-not-null" target="_blank"&gt;sas - Proc sql subquery based on nonexisitng column returns not null - Stack Overflow&lt;/A&gt;&amp;nbsp;. A discussion on the same topic. Right or wrong can probably be discussed. My own takeaway will be that it might be a good idea to be more strict in referencing tables/views i.e to use tablename.variablename notation in the sql queries.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 11:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-giving-result-when-expected-not-to/m-p/723838#M224692</guid>
      <dc:creator>Ukar441</dc:creator>
      <dc:date>2021-03-05T11:03:23Z</dc:date>
    </item>
  </channel>
</rss>

