繁体   English   中英

如何使用 SAS Studio 从文件夹中的.txt 文件读取和运行 SQL(在 Snowflake 上)并将 o/p 导出到 excel?

[英]How to read & run SQL(on Snowflake) from a .txt file in a folder using SAS Studio and export the o/p to excel?

我在 SAS Studio 的一个文件夹中有一些包含 SQL 代码的 .txt 文件用于 Snowflake 数据库。 我需要我的逻辑来使用 SAS Studio 从文件夹中选择和读取 the.txt 文件并在雪花上运行,并且需要 excel 文件中代码的 output。

期待有关如何实现这一过程的帮助和详细步骤? 我是 SAS 的新手,对 SQL 很好。

在下面代码的帮助下,我能够从文本文件中读取 SQL,运行 SAS 并将 output 导出到 a.csv 文件。 但是我需要从一个文件夹中读取多个文本文件并分别为所有文件创建.csv output。 请参阅下面的代码并帮助我如何为多个文本文件实现此目的。

data _null_;             *reading the SQL script into a variable, hopefully under 32767?;
infile "/dslanalytics-shared/dgupt12/SQLs/Query.txt" recfm=f lrecl=32767 pad;
input @1 sqlcode $32767.;
call symputx('sqlcode',sqlcode);  *putting it into a macro variable;
run;

proc sql;
connect to odbc as mycon (complete="DRIVER={SnowflakeDSIIDriver};
SERVER=;
UID=&usr.;
PWD=&pwd.;
WAREHOUSE=;
DATABASE=;
SCHEMA=;
dbcommit=10000 autocommit=no
readbuff=200 insertbuff=200;");

create table final_export as
select * from connection to mycon(&sqlcode.);
disconnect from mycon;
quit;

proc export data = work.final_export
outfile = "/dslanalytics-shared/dgupt12/Report/final_report.csv"
DBMS = csv REPLACE;
run;

您可以通过定义和使用 SAS 宏来为多个文件执行此操作:

%macro file_input (INPUTXT=,OUTPUTCSV=);

data _null_;  
infile "&INPUTTXT." recfm=f lrecl=32767 pad;
input @1 sqlcode $32767.;
call symputx('sqlcode',sqlcode);  *putting it into a macro variable;
run;

proc sql;
connect to odbc as mycon (complete="DRIVER={SnowflakeDSIIDriver};
SERVER=;
UID=&usr.;
PWD=&pwd.;
WAREHOUSE=;
DATABASE=;
SCHEMA=;
dbcommit=10000 autocommit=no
readbuff=200 insertbuff=200;");

create table final_export as
select * from connection to mycon(&sqlcode.);
disconnect from mycon;
quit;

proc export data = work.final_export
outfile = "&OUTPUTCSV."
DBMS = csv REPLACE;
run;

%mend;

/* 3 Examples for macro calls */

%file_input (INPUTTXT=%str(/dslanalytics-shared/dgupt12/SQLs/Query.txt),
             OUTPUTCSV=%str(/dslanalytics-shared/dgupt12/Report/final_report.csv));
%file_input (INPUTTXT=%str(/dslanalytics-shared/dgupt12/SQLs/Query2.txt),
             OUTPUTCSV=%str(/dslanalytics-shared/dgupt12/Report/final_report2.csv));
%file_input (INPUTTXT=%str(/dslanalytics-shared/dgupt12/SQLs/Query3.txt),
             OUTPUTCSV=%str(/dslanalytics-shared/dgupt12/Report/final_report3.csv));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM