<?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 - lengthy dynamic sql statement in variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483105#M125241</link>
    <description>&lt;P&gt;As always, show us.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WE need to see the SASLOG (run with OPTIONS MPRINT; turned on).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need to see the results of the %PUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And we need to see anything else that might be relevant here (you figure out what is relevant, we don't know because we are not in your situation).&lt;/P&gt;</description>
    <pubDate>Wed, 01 Aug 2018 16:43:43 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-08-01T16:43:43Z</dc:date>
    <item>
      <title>Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483104#M125240</link>
      <description>&lt;P&gt;I have a SAS program that loops through a CSV file and dynamically creates a SQL statement (well over 1000 characters). I have a macro variable that contains the SQL statement and I can %PUT the entire statement to the log, but I have not found a method to run this statement in a Proc SQL. The variable truncates. I have tried to piece together %substr of the macro variable, but the execution always evaluates them individually. Is there a method/workaround to run my lengthy dynamic SQL statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 16:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483104#M125240</guid>
      <dc:creator>broth38</dc:creator>
      <dc:date>2018-08-01T16:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483105#M125241</link>
      <description>&lt;P&gt;As always, show us.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WE need to see the SASLOG (run with OPTIONS MPRINT; turned on).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need to see the results of the %PUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And we need to see anything else that might be relevant here (you figure out what is relevant, we don't know because we are not in your situation).&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 16:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483105#M125241</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-01T16:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483106#M125242</link>
      <description>&lt;P&gt;One idea&amp;nbsp;would be to use &lt;A href="https://blogs.sas.com/content/sgf/2017/08/02/call-execute-for-sas-data-driven-programming/" target="_self"&gt;CALL EXECUTE&lt;/A&gt; in a data step and that way you do not need to even worry about the macro world.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 16:45:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483106#M125242</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2018-08-01T16:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483111#M125243</link>
      <description>&lt;P&gt;Thanks for the quick response! Much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data SQL_Lines;&lt;BR /&gt;infile 'SQL_Dimensions_Test1.csv' dlm=',' dsd truncover firstobs=2 end=last;&lt;BR /&gt;input ReportNo $ VarName $ ProvName $ RowLabel $ Map1 $ Map1_Oper1 $ Map1_Oper2 $ Map1_Value1 $ Map1_Value2 $ Map2 $ Map2_Oper1 $ Map2_Oper2 $Map2_Value1 $ Map2_Value2 $ Map1_NS $ Map2_NS $;&lt;/P&gt;&lt;P&gt;length strSQL $10000;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;strSQL =&amp;nbsp;strip(strSQL) || " AND "...&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;retain strSQL;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once the strSQL SQL statement has been assembed, what would be the Call Execute syntax to successfully run the SQL statement?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 16:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483111#M125243</guid>
      <dc:creator>broth38</dc:creator>
      <dc:date>2018-08-01T16:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483115#M125245</link>
      <description>&lt;P&gt;Basically, the string in CALL EXECUTE should be a valid SAS statement that you could copy, paste, and run as is.&amp;nbsp; In your case it should have "PROC SQL;" in the front and "QUIT;" at the end of the string.&amp;nbsp; I think the only thing I would change in your code is to add a little something at the end.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;.
.
.
retain strSQL;
.
.
.
if last then do;
   strSQL = cat("PROC SQL; ", StrSQL, "QUIT;");
   CALL EXECUTE(strSQL);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This way it only executes it when it reaches the end of the csv.&amp;nbsp; You could also throw "output;" in there so you can see the table with only the last row (to inspect the generated SQL string).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 17:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483115#M125245</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2018-08-01T17:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483123#M125251</link>
      <description>&lt;P&gt;Wait ... you are using SQL language in the macro variable, but you want to use it inside of a data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will always fail. You can only use data step language inside a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I did ask to see the SASLOG run with OPTIONS MPRINT turned on, this has not been provided. I did ask to see the entire results of the %PUT command, this has not been provided.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 17:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483123#M125251</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-01T17:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483134#M125257</link>
      <description>&lt;P&gt;Thank you very much. That worked great! Very much appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 17:47:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483134#M125257</guid>
      <dc:creator>broth38</dc:creator>
      <dc:date>2018-08-01T17:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL - lengthy dynamic sql statement in variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483141#M125261</link>
      <description>&lt;P&gt;I believe that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/224137"&gt;@broth38&lt;/a&gt;&amp;nbsp;has an existing process that was creating SQL statements from a csv, but couldn't figure out how to run them once created.&amp;nbsp; Judging by the input statement in the reply, the csv might list out different variable transformations/equations to be dynamically created.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length statement $ 1000;
   statement = "create table want as";
   output;

   statement = "select";
   output;

   statement = "*";
   output;

   statement = "from have2;";
   output;
run;

data want;
   set have end=last;
   retain sqlStr;
   length SqlStr $10000;
   
   sqlStr = catx(" ", sqlString, statement);
   
   if last then do;
      sqlStr = catx(" ", "PROC SQL;", StrSQL, "QUIT;");
      output;
   end;
run;
&lt;BR /&gt;data _NULL_;&lt;BR /&gt;   set want;&lt;BR /&gt;   call execute(sqlStr);&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The have dataset is where the SQL strings are being created (I can't ever get datalines to work right the first time which is why there is a bunch of output statements in there).&amp;nbsp; The want dataset is where&amp;nbsp;SAS puts together the SQL string to be ran.&amp;nbsp; The data _NULL_ step is when that SQL code is ran (I broke it out into a separate step from my previous reply).&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 18:02:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-lengthy-dynamic-sql-statement-in-variable/m-p/483141#M125261</guid>
      <dc:creator>Urban_Science</dc:creator>
      <dc:date>2018-08-01T18:02:45Z</dc:date>
    </item>
  </channel>
</rss>

