<?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: How can I read in .SQL files and have them run within SAS? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847310#M335004</link>
    <description>&lt;P&gt;Do not attach files to share error messages.&amp;nbsp; Just copy the text and paste it into the pop-up window you get when you click on the Insert Code icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you must post a photo then just paste it in or use the Insert Photos icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot see the files because the site the SAS communities uses is blocked by my company.&amp;nbsp; Here is what your DOCX file looks like when I attempt to view it using the link the forum creates.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1669918663237.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77914i7303A9576AA2A52D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1669918663237.png" alt="Tom_0-1669918663237.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Dec 2022 18:18:00 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-12-01T18:18:00Z</dc:date>
    <item>
      <title>How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/716267#M221332</link>
      <description>&lt;P&gt;A colleague of mine does not uses SAS but uses R and SQL Server Management Studio. I need to collaborate with him to write SQL code but we were wondering if there was a way to import .SQL code (queries) into a SAS program and have them run within SAS. I couldn't find anything online but it seems like the only option is to copy and paste each SQL file separately into a SAS Program. Is there a better way to do this so SAS can run and write to a .SQL file?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 23:32:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/716267#M221332</guid>
      <dc:creator>InspectahDex</dc:creator>
      <dc:date>2021-02-02T23:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/716276#M221338</link>
      <description>&lt;P&gt;You can import sql files:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_; 
  file "&amp;amp;wdir\t.sql";
  put 'select * from sashelp.class;';
run;
options source2;
proc sql;
  %include "&amp;amp;wdir\t.sql";
quit;
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Running&amp;nbsp;the&amp;nbsp;file&amp;nbsp;as&amp;nbsp;pass-through&amp;nbsp;SQL would&amp;nbsp;be&amp;nbsp;trickier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 00:55:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/716276#M221338</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-02-03T00:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/716277#M221339</link>
      <description>&lt;P&gt;Maybe something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_; 
  file "&amp;amp;wdir\t-sql.sql";
  put ' * from [dbo].[tab1]' ;
run;
options source2;
proc sql;  
  connect using SQLSVR;
  select * from connection to SQLSVR(
    select
    %include "&amp;amp;wdir\t-sql.sql" ;
    order by AGE
  );
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 00:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/716277#M221339</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-02-03T00:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/846759#M334731</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a task where I need to read multiple texts files containing SQLs from a shared location on SAS studio and dump/export the output to a folder in SAS only.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to pick, read and run one .text file containing a SQL and export that to a folder. But how do I do if I have like 10 .text files present in the input location. Please help on this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_; *reading the SQL script into a variable, hopefully under 32767?;&lt;BR /&gt;infile "/dslanalytics-shared/dgupt12/SQLs/Query.txt" recfm=f lrecl=32767 pad;&lt;BR /&gt;input @1 sqlcode $32767.;&lt;BR /&gt;call symputx('sqlcode',sqlcode); *putting it into a macro variable;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;connect to odbc as mycon (complete="DRIVER={SnowflakeDSIIDriver};&lt;BR /&gt;SERVER=;&lt;BR /&gt;UID=&amp;amp;usr.;&lt;BR /&gt;PWD=&amp;amp;pwd.;&lt;BR /&gt;WAREHOUSE=;&lt;BR /&gt;DATABASE=;&lt;BR /&gt;SCHEMA=;&lt;BR /&gt;dbcommit=10000 autocommit=no&lt;BR /&gt;readbuff=200 insertbuff=200;");&lt;/P&gt;&lt;P&gt;create table final_export as&lt;BR /&gt;select * from connection to mycon(&amp;amp;sqlcode.);&lt;BR /&gt;disconnect from mycon;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;proc export data = work.final_export&lt;BR /&gt;outfile = "/dslanalytics-shared/dgupt12/Report/final_report.csv"&lt;BR /&gt;DBMS = csv REPLACE;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 09:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/846759#M334731</guid>
      <dc:creator>NarutoTaka</dc:creator>
      <dc:date>2022-11-29T09:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/846897#M334807</link>
      <description>&lt;P&gt;The way you are doing it is complicated and also macro variables are limited to a length of 32K. Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;'s %INCLUDE approach is way better:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Run_SQL (SQL_File = , CSV_File = );

proc sql;
connect to odbc as mycon (complete="DRIVER={SnowflakeDSIIDriver};
SERVER=;
UID=&amp;amp;usr.;
PWD=&amp;amp;pwd.;
WAREHOUSE=;
DATABASE=;
SCHEMA=;
dbcommit=10000 autocommit=no
readbuff=200 insertbuff=200;");

create table final_export as
select * from connection to mycon(
%include "/dslanalytics-shared/dgupt12/SQLs/&amp;amp;SQL_File.";
);
disconnect from mycon;
quit;

proc export data = work.final_export
outfile = "/dslanalytics-shared/dgupt12/Report/&amp;amp;CSV_File."
DBMS = csv REPLACE;
run;

%mend Run_SQL;

%Run_SQL (SQL_File = Query1.txt, CSV_File = final_report1.csv);
%Run_SQL (SQL_File = Query2.txt, CSV_File = final_report2.csv);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2022 19:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/846897#M334807</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-11-29T19:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847293#M335000</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;Thank you very much for the approach. This seems very easy from the one I am using.&lt;/P&gt;&lt;P&gt;But I am getting the below error while running the below code for the %Include statement. (File attached). I have executed the code as it is as you had shared. I have just updated the connection details to run. Was anything else needed to be changed?&lt;/P&gt;&lt;P&gt;And the output part which you have mentioned will have to written for each file, how do I get it to work dynamically which takes the name of the text file from the input location and generate output file with the same name?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help on this will be very much appreciated. I am new to SAS stuck with this task now.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 17:19:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847293#M335000</guid>
      <dc:creator>NarutoTaka</dc:creator>
      <dc:date>2022-12-01T17:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847310#M335004</link>
      <description>&lt;P&gt;Do not attach files to share error messages.&amp;nbsp; Just copy the text and paste it into the pop-up window you get when you click on the Insert Code icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you must post a photo then just paste it in or use the Insert Photos icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot see the files because the site the SAS communities uses is blocked by my company.&amp;nbsp; Here is what your DOCX file looks like when I attempt to view it using the link the forum creates.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1669918663237.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77914i7303A9576AA2A52D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1669918663237.png" alt="Tom_0-1669918663237.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 18:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847310#M335004</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-01T18:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847385#M335023</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Sorry, about that. I am getting this error when the code is taking the input files for processing. I hope it helps now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*1st error*/ 

MPRINT(RUN_SQL):   create table final_export as select * from connection to mycon( %include 
 "/dslanalytics-shared/dverma1/sql_to_exl/input_sql/Query1.txt";
 MPRINT(RUN_SQL):   );
 ERROR: CLI prepare error: SQL compilation error: syntax error line 1 at position 0 unexpected '%'.
 SQL statement: %include "/dslanalytics-shared/dverma1/sql_to_exl/input_sql/Query1.txt";.
 NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
 MPRINT(RUN_SQL):   disconnect from mycon;
 NOTE: Statement not executed due to NOEXEC option.
 MPRINT(RUN_SQL):   quit;
 NOTE: The SAS System stopped processing this step because of errors.

/*2nd Error*/

SYMBOLGEN:  Macro variable SQL_FILE resolves to Query2.txt
 MPRINT(RUN_SQL):   create table final_export as select * from connection to mycon( %include 
 "/dslanalytics-shared/dverma1/sql_to_exl/input_sql/Query2.txt";
 MPRINT(RUN_SQL):   );
 ERROR: CLI prepare error: SQL compilation error: syntax error line 1 at position 0 unexpected '%'.
 SQL statement: %include "/dslanalytics-shared/dverma1/sql_to_exl/input_sql/Query2.txt";.
 NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
 MPRINT(RUN_SQL):   disconnect from mycon;
 NOTE: Statement not executed due to NOEXEC option.
 MPRINT(RUN_SQL):   quit;
 NOTE: The SAS System stopped processing this step because of errors&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 03:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847385#M335023</guid>
      <dc:creator>NarutoTaka</dc:creator>
      <dc:date>2022-12-02T03:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I read in .SQL files and have them run within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847386#M335024</link>
      <description>&lt;P&gt;It does not really work to try to invoke %INCLUDE in the MIDDLE of a statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try copying the original file into another file with the extra code before and after it.&lt;/P&gt;
&lt;P&gt;I don't have access to T-SQL but lets just do an example with a normal SAS SQL query instead.&lt;/P&gt;
&lt;P&gt;So first create a file with the query. Let's call that file SQLCODE .&lt;/P&gt;
&lt;P&gt;Then copy that into a file name WANT that adds some text before and after it.&lt;/P&gt;
&lt;P&gt;Then use %INCLUDE to run it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename sqlcode temp;
data _null_; 
  file sqlcode ;
  put ' select * from sashelp.class ' ;
run;

filename want temp;

data _null_;
  infile sqlcode end=eof;
  file want;
  if _n_=1 then put 'create table class as (' ;
  input;
  put _infile_;
  if eof then put ');' ;
run;

proc sql;
%include want / source2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;2346  filename sqlcode temp;
2347  data _null_;
2348    file sqlcode ;
2349    put ' select * from sashelp.class ' ;
2350  run;

NOTE: The file SQLCODE is:
      (system-specific pathname),
      (system-specific file attributes)

NOTE: 1 record was written to the file (system-specific pathname).
      The minimum record length was 29.
      The maximum record length was 29.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


2351
2352  filename want temp;
2353
2354  data _null_;
2355    infile sqlcode end=eof;
2356    file want;
2357    if _n_=1 then put 'create table class as (' ;
2358    input;
2359    put _infile_;
2360    if eof then put ');' ;
2361  run;

NOTE: The infile SQLCODE is:
      (system-specific pathname),
      (system-specific file attributes)

NOTE: The file WANT is:
      (system-specific pathname),
      (system-specific file attributes)

NOTE: 1 record was read from the infile (system-specific pathname).
      The minimum record length was 29.
      The maximum record length was 29.
NOTE: 3 records were written to the file (system-specific pathname).
      The minimum record length was 2.
      The maximum record length was 29.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


2362
2363  proc sql;
2364  %include want / source2;
NOTE: %INCLUDE (level 1) file WANT is (system-specific pathname).
2365 +create table class as (
2366 + select * from sashelp.class
2367 +);
NOTE: Table WORK.CLASS created, with 19 rows and 5 columns.

NOTE: %INCLUDE (level 1) ending.
2368  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 03:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-read-in-SQL-files-and-have-them-run-within-SAS/m-p/847386#M335024</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-12-02T03:47:41Z</dc:date>
    </item>
  </channel>
</rss>

