mirror of
https://github.com/yabwon/SAS_PACKAGES.git
synced 2026-01-08 07:20:06 +00:00
version 2.1
recompiled with new framework setup
This commit is contained in:
65
packages/SQLinDS/001_macro/dssql_inner.sas
Normal file
65
packages/SQLinDS/001_macro/dssql_inner.sas
Normal file
@@ -0,0 +1,65 @@
|
||||
/*** HELP START ***/
|
||||
|
||||
/* >>> %dsSQL_Inner() macro: <<<
|
||||
*
|
||||
* Internal macro called by dsSQL() function.
|
||||
* The macro generates a uniqualy named sql view on the fly
|
||||
* which is stored in DSSQL library.
|
||||
*
|
||||
* Recommended for SAS 9.3 and higher.
|
||||
* Based on paper:
|
||||
* "Use the Full Power of SAS in Your Function-Style Macros"
|
||||
* by Mike Rhoads, Westat, Rockville, MD
|
||||
* https://support.sas.com/resources/papers/proceedings12/004-2012.pdf
|
||||
*
|
||||
**/
|
||||
|
||||
/*** HELP END ***/
|
||||
|
||||
/* inner macro */
|
||||
%MACRO dsSQL_Inner() / secure;
|
||||
%local query tempfile1 tempfile2 ps_tmp;
|
||||
%let query = %superq(query_arg);
|
||||
%let query = %sysfunc(dequote(&query));
|
||||
|
||||
%let viewname = dsSQL.dsSQLtmpview&UNIQUE_INDEX_2.;
|
||||
|
||||
%let tempfile1 = A%sysfunc(datetime(), hex7.);
|
||||
%let tempfile2 = B%sysfunc(datetime(), hex7.);
|
||||
|
||||
filename &tempfile1. temp;
|
||||
filename &tempfile2. temp;
|
||||
|
||||
%let ps_tmp = %sysfunc(getoption(ps));
|
||||
options ps = MAX;
|
||||
proc printto log = &tempfile1.;
|
||||
run;
|
||||
/* get the query shape i.e. the executed one */
|
||||
proc sql feedback noexec;
|
||||
&query
|
||||
;
|
||||
quit;
|
||||
proc printto;
|
||||
run;
|
||||
options ps = &ps_tmp.;
|
||||
|
||||
%put *** executed as ***;
|
||||
data _null_;
|
||||
infile &tempfile1. FIRSTOBS = 2; /* <- 2 to ignore header */
|
||||
file &tempfile2.;
|
||||
/* create the view name */
|
||||
if _N_ = 1 then
|
||||
put " create view &viewname. as ";
|
||||
input;
|
||||
put _infile_;
|
||||
putlog ">" _infile_;
|
||||
run;
|
||||
%put *****************;
|
||||
|
||||
proc sql;
|
||||
%include &tempfile2.; /* &query */
|
||||
;
|
||||
quit;
|
||||
filename &tempfile1. clear;
|
||||
filename &tempfile2. clear;
|
||||
%MEND dsSQL_Inner;
|
||||
56
packages/SQLinDS/001_macro/sql.sas
Normal file
56
packages/SQLinDS/001_macro/sql.sas
Normal file
@@ -0,0 +1,56 @@
|
||||
/*** HELP START ***/
|
||||
|
||||
/* >>> %SQL() macro: <<<
|
||||
*
|
||||
* Main macro which allows to use
|
||||
* SQL's queries in the data step.
|
||||
* Recommended for SAS 9.3 and higher.
|
||||
* Based on paper:
|
||||
* "Use the Full Power of SAS in Your Function-Style Macros"
|
||||
* by Mike Rhoads, Westat, Rockville, MD
|
||||
* https://support.sas.com/resources/papers/proceedings12/004-2012.pdf
|
||||
*
|
||||
* SYNTAX:
|
||||
|
||||
%sql(<nonempty sql querry code>)
|
||||
|
||||
* The sql querry code is limited to 32000 bytes.
|
||||
*
|
||||
* EXAMPLE 1: simple sql query
|
||||
|
||||
data class_subset;
|
||||
set %SQL(select name, sex, height from sashelp.class where age > 12);
|
||||
run;
|
||||
|
||||
* EXAMPLE 2: query with dataset options
|
||||
|
||||
data renamed;
|
||||
set %SQL(select * from sashelp.class where sex = "F")(rename = (age=age2));
|
||||
run;
|
||||
|
||||
* EXAMPLE 3: dictionaries in datastep
|
||||
|
||||
data dictionary;
|
||||
set %SQL(select * from dictionary.macros);
|
||||
run;
|
||||
|
||||
**/
|
||||
|
||||
/*** HELP END ***/
|
||||
|
||||
|
||||
/* Main User macro */
|
||||
%MACRO SQL() / PARMBUFF SECURE;
|
||||
%let SYSPBUFF = %superq(SYSPBUFF); /* macroquoting */
|
||||
%let SYSPBUFF = %substr(&SYSPBUFF, 2, %LENGTH(&SYSPBUFF) - 2); /* remove brackets */
|
||||
%let SYSPBUFF = %superq(SYSPBUFF); /* macroquoting */
|
||||
%let SYSPBUFF = %sysfunc(quote(&SYSPBUFF)); /* quotes */
|
||||
%put NOTE:*** the query ***; /* print out the query in the log */
|
||||
%put NOTE-&SYSPBUFF.;
|
||||
%put NOTE-*****************;
|
||||
|
||||
%local UNIQUE_INDEX; /* internal variable, a unique index for views */
|
||||
%let UNIQUE_INDEX = &SYSINDEX;
|
||||
%sysfunc(dsSQL(&UNIQUE_INDEX, &SYSPBUFF)) /* <-- call dsSQL() function,
|
||||
see the WORK.SQLinDSfcmp dataset */
|
||||
%MEND SQL;
|
||||
Reference in New Issue
Block a user