mirror of
https://github.com/sasjs/core.git
synced 2026-01-03 23:50:06 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4456adf1dc | ||
|
|
03962c2a50 |
142
all.sas
142
all.sas
@@ -3444,9 +3444,9 @@ run;
|
|||||||
located. If not provided, is ignored.
|
located. If not provided, is ignored.
|
||||||
@param [out] outds= (0) The output table to load. If not provided, will
|
@param [out] outds= (0) The output table to load. If not provided, will
|
||||||
default to the table in the &ds parameter.
|
default to the table in the &ds parameter.
|
||||||
@param [in] flavour= (BASE) The SQL flavour to be applied to the output. Valid
|
@param [in] flavour= (SAS) The SQL flavour to be applied to the output. Valid
|
||||||
options:
|
options:
|
||||||
@li BASE (default) - suitable for regular proc sql
|
@li SAS (default) - suitable for regular proc sql
|
||||||
@li PGSQL - Used for Postgres databases
|
@li PGSQL - Used for Postgres databases
|
||||||
|
|
||||||
<h4> SAS Macros </h4>
|
<h4> SAS Macros </h4>
|
||||||
@@ -3459,7 +3459,7 @@ run;
|
|||||||
@author Allan Bowe (credit mjsq)
|
@author Allan Bowe (credit mjsq)
|
||||||
**/
|
**/
|
||||||
|
|
||||||
%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0,flavour=BASE
|
%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0,flavour=SAS
|
||||||
)/*/STORE SOURCE*/;
|
)/*/STORE SOURCE*/;
|
||||||
|
|
||||||
%if not %sysfunc(exist(&ds)) %then %do;
|
%if not %sysfunc(exist(&ds)) %then %do;
|
||||||
@@ -3475,7 +3475,7 @@ run;
|
|||||||
%if %index(&ds,.)=0 %then %let ds=WORK.&ds;
|
%if %index(&ds,.)=0 %then %let ds=WORK.&ds;
|
||||||
|
|
||||||
%let flavour=%upcase(&flavour);
|
%let flavour=%upcase(&flavour);
|
||||||
%if &flavour ne BASE and &flavour ne PGSQL %then %do;
|
%if &flavour ne SAS and &flavour ne PGSQL %then %do;
|
||||||
%put %str(WAR)NING: &flavour is not supported;
|
%put %str(WAR)NING: &flavour is not supported;
|
||||||
%return;
|
%return;
|
||||||
%end;
|
%end;
|
||||||
@@ -3529,7 +3529,7 @@ data _null_;
|
|||||||
%let var=%scan(&varlist,&i);
|
%let var=%scan(&varlist,&i);
|
||||||
%let vtype=%mf_getvartype(&ds,&var);
|
%let vtype=%mf_getvartype(&ds,&var);
|
||||||
%if &i=1 %then %do;
|
%if &i=1 %then %do;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put "insert into &outlib.&outds set ";
|
put "insert into &outlib.&outds set ";
|
||||||
put " &var="@;
|
put " &var="@;
|
||||||
%end;
|
%end;
|
||||||
@@ -3544,7 +3544,7 @@ data _null_;
|
|||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%else %do;
|
%else %do;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put " ,&var="@;
|
put " ,&var="@;
|
||||||
%end;
|
%end;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
%else %if &flavour=PGSQL %then %do;
|
||||||
@@ -3552,7 +3552,7 @@ data _null_;
|
|||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if &vtype=N %then %do;
|
%if &vtype=N %then %do;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put &var;
|
put &var;
|
||||||
%end;
|
%end;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
%else %if &flavour=PGSQL %then %do;
|
||||||
@@ -3565,7 +3565,7 @@ data _null_;
|
|||||||
put _____str;
|
put _____str;
|
||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put ';';
|
put ';';
|
||||||
%end;
|
%end;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
%else %if &flavour=PGSQL %then %do;
|
||||||
@@ -4398,6 +4398,7 @@ run;
|
|||||||
|
|
||||||
<h4> SAS Macros </h4>
|
<h4> SAS Macros </h4>
|
||||||
@li mf_existfileref.sas
|
@li mf_existfileref.sas
|
||||||
|
@li mf_getvarcount.sas
|
||||||
@li mp_getconstraints.sas
|
@li mp_getconstraints.sas
|
||||||
|
|
||||||
@param lib libref of the library to create DDL for. Should be assigned.
|
@param lib libref of the library to create DDL for. Should be assigned.
|
||||||
@@ -4685,71 +4686,80 @@ run;
|
|||||||
put "CREATE SCHEMA &schema;";
|
put "CREATE SCHEMA &schema;";
|
||||||
%do x=1 %to %sysfunc(countw(&dsnlist));
|
%do x=1 %to %sysfunc(countw(&dsnlist));
|
||||||
%let curds=%scan(&dsnlist,&x);
|
%let curds=%scan(&dsnlist,&x);
|
||||||
data _null_;
|
%local curdsvarcount;
|
||||||
file &fref mod;
|
%let curdsvarcount=%mf_getvarcount(&libref..&curds);
|
||||||
put "/* Postgres Flavour DDL for &schema..&curds */";
|
%if &curdsvarcount>1600 %then %do;
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
put "/* &libref..&curds contains &curdsvarcount vars */";
|
||||||
length fmt $32;
|
put "/* Postgres cannot create tables with over 1600 vars */";
|
||||||
if _n_=1 then do;
|
put "/* No DDL will be generated for this table";
|
||||||
if memtype='DATA' then do;
|
run;
|
||||||
put "CREATE TABLE &schema..&curds (";
|
%end;
|
||||||
|
%else %do;
|
||||||
|
data _null_;
|
||||||
|
file &fref mod;
|
||||||
|
put "/* Postgres Flavour DDL for &schema..&curds */";
|
||||||
|
data _null_;
|
||||||
|
file &fref mod;
|
||||||
|
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
||||||
|
length fmt $32;
|
||||||
|
if _n_=1 then do;
|
||||||
|
if memtype='DATA' then do;
|
||||||
|
put "CREATE TABLE &schema..&curds (";
|
||||||
|
end;
|
||||||
|
else do;
|
||||||
|
put "CREATE VIEW &schema..&curds (";
|
||||||
|
end;
|
||||||
|
put " "@@;
|
||||||
end;
|
end;
|
||||||
else do;
|
else put " ,"@@;
|
||||||
put "CREATE VIEW &schema..&curds (";
|
format=upcase(format);
|
||||||
end;
|
if 1=0 then; /* dummy if */
|
||||||
put " "@@;
|
%if &applydttm=YES %then %do;
|
||||||
end;
|
else if format=:'DATETIME' then fmt=' TIMESTAMP ';
|
||||||
else put " ,"@@;
|
%end;
|
||||||
format=upcase(format);
|
else if type='num' then fmt=' DOUBLE PRECISION';
|
||||||
if 1=0 then; /* dummy if */
|
else fmt='VARCHAR('!!cats(length)!!')';
|
||||||
%if &applydttm=YES %then %do;
|
if notnull='yes' then notnul=' NOT NULL';
|
||||||
else if format=:'DATETIME' then fmt=' TIMESTAMP ';
|
/* quote column names in case they represent reserved words */
|
||||||
%end;
|
name2=quote(trim(name));
|
||||||
else if type='num' then fmt=' DOUBLE PRECISION';
|
put name2 fmt notnul;
|
||||||
else fmt='VARCHAR('!!cats(length)!!')';
|
run;
|
||||||
if notnull='yes' then notnul=' NOT NULL';
|
|
||||||
/* quote column names in case they represent reserved words */
|
|
||||||
name2=quote(trim(name));
|
|
||||||
put name2 fmt notnul;
|
|
||||||
run;
|
|
||||||
|
|
||||||
/* Extra step for data constraints */
|
/* Extra step for data constraints */
|
||||||
%addConst()
|
%addConst()
|
||||||
|
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
put ');';
|
put ');';
|
||||||
run;
|
run;
|
||||||
|
|
||||||
/* Create Unique Indexes, but only if they were not already defined within
|
/* Create Unique Indexes, but only if they were not already defined within
|
||||||
the Constraints section. */
|
the Constraints section. */
|
||||||
data _null_;
|
data _null_;
|
||||||
*length ds $128;
|
*length ds $128;
|
||||||
set &idxinfo(
|
set &idxinfo(
|
||||||
where=(
|
where=(
|
||||||
memname="&curds"
|
memname="&curds"
|
||||||
and unique='yes'
|
and unique='yes'
|
||||||
and indxname not in (
|
and indxname not in (
|
||||||
%sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
|
%sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
);
|
file &fref mod;
|
||||||
file &fref mod;
|
by idxusage indxname;
|
||||||
by idxusage indxname;
|
if first.indxname then do;
|
||||||
/* ds=cats(libname,'.',memname); */
|
|
||||||
if first.indxname then do;
|
|
||||||
put 'CREATE UNIQUE INDEX "' indxname +(-1) '" ' "ON &schema..&curds(";
|
put 'CREATE UNIQUE INDEX "' indxname +(-1) '" ' "ON &schema..&curds(";
|
||||||
put ' "' name +(-1) '"' ;
|
put ' "' name +(-1) '"' ;
|
||||||
end;
|
end;
|
||||||
else put ' ,"' name +(-1) '"';
|
else put ' ,"' name +(-1) '"';
|
||||||
*else put ' ,' name ;
|
if last.indxname then do;
|
||||||
if last.indxname then do;
|
put ');';
|
||||||
put ');';
|
end;
|
||||||
end;
|
run;
|
||||||
run;
|
%end;
|
||||||
|
|
||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if %upcase(&showlog)=YES %then %do;
|
%if %upcase(&showlog)=YES %then %do;
|
||||||
|
|||||||
@@ -20,9 +20,9 @@
|
|||||||
located. If not provided, is ignored.
|
located. If not provided, is ignored.
|
||||||
@param [out] outds= (0) The output table to load. If not provided, will
|
@param [out] outds= (0) The output table to load. If not provided, will
|
||||||
default to the table in the &ds parameter.
|
default to the table in the &ds parameter.
|
||||||
@param [in] flavour= (BASE) The SQL flavour to be applied to the output. Valid
|
@param [in] flavour= (SAS) The SQL flavour to be applied to the output. Valid
|
||||||
options:
|
options:
|
||||||
@li BASE (default) - suitable for regular proc sql
|
@li SAS (default) - suitable for regular proc sql
|
||||||
@li PGSQL - Used for Postgres databases
|
@li PGSQL - Used for Postgres databases
|
||||||
|
|
||||||
<h4> SAS Macros </h4>
|
<h4> SAS Macros </h4>
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
@author Allan Bowe (credit mjsq)
|
@author Allan Bowe (credit mjsq)
|
||||||
**/
|
**/
|
||||||
|
|
||||||
%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0,flavour=BASE
|
%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0,flavour=SAS
|
||||||
)/*/STORE SOURCE*/;
|
)/*/STORE SOURCE*/;
|
||||||
|
|
||||||
%if not %sysfunc(exist(&ds)) %then %do;
|
%if not %sysfunc(exist(&ds)) %then %do;
|
||||||
@@ -51,7 +51,7 @@
|
|||||||
%if %index(&ds,.)=0 %then %let ds=WORK.&ds;
|
%if %index(&ds,.)=0 %then %let ds=WORK.&ds;
|
||||||
|
|
||||||
%let flavour=%upcase(&flavour);
|
%let flavour=%upcase(&flavour);
|
||||||
%if &flavour ne BASE and &flavour ne PGSQL %then %do;
|
%if &flavour ne SAS and &flavour ne PGSQL %then %do;
|
||||||
%put %str(WAR)NING: &flavour is not supported;
|
%put %str(WAR)NING: &flavour is not supported;
|
||||||
%return;
|
%return;
|
||||||
%end;
|
%end;
|
||||||
@@ -105,7 +105,7 @@ data _null_;
|
|||||||
%let var=%scan(&varlist,&i);
|
%let var=%scan(&varlist,&i);
|
||||||
%let vtype=%mf_getvartype(&ds,&var);
|
%let vtype=%mf_getvartype(&ds,&var);
|
||||||
%if &i=1 %then %do;
|
%if &i=1 %then %do;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put "insert into &outlib.&outds set ";
|
put "insert into &outlib.&outds set ";
|
||||||
put " &var="@;
|
put " &var="@;
|
||||||
%end;
|
%end;
|
||||||
@@ -120,7 +120,7 @@ data _null_;
|
|||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%else %do;
|
%else %do;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put " ,&var="@;
|
put " ,&var="@;
|
||||||
%end;
|
%end;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
%else %if &flavour=PGSQL %then %do;
|
||||||
@@ -128,7 +128,7 @@ data _null_;
|
|||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if &vtype=N %then %do;
|
%if &vtype=N %then %do;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put &var;
|
put &var;
|
||||||
%end;
|
%end;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
%else %if &flavour=PGSQL %then %do;
|
||||||
@@ -141,7 +141,7 @@ data _null_;
|
|||||||
put _____str;
|
put _____str;
|
||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if &flavour=BASE %then %do;
|
%if &flavour=SAS %then %do;
|
||||||
put ';';
|
put ';';
|
||||||
%end;
|
%end;
|
||||||
%else %if &flavour=PGSQL %then %do;
|
%else %if &flavour=PGSQL %then %do;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
<h4> SAS Macros </h4>
|
<h4> SAS Macros </h4>
|
||||||
@li mf_existfileref.sas
|
@li mf_existfileref.sas
|
||||||
|
@li mf_getvarcount.sas
|
||||||
@li mp_getconstraints.sas
|
@li mp_getconstraints.sas
|
||||||
|
|
||||||
@param lib libref of the library to create DDL for. Should be assigned.
|
@param lib libref of the library to create DDL for. Should be assigned.
|
||||||
@@ -304,71 +305,80 @@ run;
|
|||||||
put "CREATE SCHEMA &schema;";
|
put "CREATE SCHEMA &schema;";
|
||||||
%do x=1 %to %sysfunc(countw(&dsnlist));
|
%do x=1 %to %sysfunc(countw(&dsnlist));
|
||||||
%let curds=%scan(&dsnlist,&x);
|
%let curds=%scan(&dsnlist,&x);
|
||||||
data _null_;
|
%local curdsvarcount;
|
||||||
file &fref mod;
|
%let curdsvarcount=%mf_getvarcount(&libref..&curds);
|
||||||
put "/* Postgres Flavour DDL for &schema..&curds */";
|
%if &curdsvarcount>1600 %then %do;
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
put "/* &libref..&curds contains &curdsvarcount vars */";
|
||||||
length fmt $32;
|
put "/* Postgres cannot create tables with over 1600 vars */";
|
||||||
if _n_=1 then do;
|
put "/* No DDL will be generated for this table";
|
||||||
if memtype='DATA' then do;
|
run;
|
||||||
put "CREATE TABLE &schema..&curds (";
|
%end;
|
||||||
|
%else %do;
|
||||||
|
data _null_;
|
||||||
|
file &fref mod;
|
||||||
|
put "/* Postgres Flavour DDL for &schema..&curds */";
|
||||||
|
data _null_;
|
||||||
|
file &fref mod;
|
||||||
|
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
||||||
|
length fmt $32;
|
||||||
|
if _n_=1 then do;
|
||||||
|
if memtype='DATA' then do;
|
||||||
|
put "CREATE TABLE &schema..&curds (";
|
||||||
|
end;
|
||||||
|
else do;
|
||||||
|
put "CREATE VIEW &schema..&curds (";
|
||||||
|
end;
|
||||||
|
put " "@@;
|
||||||
end;
|
end;
|
||||||
else do;
|
else put " ,"@@;
|
||||||
put "CREATE VIEW &schema..&curds (";
|
format=upcase(format);
|
||||||
end;
|
if 1=0 then; /* dummy if */
|
||||||
put " "@@;
|
%if &applydttm=YES %then %do;
|
||||||
end;
|
else if format=:'DATETIME' then fmt=' TIMESTAMP ';
|
||||||
else put " ,"@@;
|
%end;
|
||||||
format=upcase(format);
|
else if type='num' then fmt=' DOUBLE PRECISION';
|
||||||
if 1=0 then; /* dummy if */
|
else fmt='VARCHAR('!!cats(length)!!')';
|
||||||
%if &applydttm=YES %then %do;
|
if notnull='yes' then notnul=' NOT NULL';
|
||||||
else if format=:'DATETIME' then fmt=' TIMESTAMP ';
|
/* quote column names in case they represent reserved words */
|
||||||
%end;
|
name2=quote(trim(name));
|
||||||
else if type='num' then fmt=' DOUBLE PRECISION';
|
put name2 fmt notnul;
|
||||||
else fmt='VARCHAR('!!cats(length)!!')';
|
run;
|
||||||
if notnull='yes' then notnul=' NOT NULL';
|
|
||||||
/* quote column names in case they represent reserved words */
|
|
||||||
name2=quote(trim(name));
|
|
||||||
put name2 fmt notnul;
|
|
||||||
run;
|
|
||||||
|
|
||||||
/* Extra step for data constraints */
|
/* Extra step for data constraints */
|
||||||
%addConst()
|
%addConst()
|
||||||
|
|
||||||
data _null_;
|
data _null_;
|
||||||
file &fref mod;
|
file &fref mod;
|
||||||
put ');';
|
put ');';
|
||||||
run;
|
run;
|
||||||
|
|
||||||
/* Create Unique Indexes, but only if they were not already defined within
|
/* Create Unique Indexes, but only if they were not already defined within
|
||||||
the Constraints section. */
|
the Constraints section. */
|
||||||
data _null_;
|
data _null_;
|
||||||
*length ds $128;
|
*length ds $128;
|
||||||
set &idxinfo(
|
set &idxinfo(
|
||||||
where=(
|
where=(
|
||||||
memname="&curds"
|
memname="&curds"
|
||||||
and unique='yes'
|
and unique='yes'
|
||||||
and indxname not in (
|
and indxname not in (
|
||||||
%sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
|
%sysfunc(tranwrd("&constraints_used",%str( ),%str(",")))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
);
|
file &fref mod;
|
||||||
file &fref mod;
|
by idxusage indxname;
|
||||||
by idxusage indxname;
|
if first.indxname then do;
|
||||||
/* ds=cats(libname,'.',memname); */
|
|
||||||
if first.indxname then do;
|
|
||||||
put 'CREATE UNIQUE INDEX "' indxname +(-1) '" ' "ON &schema..&curds(";
|
put 'CREATE UNIQUE INDEX "' indxname +(-1) '" ' "ON &schema..&curds(";
|
||||||
put ' "' name +(-1) '"' ;
|
put ' "' name +(-1) '"' ;
|
||||||
end;
|
end;
|
||||||
else put ' ,"' name +(-1) '"';
|
else put ' ,"' name +(-1) '"';
|
||||||
*else put ' ,' name ;
|
if last.indxname then do;
|
||||||
if last.indxname then do;
|
put ');';
|
||||||
put ');';
|
end;
|
||||||
end;
|
run;
|
||||||
run;
|
%end;
|
||||||
|
|
||||||
%end;
|
%end;
|
||||||
%end;
|
%end;
|
||||||
%if %upcase(&showlog)=YES %then %do;
|
%if %upcase(&showlog)=YES %then %do;
|
||||||
|
|||||||
Reference in New Issue
Block a user