以编程方式将数据设置为多个文件

时间:2014-02-19 19:29:15

标签: sas

我有一个包含文件名的数据集。这些文件名是文本文件,我想导入它们创建一个包含所有文件的主数据集。这是我的代码:

%macro create_data();
proc sql noprint;
  select count(*) into :count from Filenames;* Filenames is dataset with Filenames;
  select fname into :filelist separated by " " from Filenames ;
quit;

%do i = 1 %to &count.;
 *import file iteratively;
 proc import datafile='c:\temp\filelist[i].txt' *stuck here;
 out=subset
 dbms=dlm
 replace;
 delimiter=' ';
 run;
 *stuck here as well. Trying to concatenate datasets iteratively to create master;
 data master;
  set subset;
 run;
%end;
%mend;

1 个答案:

答案 0 :(得分:2)

假设您的文件在结构上完全相同,请尝试以下操作:

%macro create_data();
proc sql noprint;
  select count(*) into :count from Filenames;* Filenames is dataset with Filenames;
  select fname into :filelist separated by " " from Filenames ;
/* quit removed - as not necessary */

%do i = 1 %to &count.;
 *import file iteratively;
 /* change 1 */
 proc import datafile="c:\temp\%scan(&filelist,&i).txt" *stuck here; 
 out=subset
 dbms=dlm
 replace;
 delimiter=' ';
 run;
 *stuck here as well. Trying to concatenate datasets iteratively to create master;
 /* change 2 - if master does not exist, is created "like" subset */
 proc append base= master data= subset;
 run;
%end;
%mend;