SAS 将文件自动导入,并自动获取文件名

来源:互联网 发布:最快的网络借款 编辑:程序博客网 时间:2024/05/02 15:39

这个是我最近解决的问题,欢迎大家批评指点哈!


概述:SAS 可以用input,在指定column 名字的情况下,load数据;可以用proc import 来导入未知column个数的数据,他会自动生成每一列的格式,但有个弊端,就是针对空的列,他会assign char的格式,如果我要append同样格式的文件到第一个文件,第二个文件中那个列不为空,这时,就会出现问题。

下面的方法可以解决,把多个格式一样的数据,load到sas的一个表中,而且可以自动提取表名。


/*summary of the macro variables


Used for Comma delimited csv files. 

num  -- how many columns in the file

filename -- file name you want to do the qc and summary(including file location
*/

%macro infiledata(filename);
proc import datafile="&filename" out=aa replace dbms=csv;
run;
******get the the quantity of the column***********;
%let dsid=%sysfunc(open(aa),i);
%let num=%sysfunc(attrn(&dsid,nvars));
%let rc=%sysfunc(close(&dsid));


%macro importfile(n);
data b0;
infile "&filename"delimiter=',' MISSOVERDSD lrecl=32767firstobs=2 ;
length value1-value&n $ 200;
input value1-value&n $ ;
RUN;
%mend importfile;
%importfile(&num);
********get the column name and the location of the column*******;


proc contents data=aa out=o1(keep=varnum name);
run;


data _null_;
set o1 (keep=name);
call symputx('rc','1');
where upcase(name)="%upcase(mailing_key)";
run;

%macro insertv(n,name);
%do i=1 %to &n;
%let j=%eval(&i-1);


proc sql;
select distinct name
into: v&i
from o1
where varnum=&i;
QUIT;


data b&i;
set b&j;
rename &name&i=&&v&i;
run;
proc sql;
drop table b&j;
quit;
%if &i=&n %then %do ;
data work.tempwavetables;
set b&i;
run;
proc delete data= b&i;run;
%end;
%end;


%mend insertv;


%insertv(&num,value);


%macro append();
proc sql;
insert into work.wavetables  
select "&filename"  as file_name format=$200.
,*
from work.tempwavetables tab;
quit;


proc delete data= work.tempwavetables;
run;
%mend append;


%MACRO check(filename);
%if %sysfunc(exist(work.wavetables)) %then %do;
%append();
%end;


%else %do;


Proc sql;
CREATE table work.wavetables as
select "&filename" as file_name format=$200.
,*
from work.tempwavetables;
quit;


Proc sql;
alter table work.wavetables
modify file_name char(200);
run;


proc delete data= work.tempwavetables;
run;
%END;
%mend check;


%check(&filename);


*%drop temp tables;
proc sql;
drop table out;
quit;
proc sql;
drop table aa;
quit;

%mend infiledata;




%macro runmc(n);
%if %length(%scan(&&file&n,1,'|'))>1 %then %do;
%infiledata(filename=&&file&n);

%end;
%mend runmc;


%runmc(1);
%runmc(2);
%runmc(3);
%runmc(4);
%runmc(5);
run;

0 0
原创粉丝点击