diff --git a/all.sas b/all.sas index 91e94a4..d1abba4 100644 --- a/all.sas +++ b/all.sas @@ -2250,7 +2250,7 @@ run; %if &outfound=0 %then %do; filename &outref temp lrecl=2097088; -%%end; +%end; %if &action=ENCODE %then %do; data _null_; @@ -2258,12 +2258,12 @@ run; retain line ""; infile &inref recfm=F lrecl= 1 end=eof; input @1 stream $char1.; - file &outref lrecl=76; + file &outref recfm=N; substr(line,(_N_-(CEIL(_N_/57)-1)*57),1) = byte(rank(stream)); if mod(_N_,57)=0 or EOF then do; if eof then b64=put(trim(line),$base64X76.); else b64=put(line, $base64X76.); - put b64; + put b64 + (-1) @; line=""; end; run; @@ -2275,19 +2275,13 @@ run; fileout = fopen("&outref",'O',3,'B'); char= '20'x; do while(fread(filein)=0); - raw="1234"; + length raw $4; do i=1 to 4; rc=fget(filein,char,1); substr(raw,i,1)=char; end; - val="123"; - val=input(raw,$base64X4.); - do i=1 to 3; - length byte $1; - byte=byte(rank(substr(val,i,1))); - rc = fput(fileout, byte); - end; - rc =fwrite(fileout); + rc = fput(fileout,input(raw,$base64X4.)); + rc = fwrite(fileout); end; rc = fclose(filein); rc = fclose(fileout); @@ -3429,6 +3423,117 @@ data &outds; run; %mend mp_ds2fmtds;/** + @file + @brief Export a dataset to SQL insert statements + @details Converts dataset values to SQL insert statements for use across + multiple database types. + + Usage: + + %mp_ds2inserts(sashelp.class,outref=myref,outds=class) + data class; + set sashelp.class; + stop; + proc sql; + %inc myref; + + @param [in] ds The dataset to be exported + @param [out] outref= (0) The output fileref. If it does not exist, it is + created. If it does exist, new records are APPENDED. + @param [out] outlib= (0) The library (or schema) in which the target table is + 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. + +

SAS Macros

+ @li mf_existfileref.sas + @li mf_getvarcount.sas + @li mf_getvarlist.sas + @li mf_getvartype.sas + + @version 9.2 + @author Allan Bowe (credit mjsq) +**/ + +%macro mp_ds2inserts(ds, outref=0,outlib=0,outds=0 +)/*/STORE SOURCE*/; + +%if not %sysfunc(exist(&ds)) %then %do; + %put %str(WARN)ING: &ds does not exist; + %return; +%end; + +%if %index(&ds,.)=0 %then %let ds=WORK.&ds; + +%if &outref=0 %then %do; + %put %str(WARN)ING: Please provide a fileref; + %return; +%end; +%if %mf_existfileref(&outref)=0 %then %do; + filename &outref temp lrecl=66000; +%end; + +%if &outlib=0 %then %let outlib=; +%else %let outlib=&outlib..; + +%if &outds=0 %then %let outds=%scan(&ds,2,.); + +%local nobs; +proc sql noprint; +select count(*) into: nobs TRIMMED from &ds; +%if &nobs=0 %then %do; + data _null_; + file &outref mod; + put "/* No rows found in &ds */"; + run; +%end; + +%local vars; +%let vars=%mf_getvarcount(&ds); +%if &vars=0 %then %do; + data _null_; + file &outref mod; + put "/* No columns found in &ds */"; + run; +%end; + +%local varlist; +%let varlist=%mf_getvarlist(&ds); + +/* next, export data */ +data _null_; + file &outref mod ; + if _n_=1 then put "/* &outlib.&outds (&nobs rows, &vars columns) */"; + set &ds; + length _____str $32767; + format _numeric_ best.; + format _character_ ; + %local i comma var vtype; + %do i=1 %to %sysfunc(countw(&varlist)); + %let var=%scan(&varlist,&i); + %let vtype=%mf_getvartype(&ds,&var); + %if &i=1 %then %do; + put "insert into &outlib.&outds set "; + put " &var="@; + %end; + %else %do; + put " ,&var="@; + %end; + %if &vtype=N %then %do; + /* @todo - deal with nulls in other db flavours */ + /* from ._ to .z */ + put &var; + %end; + %else %do; + _____str="'"!!trim(tranwrd(&var,"'","''"))!!"'"; + put _____str; + %end; + %end; + put ';'; + if _n_=&nobs then put /; +run; + +%mend mp_ds2inserts;/** @file @brief Checks an input filter table for validity @details Performs checks on the input table to ensure it arrives in the diff --git a/base/mp_ds2inserts.sas b/base/mp_ds2inserts.sas index 789524c..b964673 100644 --- a/base/mp_ds2inserts.sas +++ b/base/mp_ds2inserts.sas @@ -76,9 +76,6 @@ select count(*) into: nobs TRIMMED from &ds; %local varlist; %let varlist=%mf_getvarlist(&ds); -proc format; /* credit yabwon for special null removal */ - value bart ._ - .z = null - other = [best.]; /* next, export data */ data _null_; file &outref mod ;