這篇文章為代碼備份,代碼參考SAS編程:如何將數據集批量輸出到Excel?。
Review數據集時,將數據集批量輸出到EXCEL中進行簡單的篩選查詢,是一個比較實用的方法。為了程序穩定、快速運行,輸出采用proc export
的方法。
以下為代碼備份,方便引用。
***1. Set library and Excel file;
libname adam "/xxx/adam" access = readonly;
%global libname excel;
%let libname = ADaM;
%let excel = /xx/xxx/20210144_adam_&sysdate..xlsx;
***2. Create the macro;
%macro exp_dt(lib=, res=);
**2.1 Get datasets name and number in the library;
data memname;
set sashelp.vtable end = eof;
where libname = %upcase("&lib.") ;
call symputx("name"||strip(put(_n_, best.)), strip(memname));
if eof then call symputx("nobs", strip(put(_n_, best.)));
run;
%put name1 = &name1;
%put nobs = &nobs;
**2.2 Export all the datasets;
%macro loop;
%do i = 1 %to &nobs;
proc export data=&lib..&&name&i.
outfile="&res."
dbms=xlsx replace;
sheet = "&&name&i.";
run;
%end;
%mend loop;
%loop;
%mend exp_dt;
***3. Invoke the macro ;
%exp_dt(
lib = %str(&libname.)
,res = %str(&excel.)
);
感謝閱讀,歡迎關注:SAS茶談!
若有疑問,歡迎評論交流!