diff --git a/all.sas b/all.sas index 9bf5e5d9..6bd54622 100644 --- a/all.sas +++ b/all.sas @@ -2645,6 +2645,63 @@ quit; %put NOTE-;%put NOTE-; %put NOTE- %sysfunc(dequote(&cards_file.)); %put NOTE-;%put NOTE-; +%mend;/** + @file + @brief Export a dataset to a CSV file + @details Export to a file or a fileref + Usage: + + %mp_ds2csv(sashelp.class,outref="%sysfunc(pathname(work))/file.csv") + + @param ds The dataset to be exported + @param outfile= The output filename - should be quoted. + @param outref= The output fileref (takes precedence if provided) + @param outencoding= The output encoding to use (unquoted) + + @version 9.2 + @author Allan Bowe (credit mjsq) +**/ + +%macro mp_ds2csv(ds, outref=0, outfile=, outencoding=0 +)/*/STORE SOURCE*/; + +%if not %sysfunc(exist(&ds)) %then %do; + %put WARNING: &ds does not exist; + %return; +%end; + +%if %index(&ds,.)=0 %then %let ds=WORK.&ds; + +%if &outencoding=0 %then %let outencoding=; +%else %let outencoding=encoding="&outencoding"; + +%local outloc; +%if &outref=0 %then %let outloc=&outfile; +%else %let outloc=&outref; + +/* credit to mjsq - https://stackoverflow.com/a/55642267 */ + +/* first get headers */ +data _null_; + file &outloc dlm=',' dsd &outencoding lrecl=32767; + length header $ 2000; + dsid=open("&ds.","i"); + num=attrn(dsid,"nvars"); + do i=1 to num; + header = trim(left(coalescec(varlabel(dsid,i),varname(dsid,i)))); + put header @; + end; + rc=close(dsid); +run; + +/* next, export data */ +data _null_; + set &ds.; + file &outloc mod dlm=',' dsd &outencoding lrecl=32767; + put (_all_) (+0); +run; + + %mend;/** @file mp_getconstraints.sas @brief Get constraint details at column level diff --git a/base/mp_ds2csv.sas b/base/mp_ds2csv.sas new file mode 100644 index 00000000..b62adf86 --- /dev/null +++ b/base/mp_ds2csv.sas @@ -0,0 +1,58 @@ +/** + @file + @brief Export a dataset to a CSV file + @details Export to a file or a fileref + Usage: + + %mp_ds2csv(sashelp.class,outref="%sysfunc(pathname(work))/file.csv") + + @param ds The dataset to be exported + @param outfile= The output filename - should be quoted. + @param outref= The output fileref (takes precedence if provided) + @param outencoding= The output encoding to use (unquoted) + + @version 9.2 + @author Allan Bowe (credit mjsq) +**/ + +%macro mp_ds2csv(ds, outref=0, outfile=, outencoding=0 +)/*/STORE SOURCE*/; + +%if not %sysfunc(exist(&ds)) %then %do; + %put WARNING: &ds does not exist; + %return; +%end; + +%if %index(&ds,.)=0 %then %let ds=WORK.&ds; + +%if &outencoding=0 %then %let outencoding=; +%else %let outencoding=encoding="&outencoding"; + +%local outloc; +%if &outref=0 %then %let outloc=&outfile; +%else %let outloc=&outref; + +/* credit to mjsq - https://stackoverflow.com/a/55642267 */ + +/* first get headers */ +data _null_; + file &outloc dlm=',' dsd &outencoding lrecl=32767; + length header $ 2000; + dsid=open("&ds.","i"); + num=attrn(dsid,"nvars"); + do i=1 to num; + header = trim(left(coalescec(varlabel(dsid,i),varname(dsid,i)))); + put header @; + end; + rc=close(dsid); +run; + +/* next, export data */ +data _null_; + set &ds.; + file &outloc mod dlm=',' dsd &outencoding lrecl=32767; + put (_all_) (+0); +run; + + +%mend; \ No newline at end of file