42%macro mp_ds2inserts(ds, outref=0,schema=0,outds=0,flavour=SAS,maxobs=max
46%
if not %sysfunc(exist(&ds)) %then %
do;
47 %put %str(WAR)NING: &ds does not exist;
51%
if not %sysfunc(exist(&ds)) %then %
do;
52 %put %str(WAR)NING: &ds does not exist;
56%
if %index(&ds,.)=0 %then %let ds=WORK.&ds;
58%let flavour=%upcase(&flavour);
59%
if &flavour ne SAS and &flavour ne PGSQL %then %
do;
60 %put %str(WAR)NING: &flavour is not supported;
64%
if &outref=0 %then %
do;
65 %put %str(WAR)NING: Please provide a fileref;
68%
if %mf_existfileref(&outref)=0 %then %
do;
69 filename &outref temp lrecl=66000;
72%
if &schema=0 %then %let schema=;
73%
else %let schema=&schema..;
75%
if &outds=0 %then %let outds=%scan(&ds,2,.);
79select count(*) into: nobs TRIMMED from &ds;
88%let vars=%mf_getvarcount(&ds);
96%else %if &vars>1600 and &flavour=PGSQL %then %do;
106%local varlist varlistcomma;
107%let varlist=%mf_getvarlist(&ds);
108%let varlistcomma=%mf_getvarlist(&ds,dlm=%str(,),quote=
double);
113 if _n_=1 then put "";
115 %if &maxobs ne max %then %do;
116 if _n_>&maxobs then stop;
118 length _____str $32767;
119 call missing(_____str);
120 format _numeric_ best.;
122 %local i comma var vtype vfmt;
123 %do i=1 %to %sysfunc(countw(&varlist));
124 %let var=%scan(&varlist,&i);
125 %let vtype=%mf_getvartype(&ds,&var);
126 %let vfmt=%upcase(%mf_getvarformat(&ds,&var,force=1));
128 %if &flavour=SAS %then %do;
129 put "insert into &schema.&outds set ";
132 %else %if &flavour=PGSQL %then %do;
134 "INSERT INTO &schema.&outds ("
135 ,symget('varlistcomma')
143 %if &flavour=SAS %then %do;
146 %else %if &flavour=PGSQL %then %do;
150 %if &vtype=N %then %do;
151 %if &flavour=SAS %then %do;
154 %else %if &flavour=PGSQL %then %do;
155 if missing(&var) then put 'NULL';
156 %if &applydttm=YES and "%substr(&vfmt.xxxxxxxx,1,8)"="DATETIME"
158 else put "TIMESTAMP '" &var E8601DT25.6 "'";
166 _____str="'"!!trim(tranwrd(&var,"'","''"))!!"'";
170 %if &flavour=SAS %then %do;
173 %else %if &flavour=PGSQL %then %do;
177 if _n_=&nobs then put /;