diff --git a/all.sas b/all.sas index d1abba4..766e48e 100644 --- a/all.sas +++ b/all.sas @@ -3444,6 +3444,10 @@ run; located. If not provided, is ignored. @param [out] outds= (0) The output table to load. If not provided, will default to the table in the &ds parameter. + @param [in] flavour= (BASE) The SQL flavour to be applied to the output. Valid + options: + @li BASE (default) - suitable for regular proc sql + @li PGSQL - Used for Postgres databases

SAS Macros

@li mf_existfileref.sas @@ -3455,18 +3459,29 @@ run; @author Allan Bowe (credit mjsq) **/ -%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0 +%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0,flavour=BASE )/*/STORE SOURCE*/; %if not %sysfunc(exist(&ds)) %then %do; - %put %str(WARN)ING: &ds does not exist; + %put %str(WAR)NING: &ds does not exist; + %return; +%end; + +%if not %sysfunc(exist(&ds)) %then %do; + %put %str(WAR)NING: &ds does not exist; %return; %end; %if %index(&ds,.)=0 %then %let ds=WORK.&ds; +%let flavour=%upcase(&flavour); +%if &flavour ne BASE and &flavour ne PGSQL %then %do; + %put %str(WAR)NING: &flavour is not supported; + %return; +%end; + %if &outref=0 %then %do; - %put %str(WARN)ING: Please provide a fileref; + %put %str(WAR)NING: Please provide a fileref; %return; %end; %if %mf_existfileref(&outref)=0 %then %do; @@ -3497,8 +3512,9 @@ select count(*) into: nobs TRIMMED from &ds; run; %end; -%local varlist; +%local varlist varlistcomma; %let varlist=%mf_getvarlist(&ds); +%let varlistcomma=%mf_getvarlist(&ds,dlm=%str(,),quote=double); /* next, export data */ data _null_; @@ -3513,23 +3529,49 @@ data _null_; %let var=%scan(&varlist,&i); %let vtype=%mf_getvartype(&ds,&var); %if &i=1 %then %do; - put "insert into &outlib.&outds set "; - put " &var="@; + %if &flavour=BASE %then %do; + put "insert into &outlib.&outds set "; + put " &var="@; + %end; + %else %if &flavour=PGSQL %then %do; + _____str=cats( + "INSERT INTO &outlib.&outds (" + ,symget('varlistcomma') + ,") VALUES (" + ); + put _____str; + put " "@; + %end; %end; %else %do; - put " ,&var="@; + %if &flavour=BASE %then %do; + put " ,&var="@; + %end; + %else %if &flavour=PGSQL %then %do; + put " ,"@; + %end; %end; %if &vtype=N %then %do; - /* @todo - deal with nulls in other db flavours */ - /* from ._ to .z */ - put &var; + %if &flavour=BASE %then %do; + put &var; + %end; + %else %if &flavour=PGSQL %then %do; + if missing(&var) then put 'NULL'; + else put &var; + %end; %end; %else %do; _____str="'"!!trim(tranwrd(&var,"'","''"))!!"'"; put _____str; %end; %end; - put ';'; + %if &flavour=BASE %then %do; + put ';'; + %end; + %else %if &flavour=PGSQL %then %do; + put ');'; + %end; + if _n_=&nobs then put /; run; @@ -4355,12 +4397,13 @@ run; %mp_getddl(work,test,flavour=tsql,showlog=YES)

SAS Macros

+ @li mf_existfileref.sas @li mp_getconstraints.sas @param lib libref of the library to create DDL for. Should be assigned. @param ds dataset to create ddl for (optional) - @param fref= the fileref to which to write the DDL. If not preassigned, will - be assigned to TEMP. + @param fref= the fileref to which to _append_ the DDL. If it does not exist, + it will be created. @param flavour= The type of DDL to create (default=SAS). Supported=TSQL @param showlog= Set to YES to show the DDL in the log @param schema= Choose a preferred schema name (default is to use actual schema @@ -4376,9 +4419,10 @@ run; )/*/STORE SOURCE*/; /* check fileref is assigned */ -%if %sysfunc(fileref(&fref)) > 0 %then %do; - filename &fref temp; +%if %mf_existfileref(&outref)=0 %then %do; + filename &outref temp ; %end; + %if %length(&libref)=0 %then %let libref=WORK; %let flavour=%upcase(&flavour); @@ -4457,7 +4501,7 @@ create table _data_ as %mend addConst; data _null_; - file &fref; + file &fref mod; put "/* DDL generated by &sysuserid on %sysfunc(datetime(),datetime19.) */"; run; @@ -5114,6 +5158,8 @@ create table &outds (rename=( @li mf_getvartype.sas @param [in] libds dataset to hash + @param [in] salt= Provide a salt (could be, for instance, the name of the + dataset). Max 32 chars. @param [out] outds= (work.mf_hashdataset) The output dataset to create. This will contain one column (hashkey) with one observation (a hex32. representation of the input hash) @@ -5127,7 +5173,8 @@ create table &outds (rename=( %macro mp_hashdataset( libds, - outds= + outds=, + salt= )/*/STORE SOURCE*/; %if %mf_getattrn(&libds,NLOBS)=0 %then %do; %put %str(WARN)ING: Dataset &libds is empty;, or is not a dataset; @@ -5147,7 +5194,7 @@ create table &outds (rename=( %let varlist=%mf_getvarlist(&libds); data &outds(rename=(&keyvar=hashkey) keep=&keyvar); length &prevkeyvar &keyvar $32; - retain &prevkeyvar; + retain &prevkeyvar "&salt"; set &libds end=&lastvar; /* hash should include previous row */ &keyvar=put(md5(&prevkeyvar diff --git a/base/mp_ds2inserts.sas b/base/mp_ds2inserts.sas index b964673..09e510a 100644 --- a/base/mp_ds2inserts.sas +++ b/base/mp_ds2inserts.sas @@ -20,6 +20,10 @@ located. If not provided, is ignored. @param [out] outds= (0) The output table to load. If not provided, will default to the table in the &ds parameter. + @param [in] flavour= (BASE) The SQL flavour to be applied to the output. Valid + options: + @li BASE (default) - suitable for regular proc sql + @li PGSQL - Used for Postgres databases

SAS Macros

@li mf_existfileref.sas @@ -31,18 +35,29 @@ @author Allan Bowe (credit mjsq) **/ -%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0 +%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0,flavour=BASE )/*/STORE SOURCE*/; %if not %sysfunc(exist(&ds)) %then %do; - %put %str(WARN)ING: &ds does not exist; + %put %str(WAR)NING: &ds does not exist; + %return; +%end; + +%if not %sysfunc(exist(&ds)) %then %do; + %put %str(WAR)NING: &ds does not exist; %return; %end; %if %index(&ds,.)=0 %then %let ds=WORK.&ds; +%let flavour=%upcase(&flavour); +%if &flavour ne BASE and &flavour ne PGSQL %then %do; + %put %str(WAR)NING: &flavour is not supported; + %return; +%end; + %if &outref=0 %then %do; - %put %str(WARN)ING: Please provide a fileref; + %put %str(WAR)NING: Please provide a fileref; %return; %end; %if %mf_existfileref(&outref)=0 %then %do; @@ -73,8 +88,9 @@ select count(*) into: nobs TRIMMED from &ds; run; %end; -%local varlist; +%local varlist varlistcomma; %let varlist=%mf_getvarlist(&ds); +%let varlistcomma=%mf_getvarlist(&ds,dlm=%str(,),quote=double); /* next, export data */ data _null_; @@ -89,23 +105,49 @@ data _null_; %let var=%scan(&varlist,&i); %let vtype=%mf_getvartype(&ds,&var); %if &i=1 %then %do; - put "insert into &outlib.&outds set "; - put " &var="@; + %if &flavour=BASE %then %do; + put "insert into &outlib.&outds set "; + put " &var="@; + %end; + %else %if &flavour=PGSQL %then %do; + _____str=cats( + "INSERT INTO &outlib.&outds (" + ,symget('varlistcomma') + ,") VALUES (" + ); + put _____str; + put " "@; + %end; %end; %else %do; - put " ,&var="@; + %if &flavour=BASE %then %do; + put " ,&var="@; + %end; + %else %if &flavour=PGSQL %then %do; + put " ,"@; + %end; %end; %if &vtype=N %then %do; - /* @todo - deal with nulls in other db flavours */ - /* from ._ to .z */ - put &var; + %if &flavour=BASE %then %do; + put &var; + %end; + %else %if &flavour=PGSQL %then %do; + if missing(&var) then put 'NULL'; + else put &var; + %end; %end; %else %do; _____str="'"!!trim(tranwrd(&var,"'","''"))!!"'"; put _____str; %end; %end; - put ';'; + %if &flavour=BASE %then %do; + put ';'; + %end; + %else %if &flavour=PGSQL %then %do; + put ');'; + %end; + if _n_=&nobs then put /; run; diff --git a/base/mp_getddl.sas b/base/mp_getddl.sas index be92303..4b7d400 100644 --- a/base/mp_getddl.sas +++ b/base/mp_getddl.sas @@ -16,12 +16,13 @@ %mp_getddl(work,test,flavour=tsql,showlog=YES)

SAS Macros

+ @li mf_existfileref.sas @li mp_getconstraints.sas @param lib libref of the library to create DDL for. Should be assigned. @param ds dataset to create ddl for (optional) - @param fref= the fileref to which to write the DDL. If not preassigned, will - be assigned to TEMP. + @param fref= the fileref to which to _append_ the DDL. If it does not exist, + it will be created. @param flavour= The type of DDL to create (default=SAS). Supported=TSQL @param showlog= Set to YES to show the DDL in the log @param schema= Choose a preferred schema name (default is to use actual schema @@ -37,9 +38,10 @@ )/*/STORE SOURCE*/; /* check fileref is assigned */ -%if %sysfunc(fileref(&fref)) > 0 %then %do; - filename &fref temp; +%if %mf_existfileref(&outref)=0 %then %do; + filename &outref temp ; %end; + %if %length(&libref)=0 %then %let libref=WORK; %let flavour=%upcase(&flavour); @@ -118,7 +120,7 @@ create table _data_ as %mend addConst; data _null_; - file &fref; + file &fref mod; put "/* DDL generated by &sysuserid on %sysfunc(datetime(),datetime19.) */"; run; diff --git a/base/mp_hashdataset.sas b/base/mp_hashdataset.sas index d05ed94..1c05059 100644 --- a/base/mp_hashdataset.sas +++ b/base/mp_hashdataset.sas @@ -20,6 +20,8 @@ @li mf_getvartype.sas @param [in] libds dataset to hash + @param [in] salt= Provide a salt (could be, for instance, the name of the + dataset). Max 32 chars. @param [out] outds= (work.mf_hashdataset) The output dataset to create. This will contain one column (hashkey) with one observation (a hex32. representation of the input hash) @@ -33,7 +35,8 @@ %macro mp_hashdataset( libds, - outds= + outds=, + salt= )/*/STORE SOURCE*/; %if %mf_getattrn(&libds,NLOBS)=0 %then %do; %put %str(WARN)ING: Dataset &libds is empty;, or is not a dataset; @@ -53,7 +56,7 @@ %let varlist=%mf_getvarlist(&libds); data &outds(rename=(&keyvar=hashkey) keep=&keyvar); length &prevkeyvar &keyvar $32; - retain &prevkeyvar; + retain &prevkeyvar "&salt"; set &libds end=&lastvar; /* hash should include previous row */ &keyvar=put(md5(&prevkeyvar