<?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 Migrating Proc SQL code with calculated column to Proc FedSQL in SAS Viya</title>
    <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663474#M613</link>
    <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is partially off-topic since Proc FedSQL goes geyond Viya,&lt;/P&gt;
&lt;P&gt;however my question might be especially relevant here since&lt;/P&gt;
&lt;P&gt;Proc SQL is not CAS-enabled (or, ist it ? in preproduction seems to work fine &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; ) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Proc SQL with calculated column */

Proc SQL;
select t.*, scan(t.email,-1) as MAILTLD
from mytable as t
where t.flag = 'SPAM' and calculated MAILTLD eq 'com'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How do we refactor this code on Viya using Proc FedSQL ANSI syntax ? I was thinking of correlated subqueries, something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Proc FedSQL correlated subquery - roughly sketched*/

Proc FedSQL sessref=mycas;
select t.*
from caslib."mytable" as t
where t.flag = 'SPAM' and 'com' = ( select scan( v.email , -1) 
                                    from caslib."mytable" as v
                                    where v.email = t.email )
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, then I read in Proc FedSQL documentation :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="xisDoc-noteGenText"&gt;Note: &lt;/SPAN&gt;Correlated subqueries are not yet supported on the CAS server.&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=p0d70hfuuzsq0tn1ndl5k4x9m56z.htm&amp;amp;locale=en" target="_self"&gt;https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=p0d70hfuuzsq0tn1ndl5k4x9m56z.htm&amp;amp;locale=en&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What option do we still have ? Creating a second "outer" result set then merging with the "inner" query&amp;nbsp; ?&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jun 2020 13:22:05 GMT</pubDate>
    <dc:creator>ronan</dc:creator>
    <dc:date>2020-06-19T13:22:05Z</dc:date>
    <item>
      <title>Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663474#M613</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is partially off-topic since Proc FedSQL goes geyond Viya,&lt;/P&gt;
&lt;P&gt;however my question might be especially relevant here since&lt;/P&gt;
&lt;P&gt;Proc SQL is not CAS-enabled (or, ist it ? in preproduction seems to work fine &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; ) :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Proc SQL with calculated column */

Proc SQL;
select t.*, scan(t.email,-1) as MAILTLD
from mytable as t
where t.flag = 'SPAM' and calculated MAILTLD eq 'com'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How do we refactor this code on Viya using Proc FedSQL ANSI syntax ? I was thinking of correlated subqueries, something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Proc FedSQL correlated subquery - roughly sketched*/

Proc FedSQL sessref=mycas;
select t.*
from caslib."mytable" as t
where t.flag = 'SPAM' and 'com' = ( select scan( v.email , -1) 
                                    from caslib."mytable" as v
                                    where v.email = t.email )
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, then I read in Proc FedSQL documentation :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="xisDoc-noteGenText"&gt;Note: &lt;/SPAN&gt;Correlated subqueries are not yet supported on the CAS server.&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=p0d70hfuuzsq0tn1ndl5k4x9m56z.htm&amp;amp;locale=en" target="_self"&gt;https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=proc&amp;amp;docsetTarget=p0d70hfuuzsq0tn1ndl5k4x9m56z.htm&amp;amp;locale=en&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What option do we still have ? Creating a second "outer" result set then merging with the "inner" query&amp;nbsp; ?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 13:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663474#M613</guid>
      <dc:creator>ronan</dc:creator>
      <dc:date>2020-06-19T13:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663490#M614</link>
      <description>&lt;P&gt;Repeat your expression from the select for MAILTLD in the where and it should work&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 13:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663490#M614</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2020-06-19T13:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663494#M615</link>
      <description>&lt;P&gt;Thanks for your reply, I gave this simple code as an example. Of course we can apply the SCAN function straight in the WHERE clause (If I follow you correctly) .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then how can we proceed when the calculated column is a more complex transformation, for instance coming from a CASE manipulation of values ? The following code is quite arbitrary, this is just an example :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQL;
select t.*, CASE WHEN scan(t.email,-1) EQ 'com' THEN 'COMMERCIAL' ELSE 'NON COMMERCIAL' as FLAGCOM
from mytable
where t.FLAG = 'SPAM' AND calculated FLAGCOM = 'COMMERCIAL'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 14:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663494#M615</guid>
      <dc:creator>ronan</dc:creator>
      <dc:date>2020-06-19T14:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663533#M616</link>
      <description>&lt;P&gt;If you have several long expressions, think about creating an inline view, the following example shows it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data cars;&lt;BR /&gt;set sashelp.cars;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc delete data=mycars;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc fedsql ;&lt;BR /&gt;create table mycars as&lt;BR /&gt;select&lt;BR /&gt;origin&lt;BR /&gt;, priceType&lt;BR /&gt;, avg(invoice) as avg_invoice&lt;BR /&gt;from (&lt;BR /&gt;select &lt;BR /&gt;*&lt;BR /&gt;, case&lt;BR /&gt;when invoice &amp;gt;= 0 and invoice &amp;lt; 30000 then 'affordable'&lt;BR /&gt;when invoice &amp;gt;= 30000 and invoice &amp;lt; 60000 then 'need to think'&lt;BR /&gt;else 'not my budget'&lt;BR /&gt;end as priceType&lt;BR /&gt;from&lt;BR /&gt;cars&lt;BR /&gt;) as c1&lt;BR /&gt;group by &lt;BR /&gt;origin&lt;BR /&gt;, priceType&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;proc print data=mycars;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 16:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663533#M616</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2020-06-19T16:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663555#M617</link>
      <description>&lt;P&gt;Thanks for your code; however the code I have to migrate requires to filter the rows with a calculated column. The requirement was not clearly expressed, my fault. I ran your code in a SAS 9 session, adding only a WHERE clause before the GROUP BY, like this :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fedsql ;
create table mycars as
select origin, priceType, avg(invoice) as avg_invoice
from (
select  *, case when invoice &amp;gt;= 0 and invoice &amp;lt; 30000 then 'affordable'
  		when invoice &amp;gt;= 30000 and invoice &amp;lt; 60000 then 'need to think'
 		else 'not my budget'
   		end as priceType
from cars
) as c1
&lt;STRONG&gt;where c1.priceType eq 'affordable'&lt;/STRONG&gt;
group by  origin, priceType

;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Proc SQL accepts this kind of syntax, Proc FedSQL does not unfortunately (on SAS 9 at least):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; ERROR: Syntax error at or near "EQ"
 NOTE: PROC FEDSQL has set option NOEXEC and will continue to prepare statements.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 17:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663555#M617</guid>
      <dc:creator>ronan</dc:creator>
      <dc:date>2020-06-19T17:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663651#M618</link>
      <description>&lt;P&gt;Just replace EQ with = and the syntax becomes valid.&lt;/P&gt;
&lt;P&gt;Alternatively consider option2 and filter the data directly when reading from source, then create the calculated column in the Select clause and Group by this calculated column.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
  set sashelp.cars;
run;

proc datasets lib=work nolist nowarn;
  delete mycars mycars2;
  run;
quit;

/* option 1 */
proc fedsql;
/*  create table mycars as*/
    select origin, c1.priceType, avg(invoice) as avg_invoice
    from 
      (
        select  *, case when invoice &amp;gt;= 0 and invoice &amp;lt; 30000 then 'affordable'
          when invoice &amp;gt;= 30000 and invoice &amp;lt; 60000 then 'need to think'
          else 'not my budget'
          end as priceType
        from cars
      ) as c1
    where priceType = 'affordable'
    group by  origin, priceType
  ;
quit;

/* option 2 */
proc fedsql;
/*  create table mycars2 as*/
    select 
      origin, 
      case 
        when invoice &amp;gt;= 0 and invoice &amp;lt; 30000 then 'affordable'
        when invoice &amp;gt;= 30000 and invoice &amp;lt; 60000 then 'need to think'
        else 'not my budget'
        end as priceType,
      avg(invoice) as avg_invoice
    from cars
    where invoice &amp;gt;= 0 and invoice &amp;lt; 30000
    group by  origin, priceType
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1592609825474.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46404iB9843736021F524C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1592609825474.png" alt="Patrick_0-1592609825474.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jun 2020 23:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663651#M618</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-19T23:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: Migrating Proc SQL code with calculated column to Proc FedSQL</title>
      <link>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663936#M619</link>
      <description>&lt;P&gt;Thank you both for your help &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;, with the correct operator, FedSQL runs OK on SAS 9 and CAS as well.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jun 2020 09:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya/Migrating-Proc-SQL-code-with-calculated-column-to-Proc-FedSQL/m-p/663936#M619</guid>
      <dc:creator>ronan</dc:creator>
      <dc:date>2020-06-22T09:34:38Z</dc:date>
    </item>
  </channel>
</rss>

