| 
 | 
7#
 
 
 楼主 |
发表于 2010-5-9 13:25:32
|
只看该作者
 
 
 
Re: import多个csv文件并合并
版本1: 鸣谢tianwild的赞助 
[code:lo5954wi]%macro import1(path,out_ds); 
        options nomprint nosymbolgen; 
        %if %sysfunc(exist(&out_ds)) %then  
                %do; 
                        proc datasets library=work nolist; 
                                delete &out_ds; 
                        quit; 
                %end; 
        %let filrf=mydir; 
        %let rc=%sysfunc(filename(filrf,&path)); 
        %let did=%sysfunc(dopen(&filrf)); 
        %do i=1 %to %sysfunc(dnum(&did)); 
            %let memname=%sysfunc(dread(&did,&i)); 
                %if %upcase(%scan(&memname,-1,'.'))=CSV %then  
                        %do; 
                                proc import out=_temp datafile="&path\&memname" dbms=csv replace; 
                                        getnames=yes; 
                                        datarow=2; 
                                run; 
                                data _temp; 
                                        length In $200; 
                                        set _temp; 
                                        in="&memname"; 
                                run; 
                                proc datasets library=work nolist; 
                                        append base=&out_ds data=_temp; 
                                quit; 
                        %end; 
        %end; 
        %let rc=%sysfunc(dclose(&did)); 
        proc datasets library=work nolist; 
                delete _temp; 
        quit; 
%mend; 
%import1(D:\test,out1)[/code:lo5954wi] 
版本2 
[code:lo5954wi]%macro import2(path,var_list,out_ds); 
        options nomprint nosymbolgen noxwait xmin; 
        %macro inner(fname); 
                data _temp; 
                        length In $200; 
                        infile "&path\&fname" firstobs=2 dlm=','; 
                        input &var_list; 
                        in="&fname"; 
                run; 
                proc datasets library=work nolist; 
                        append base=&out_ds data=_temp; 
                quit; 
        %mend; 
        %if %sysfunc(exist(&out_ds)) %then  
                %do; 
                        proc datasets library=work nolist; 
                                delete &out_ds; 
                        quit; 
                %end; 
        x "cd &path"; 
        x "dir :attrib *.csv /o:-d/b >filename.txt"; 
        data _null_; 
                length filename $200; 
                infile "&path\filename.txt" truncover; 
                input filename $1-200; 
                call execute('%inner('||trim(filename)||')'); 
        run; 
        x "del filename.txt"; 
        proc datasets library=work nolist; 
                delete _temp; 
        quit; 
%mend; 
%import2(d:\test,x y,out2)[/code:lo5954wi] 
版本3 
[code:lo5954wi]%macro import3(path,var_list,out_ds); 
        options nomprint nosymbolgen noxwait xmin; 
        x "cd &path"; 
        x "dir :attrib *.csv /o:-d/b >filename.txt"; 
        data &out_ds; 
                length filename $200; 
                infile "&path\filename.txt" truncover; 
                input filename $1-200; 
                filename=cats("&path\",filename); 
                do until(last); 
                        infile CSV filevar=filename end=last firstobs=2 dlm=','; 
                        input &var_list; 
                        In=filename; 
                        output; 
                end; 
        run; 
        x "del filename.txt"; 
%mend; 
%import3(d:\test,x y,out3)[/code:lo5954wi] |   
 
 
 
 |