<?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: Dynamic SQL code storing in macro variable and call to Execute proc sql; in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722756#M27919</link>
    <description>&lt;P&gt;I tried with Simple code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;Proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;connect&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;Execute&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;"DECLARE @Query NVARCHAR(MAX) = '';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Set @Query='select distinct qrm_db from [FASTControl].[dbo].ALM_NVPData'&lt;/P&gt;&lt;P&gt;PRINT @Query&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;EXECUTE sp_executesql @Query"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Log says &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;/P&gt;&lt;P&gt;31 connect to odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);&lt;/P&gt;&lt;P&gt;32 Execute ("DECLARE @Query NVARCHAR(MAX) = '';&lt;/P&gt;&lt;P&gt;33 Set @Query='select distinct qrm_db from [FASTControl].[dbo].ALM_NVPData'&lt;/P&gt;&lt;P&gt;34 PRINT @Query&lt;/P&gt;&lt;P&gt;35 EXECUTE sp_executesql @Query") by odbc;&lt;/P&gt;&lt;P&gt;36 quit;&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.17 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.04 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't see any output is this SQL executes?&lt;/P&gt;</description>
    <pubDate>Tue, 02 Mar 2021 01:29:41 GMT</pubDate>
    <dc:creator>Ashpak</dc:creator>
    <dc:date>2021-03-02T01:29:41Z</dc:date>
    <item>
      <title>Dynamic SQL code storing in macro variable and call to Execute proc sql;</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722743#M27915</link>
      <description>&lt;P&gt;Hi I have Dynamic SQL code written in PROC sql procedure , I am forming Dynamic sql and storing&amp;nbsp; sQL code into variable and calling that variable&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Effdt = '';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Query = '';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;Proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;connect&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; * &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; connection to odbc&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;SELECT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; @Effdt += + QUOTENAME(EFF_DT)+ &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;','&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;FROM&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;distinct&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ROW_NUMBER() over (&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;order&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;desc&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; )&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; rownum,a.EFF_DT&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;distinct&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;case&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Dec20'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2020-12-31'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Jan21'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2021-01-31'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Nov20'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2020-11-30'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Oct20'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2020-10-31'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;else&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2999-01-01'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;end&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; [FASTControl]&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;[dbo]&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;.A&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;LM_NVPData &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; QRM_DB &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;in&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Jan21'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Dec20'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Nov20'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Oct20'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;)a&lt;/P&gt;&lt;P&gt;)F&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f.rownum&amp;lt;=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;) eff_dt &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;order&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;SET @Effdt = &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;LEFT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(@Effdt, LEN(@Effdt)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Set @Query=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;' &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;select * from&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;select case when qrm_db=''Dec20'' then ''2020-12-31''&lt;/P&gt;&lt;P&gt;when qrm_db=''Jan21'' then ''2021-01-31''&lt;/P&gt;&lt;P&gt;when qrm_db=''Nov20'' then ''2020-11-30''&lt;/P&gt;&lt;P&gt;when qrm_db=''Oct20'' then ''2020-10-31''&lt;/P&gt;&lt;P&gt;else ''2999-01-01'' end EFF_DT , NVPnode,RateIndex from [FASTControl].[dbo].ALM_NVPData&lt;/P&gt;&lt;P&gt;where QRM_DB in (''Jan21'',''Dec20'',''Nov20'',''Oct20'')&lt;/P&gt;&lt;P&gt;)a&lt;/P&gt;&lt;P&gt;pivot&lt;/P&gt;&lt;P&gt;( max(RateIndex)&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;For EFF_DT in ('&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;+@Effdt+&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;') &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;)pivts'&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;EXECUTE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; sp_executesql @Query;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I am able to execute above SQL code in SQL server but from SAS getting below error message may be variable not able to store dynamic SQL code into memory. do we know other solution to this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;NOTE: The quoted string currently being processed has become more than 262 characters long. You might have unbalanced quotation&lt;/P&gt;&lt;P&gt;marks.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Mar 2021 23:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722743#M27915</guid>
      <dc:creator>Ashpak</dc:creator>
      <dc:date>2021-03-01T23:56:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL code storing in macro variable and call to Execute proc sql;</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722746#M27916</link>
      <description>&lt;P&gt;You will have to do this in an EXECUTE statement, not a SELECT statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc SQL;

connect to odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);

execute(

SELECT @Effdt += + QUOTENAME(EFF_DT)+ ','

FROM

(

select distinct EFF_DT from

 

(

select ROW_NUMBER() over (order by EFF_DT desc )as rownum,a.EFF_DT

from (

select distinct case when qrm_db='Dec20' then '2020-12-31'

when qrm_db='Jan21' then '2021-01-31'

when qrm_db='Nov20' then '2020-11-30'

when qrm_db='Oct20' then '2020-10-31'

else '2999-01-01' end as EFF_DT

from [FASTControl].[dbo].ALM_NVPData where QRM_DB in ('Jan21','Dec20','Nov20','Oct20')

 

)a

)F

where f.rownum&amp;lt;=4

) eff_dt order by EFF_DT

SET @Effdt = LEFT(@Effdt, LEN(@Effdt)-1)

Set @Query='

select * from

(

select case when qrm_db=''Dec20'' then ''2020-12-31''

when qrm_db=''Jan21'' then ''2021-01-31''

when qrm_db=''Nov20'' then ''2020-11-30''

when qrm_db=''Oct20'' then ''2020-10-31''

else ''2999-01-01'' end EFF_DT , NVPnode,RateIndex from [FASTControl].[dbo].ALM_NVPData

where QRM_DB in (''Jan21'',''Dec20'',''Nov20'',''Oct20'')

)a

pivot

( max(RateIndex)

For EFF_DT in ('+@Effdt+')

)pivts'

;

EXECUTE sp_executesql @Query;
) by ODBC;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Mar 2021 00:11:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722746#M27916</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-03-02T00:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL code storing in macro variable and call to Execute proc sql;</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722749#M27917</link>
      <description>&lt;P&gt;Thanks, but getting below error message&lt;/P&gt;&lt;P&gt;ERROR: CLI execute error: [OpenLink][ODBC][SQL Server]Incorrect syntax near the keyword 'in'. (156) : [OpenLink][ODBC][SQL&lt;/P&gt;&lt;P&gt;Server]General SQL Server error: Check messages from the SQL Server (156)&lt;/P&gt;&lt;P&gt;NOTE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for the below code&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;Proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;connect&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;execute&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;DECLARE @Effdt NVARCHAR(MAX) = &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;''&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#ff0000"&gt;DECLARE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; @Query NVARCHAR(MAX) = &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;''&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;SELECT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; @Effdt += + QUOTENAME(EFF_DT)+ &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;','&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FROM&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;distinct&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ROW_NUMBER() over (&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;order&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;desc&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; )&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; rownum,a.EFF_DT&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;distinct&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;case&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Dec20'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2020-12-31'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Jan21'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2021-01-31'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Nov20'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2020-11-30'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;when&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; qrm_db=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Oct20'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2020-10-31'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;else&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#800080"&gt;'2999-01-01'&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;end&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; [FASTControl]&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;[dbo]&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;.A&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;LM_NVPData &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; QRM_DB &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;in&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Jan21'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Dec20'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Nov20'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'Oct20'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;)a&lt;/P&gt;&lt;P&gt;)F&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;where&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; f.rownum&amp;lt;=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;4&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;) eff_dt &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;order&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; EFF_DT&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;SET @Effdt = &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;LEFT&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(@Effdt, LEN(@Effdt)-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#008080"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Set @Query=&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select * from&lt;/P&gt;&lt;P&gt;(&lt;/P&gt;&lt;P&gt;select case when qrm_db=''Dec20'' then ''2020-12-31''&lt;/P&gt;&lt;P&gt;when qrm_db=''Jan21'' then ''2021-01-31''&lt;/P&gt;&lt;P&gt;when qrm_db=''Nov20'' then ''2020-11-30''&lt;/P&gt;&lt;P&gt;when qrm_db=''Oct20'' then ''2020-10-31''&lt;/P&gt;&lt;P&gt;else ''2999-01-01'' end EFF_DT , NVPnode,RateIndex from [FASTControl].[dbo].ALM_NVPData&lt;/P&gt;&lt;P&gt;where QRM_DB in (''Jan21'',''Dec20'',''Nov20'',''Oct20'')&lt;/P&gt;&lt;P&gt;)a&lt;/P&gt;&lt;P&gt;pivot&lt;/P&gt;&lt;P&gt;( max(RateIndex)&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;For EFF_DT in ('&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;+@Effdt+&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;')&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;)pivts'&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;EXECUTE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; sp_executesql @Query;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 00:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722749#M27917</guid>
      <dc:creator>Ashpak</dc:creator>
      <dc:date>2021-03-02T00:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL code storing in macro variable and call to Execute proc sql;</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722751#M27918</link>
      <description>&lt;P&gt;You've got an SQL Server SQL syntax error in there somewhere but I don't know which "IN" is causing the problem. Obviously I can't test that for you. Try simplifying your SQL, chopping out sections, until you have found the problem.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: I know our SQL Server doesn't like double quotes. Try converting these to single quotes.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 00:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722751#M27918</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-03-02T00:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL code storing in macro variable and call to Execute proc sql;</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722756#M27919</link>
      <description>&lt;P&gt;I tried with Simple code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;Proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;connect&lt;/FONT&gt; &lt;FONT face="Courier New" size="3" color="#0000ff"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;Execute&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;"DECLARE @Query NVARCHAR(MAX) = '';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Set @Query='select distinct qrm_db from [FASTControl].[dbo].ALM_NVPData'&lt;/P&gt;&lt;P&gt;PRINT @Query&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#800080"&gt;EXECUTE sp_executesql @Query"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; odbc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Log says &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;/P&gt;&lt;P&gt;31 connect to odbc(DSN=&amp;amp;DSN user=&amp;amp;user password=&amp;amp;pwds);&lt;/P&gt;&lt;P&gt;32 Execute ("DECLARE @Query NVARCHAR(MAX) = '';&lt;/P&gt;&lt;P&gt;33 Set @Query='select distinct qrm_db from [FASTControl].[dbo].ALM_NVPData'&lt;/P&gt;&lt;P&gt;34 PRINT @Query&lt;/P&gt;&lt;P&gt;35 EXECUTE sp_executesql @Query") by odbc;&lt;/P&gt;&lt;P&gt;36 quit;&lt;/P&gt;&lt;P&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/P&gt;&lt;P&gt;real time 0.17 seconds&lt;/P&gt;&lt;P&gt;cpu time 0.04 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't see any output is this SQL executes?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 01:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722756#M27919</guid>
      <dc:creator>Ashpak</dc:creator>
      <dc:date>2021-03-02T01:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL code storing in macro variable and call to Execute proc sql;</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722770#M27922</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/337616"&gt;@Ashpak&lt;/a&gt;&amp;nbsp; - see my response to your new post.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Mar 2021 02:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Dynamic-SQL-code-storing-in-macro-variable-and-call-to-Execute/m-p/722770#M27922</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-03-02T02:46:14Z</dc:date>
    </item>
  </channel>
</rss>

